English (United States) feliratok

← Inputs to a Method

Beágyazókód kérése
14 Languages

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

  1. Wait.
  2. >> What's wrong?
  3. >> When did this turn into
    a cooking show all of a sudden?
  4. >> Since we had to talk about methods?
  5. >> Care to explain, Rachael Ray?
  6. >> [LAUGH] Well,
    have you ever baked a cake before?
  7. >> I have eaten a cake before.
  8. >> Okay.
  9. Well, then you're fully qualified for
    this.
  10. >> Okay.
  11. >> All you have to do to bake
    a cake is to put in flour, sugar.
  12. >> The ingredients.
  13. Okay.
    >> The ingredients.
  14. Yeah.
  15. And then you follow
    the instructions of the recipe.
  16. >> Okay.
    >> And then out comes a cake.
  17. >> Like this?
  18. >> Yeah.
  19. >> Okay.
    Well
  20. connect the analogy to
    the methods one more time.
  21. >> Okay.
    Sure so for
  22. a method you can put in inputs.
  23. >> Like we did the quantity for
    the display method?
  24. >> Yeah, and then you execute
    the instructions of the method.
  25. >> Okay, almost like the recipe for
    making a cake?
  26. >> Mm-hm.
  27. Then you can have
    the output value returned.
  28. >> Outputs, they are delicious.
  29. >> [LAUGH]
    >> Let's look up more information
  30. about input parameters for methods.
  31. I'm going to do a Google search for
    Jjava method parameter.
  32. The first link looks good so
    I'm going to click on it.
  33. Now this article talks about
    passing information to a method.
  34. Here's a code snippet that computes
    the monthly payments for a home loan.
  35. It has four input parameters,
    loan amount, interest rate, future value
  36. of the loan, as well as the number
    periods of the length of the loan.
  37. And then inside this method we see
    that we can use these input parameters
  38. simply as variables to do
    our math calculations.
  39. When we call the compute payment method,
  40. we have to make sure we pass in
    the inputs in this specific order.
  41. So the loan amount has to go first,
    and then the rate and so on.
  42. For the quiz in this section,
  43. you'll have the chance to review
    this article in more detail.
  44. Within the main activity
    of the desk Java app,
  45. we're going to be modifying
    the display quantity method and
  46. the input parameter to
    give you some practice.
  47. Notice that in the decrement
    method of our app,
  48. what we call the display
    quantity method,
  49. we're passing the quantity variable
    over as input to this method.
  50. But the input parameter
    is called 'number', so
  51. whenever we want to refer to this input,
    we just have to say number.
  52. Now technically we could have used
    the global quantity variable within this
  53. method instead of passing
    it as an input parameter.
  54. but for the purposes of teaching
    you about input parameters,
  55. let's just leave it like this.
  56. I can change this call to
    the display quantity method so
  57. it passes input, the number 30.
  58. This is valid because it still
    matches the input data type.
  59. So in the decrement method when
    we call it display quantity,
  60. it will jump down here and
    it will execute these instructions, and
  61. the variable number will
    have the value of 30.
  62. I'll run it on my device to show you,
    so now when I run the minus button on my
  63. app, it will display
    a quantity of 30 here.
  64. So let me try that.
  65. There.
    And it actually happened like that.
  66. I can also create a different variable
    and pass that as an input to the method.
  67. Here I've created a local variable
    called some quantity, and
  68. I initialized it to 100.
  69. I passed this variable
    as input to this method,
  70. and now the number variable
    inside here has a value of 100.
  71. Let me run this on the device.
  72. Now I expect when I hit the minus
    button the quantity will update to 100.
  73. And it does.
  74. These are just some things
    you can do to experiment with
  75. passing in different inputs to a method.
  76. As you work with input parameters,
  77. another term that you might
    come across is arguments.
  78. When you call a method,
  79. the inputs being passed to that
    method are called the arguments.
  80. Now this is kind of a funny word.
  81. Because when you think of arguments,
    you think of people yelling and
  82. cursing at each other.
  83. But this is not like that.
  84. Arguments are simply the inputs
    being passed to a method.
  85. Input parameters are the actual
    inputs in your method declaration and
  86. those are the variables that
    are used within your method.
  87. So that's a slight difference between
    input parameter and arguments.
  88. In any case, go ahead and
    read the article and
  89. the instructor notes
    about method parameters.
  90. Then go ahead and
    experiment with the code in your app.
  91. First try to rename
    the number input parameter
  92. within the display quantity
    method declaration.
  93. Just simply give it
    a different variable name.
  94. Then go ahead and fix any errors,
    and then run the app again.
  95. For the next test, go ahead and
    change the data type
  96. of the number input parameter from
    an int to something else like String.
  97. Observe what happens and
    then go ahead and undo the change.
  98. If you want to experiment with
    other things, go right ahead.
  99. For example, if you have a question,
  100. what would happen if I do x,
    then just go ahead and try it.