English (United States) subtitles

← TextView Text Size

Get Embed Code
14 Languages

Showing Revision 4 created 09/24/2016 by Udacity Robot.

  1. Okay Katherine.
  2. >> Mm-hm.
    >> So
  3. when we hit the plus button in that app.
  4. The number of cups of coffee goes up.
  5. >> Yeah it goes from
    two cups to three cups.
  6. And what happens when we
    press the plus button again?
  7. >> Well it actually just stays at three.
  8. Yeah.
  9. So no matter how many times we
    press the plus button in that app,
  10. the number of cups of
    coffee stays at three.
  11. >> Yeah.
    >> Why is that happening?
  12. >> Well, I think it's because,
    in our code, we only update it to three.
  13. And we never go beyond that.
  14. >> Right.
    That number is hard coded right there.
  15. >> Yeah.
    >> You know what this remind me of?
  16. >> What is it?
  17. >> Well, [LAUGH] my computer science
    professor in college used to say.
  18. >> Yeah?
  19. When you submit your final project.
  20. >> Yeah?
  21. >> Please, no magic numbers.
  22. >> Wait, what's a magic number?
  23. >> Well, three's kind of a magic number.
  24. >> Okay.
    >> It's a magic number because,
  25. we are doing the addition in our head,
    but then to the computer.
  26. >> We're telling it the right answer.
  27. >> Okay, yeah, it would be better if
    the computer actually calculated itself.
  28. >> Wait.
  29. When you talk about calculation in math.
  30. >> Uh-huh.
    >> I need to start writing things down.
  31. >> Okay.
  32. Let's write some pseudocode.
  33. Let's write the pseudocode for this.
  34. Assume that we've already created
    quantity variable and set it to two.
  35. When the plus button
    in the app is pressed,
  36. we're going to take the current
    quantity value which is two.
  37. >> Add one to it to make it three, and
  38. then store that as the new value
    inside the quantity variable.
  39. If we hit the plus button again,
    then we're going to take the current
  40. quantity value which is now three,
    and add one to it which makes it four.
  41. And then store that as the new
    value inside the variable,
  42. and here's the actual Java code for it.
  43. We initialize a variable first.
  44. If the plus button is pressed again,
  45. then we're going to take the existing
    quantity value, add a one to it,
  46. and then store this as the new value for
    the quantity variable.
  47. When you read these lines of code,
    you can imagine the boxes in your head.
  48. Even though we're used to reading
    things from left to right,
  49. I always start with what's on the right
    hand side of the equal sign first.
  50. In this case,
    we have the current value of quantity.
  51. Which is two, and then we add one to it,
    and that makes three.
  52. Then we store the three inside
    the variable, like this.
  53. The righthand side is an expression,
    which we learned about earlier.
  54. And an expression can be made
    up variables, operators and
  55. literal values, and more.
  56. If we click on the plus button again,
    then we can say quantity = quantity + 1.
  57. At this point the current
    value of quantity is three,
  58. then we add one to it,
    and that makes four.
  59. Then we store four as the new value
    inside the quantity variable like this.
  60. Essentially we've have reduced
    it down to a common pattern,
  61. that the computer can repeat each
    time the plus button is pressed.
  62. Instead of having to write a new line
    of code that says the explicit value of
  63. the number of cups of coffee,
    we can just do quantity = quantity + 1,
  64. and continue to build off of
    the existing value of quantity.
  65. This is a lot more flexible.
  66. Say you You wanted to change
    initial quantity to 3,
  67. then the rest of the lines of code
    wouldn't have to change at all.
  68. Because we would always use the latest
    value of quantity, whatever it is,
  69. which, in our old approach, if we wanted
    to update the quantity value to 3,
  70. then after you hit the plus button, you
    would have to write a new line of code
  71. that says quantity equals 4 and
    update this to be quantity equals 5.
  72. When we update the quantity to a
    completely new literal value each time,
  73. it's hard for the computer to know
    what the pattern is between them.
  74. Instead if we update quantity based on
    the current value of that variable and
  75. we increment it in a predictable way,
    then the computer will be able to do
  76. this every single time that
    the plus button is pressed.
  77. This approach will work a lot better for
    our coffee ordering app.
  78. Each time we hit the plus button,
  79. we can just increase the existing
    quantity value by 1.
  80. Practice writing out each line of code
    by typing them into the boxes we've
  81. provided here.
  82. Pay attention to each character and
  83. This is the first time you're updating a
    variable, using the variable itself, so
  84. make sure you type out this
    variable name correctly.
  85. When you're done, fill out this column
    that represents the current quantity
  86. value after each line
    of code is executed.