English subtitles

← Use Return Value from Method - Solution

Get Embed Code
13 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. First we're going to modify
    the calculatePrice method signature
  2. to have the correct return data type.
  3. We're also going to assume that
    there's 0 input parameters.
  4. This is where the calculatePrice
    method is defined.
  5. The purpose of this method is to
    calculate the total price of the order
  6. and then return that.
  7. The price should be an integer so
  8. we should change the return
    data type from void to int.
  9. That takes care of the first task.
  10. The quiz also said to make sure that
    there's no input parameters, and
  11. that's what we have here.
  12. Now the second task is to
    modify the inside of the method
  13. to make sure that we're computing
    the correct price for the order.
  14. And assuming $5.00 for a cup of coffee.
  15. Inside this method we're taking
    the quantity of cups ordered.
  16. And we're multiplying it
    by $5 per cup of coffee.
  17. This equals the total order price.
  18. So this line of code is cracked.
  19. But there's two issues that I see,
    the variable price is never used and
  20. there's an error that says
    missing return statement.
  21. It's expecting us to return an integer,
    but we're not returning anything.
  22. Right now our code is the same
    as if we just said this.
  23. We need a line that says return and
    then actually put a value here and
  24. the value on return is a price.
  25. So, I'm just going to put price,
    which is a variable we defined up here.
  26. Cool, so now all the errors go away.
  27. We should also modify the Java DOC.
  28. So that we specify that we're
    turning the total price.
  29. That way when someone's calling this
    method, they know what to expect.
  30. Okay, so now we finished all
    the parts for defining this method.
  31. Now, let's move on to where
    the method is getting called.
  32. Within the submitOrder method, we
    should call the calculatePrice method.
  33. Currently calculate price,
    is floating in the middle of nowhere.
  34. I'm going to copy it, and
    I'm going to paste it up here.
  35. Because before we would calculate
    the price of the order in line,
  36. in the submit order method, but
    now we have a method for it.
  37. So I'm going to paste this here.
  38. Now get rid of the extra semicolon.
  39. You'll notice that price is a local
    variable in this method, and
  40. also in this method.
  41. It's okay if they have the same name,
  42. because these are different
    variable scopes.
  43. So the price variable here is totally
    unrelated with the variable here.
  44. They're updated and
    created independently from each other.
  45. And once this method finishes executing,
    this price variable is gone.
  46. Okay back to our submitOrder method.
  47. So we calculated a price and
  48. stored it in a variable, and then we
    can use it to create the price message.
  49. So, actually,
    all of this code can stay the same.
  50. We're concatenating the integer,
  51. price, to the string total
    with the currency symbol.
  52. Then we concatenate "Thank you!" and
    then just display it on the screen.
  53. So I'm going to erase these extra,
    blank lines, and save it, and
  54. then we can run it on our device.
  55. Okay, so I changed the quantity, and
  56. hit ORDER, then it correctly
    shows $25 as the price.
  57. Awesome, it worked.
  58. But how can I be sure that
    calculatePrice is actually
  59. working because there's no user visible
    change compared to our app from before.
  60. Here are a couple of techniques that
    you can do to verify that your code is
  61. actually running.
  62. I could have it return a really
    crazy number, like 700.
  63. Save my code and then run it.
  64. Now what I expect to see is
    if I change the quantity and
  65. hit the ORDER button,
    it should saw that the price is $700.
  66. That way I'll know that this calculate
    price method is actually getting called.
  67. And it does, cool.
  68. Now I can revert this change.
  69. Another way to check that this
    method is getting executed
  70. is to add a break point here.
  71. So I'm going to click on
    this little bug icon and
  72. then our apple enter debug mode.
  73. If I change the quantity and
  74. I hit order, then I expect that
    the app will pause at this point.
  75. And it does.
  76. So now we're in
    the calculate price method.
  77. There is a global variable, quantity.
  78. And if I step over to the next line then
    I see that there's now a local variable,
  79. price And it's set to be $15 because
    quantity is 3, 3 times 5 is 15, cool.
  80. So, it's going through
    the calculated price method, and
  81. if I hit Resume,
    then it updates properly on this screen.