YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← Add or Remove Parameters

Get Embed Code
13 Languages

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

  1. I'm going to walk you through
    an example of a method with a different
  2. number of input parameters.
  3. I started by creating a new app from
    the project wizard in Android Studio, so
  4. this is completely unrelated
    to the Just Java app.
  5. I added this method
    in the main activity.
  6. It's called createWeatherMessage.
  7. There's no input parameters, and
    the return value is a string.
  8. This method is supposed to create a new
    message that specifies the city name and
  9. the temperature.
  10. You can think of a method as
    being a template of instructions.
  11. So I can make temperature
    be an input parameter.
  12. And it's a number, so
    I'm going to say, int temperature.
  13. And then I can use this right here.
  14. So, this is a string literal, and
  15. then it's concatenated with
    the temperature integer.
  16. And then it's concatenated with another
    string that says degrees Fahrenheit.
  17. So now the message can be customized
    based on the temperature because it's
  18. an input parameter.
  19. Oh yeah, I forgot to mention this
    earlier, but in the onCreate method of
  20. the main activity, which is called
    when the activity gets created,
  21. I can call the createWeatherMessage.
  22. Before I passed in no arguments, but
    now we have one required parameter.
  23. So I'm going to pass in 77 for
    the temperature.
  24. Now that makes the error go away,
    and this method call is correct.
  25. The other thing I should modify
    is this comment on the method.
  26. I'm going to add a blank line, and
  27. then I'll start to describe the input
    parameter which is temperature here.
  28. I'm going to type @param temperature,
    and
  29. then I can add a description saying that
    this is the temperature of the city.
  30. Sometimes the parameter names
    are a little hard to understand, so
  31. that's why the description is useful.
  32. Other times if your method is public
    then other people will be able
  33. to call this method, and
  34. they might not know what
    are the instructions inside this method.
  35. So reading this java doc and
  36. the description of the parameters
    is really useful to them.
  37. I mentioned earlier that
    a method is like a template.
  38. So, what if we want to change the city
    so that it's not always San Francisco?
  39. I can make city be an input parameter.
  40. So I can just add a comma here.
  41. And since city name is text I'm
    going to make it a string data type.
  42. Then I can delete San Francisco and
    replace it with a variable city name.
  43. And remember to add a space before and
    after here as well.
  44. Cool, so
    now this method can create a message
  45. that's customizable to
    a certain city and temperature.
  46. I also remembered to
    modify the java doc so
  47. that it has this extra parameter and
    description for a city name.
  48. Since I changed the method signature,
    now I have an error up here.
  49. The method no longer works
    with just one argument.
  50. I need to pass in two arguments.
  51. So I add a comma and
    then pass in San Francisco string.
  52. Now the error goes away.
  53. So when I call createWeatherMessage(77,
    it's going to be value of temperature,
  54. and San Francisco will be
    the value of city name.
  55. And it will create
    the proper string here.
  56. Now we're going to go back to
    the calculate price method,
  57. and we're going to modify
    the method signature so
  58. that it has a different
    number of input parameters.
  59. We're going to define it with zero
    input parameters, one input parameter,
  60. and lastly,
    try it with two input parameters.
  61. You'll see how changing the method
    signature here to have a different
  62. number of input parameters with affect
    the way that the method is called.
  63. Normally when you're building your app,
  64. you will just define it one way with
    a certain number of input parameters.
  65. And usually that number of input
    parameters is going to be determined
  66. based off of what's
    happening inside the method.
  67. The best practice is to
    only pass input parameters
  68. that you're actually going
    to need inside this method.
  69. There's no point in passing over more
    information if you're not going to
  70. use it inside here.
  71. Here are the instructions for this task.
  72. First define the calculate price method
    in your app in the main activity.
  73. See the instrructor notes for
    a code snippet that you can start with.
  74. We're going to modify
    the calculatePrice method so
  75. that it only has one input parameter.
  76. Then in the submitOrder method
    call calculatePrice and
  77. pass in quantity as the input.
  78. Then try to modify the calculatePrice
    method to have two input parameters.
  79. That way in the submitOrder method you
    will call calculatePrice quantity and
  80. then you can pass in the price
    which could be $10 for example.
  81. And lastly, try changing calculatePrice
    so that it has zero input parameters.
  82. That way in the submitOrder method
    you would just call calculatePrice
  83. like this.
  84. Let's do the first one together so
    you understand what I mean.
  85. I'm going to open up the link
    in the instructor notes.
  86. In this gist we provided code for
    the calculatePrice method.
  87. So go ahead and
    select everything and then copy it.
  88. In Android Studio,
    I'm going to paste in this method.
  89. I'm just going to paste it in
    right below submitOrder, and
  90. I'll make sure that there's a blank
    line in between each method here.
  91. That makes it easier to read.
  92. Then I can call this method
    from the submitOrder method.
  93. I'm just going to insert the call
    at the very bottom of the method.
  94. Later we're going to integrate
    it with the code up here.
  95. But for now, this is good.
  96. Oops, I forgot that we have one input
    parameter, so I need a pass quantity.
  97. Now do the same for
  98. the remaining two tasks by modifying
    the input parameters here.
  99. To complete this task,
    if you need examples to reference,
  100. you can always do a Google search for
    Java methods.