English subtitles

← Calculate Price Method

Get Embed Code
13 Languages

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

  1. Earlier, we saw that we could pass
    a number into the display method.
  2. Let's talk more about the different
    inputs and output of a method.
  3. Let's look at the submitOrder method
    in the MainActivity as an example.
  4. Say you have a bunch of lines
    of code in that method.
  5. When the user clicks
    on the Order button,
  6. it will trigger this submitOrder
    method to be called.
  7. The Android device will execute each of
    these instructions from top to bottom,
  8. and when it hits the calculatePrice
    method call it will
  9. jump over to where the calculatePrice
    method is defined.
  10. Say that this is defined at
    the bottom of the MainActivity file.
  11. The Android device will execute each of
    these instructions from top to bottom of
  12. the method, and then it will jump
    back to the submitOrder method.
  13. Then, it will continue executing these
    instructions until it hits the end
  14. of the method.
  15. When the calculatePrice
    method is called,
  16. we can pass a bunch of inputs over so
  17. that when we're executing these lines
    of code, we can use these input values.
  18. As a result of this method,
  19. we can pass a single output
    value back to the caller.
  20. That way, the output value can be
    used in the subsequent lines of code.
  21. In Java, we call the inputs to
    a method input parameters, and
  22. we can have zero or
    more input parameters.
  23. In Java,
    we call the output a return value,
  24. and we can have zero or
    one return value.
  25. An example of an input parameter
    is passing the quantity
  26. over to the calculatePrice method.
  27. That way, this method can use the
    quantity to figure out the total price
  28. of the whole order.
  29. Then, we can pass that total
    price back as a return value.
  30. And then in the submitOrder method,
    down here,
  31. we can use this total price to do other
    things, like display it onscreen, or
  32. create receipt, or anything like that.
  33. So to summarize, this is where we
    define the calculatePrice method, and
  34. this is where we call it.
  35. We want to create a method to
    calculate the price of a coffee order.
  36. Currently, we calculate the price
    directly in the submitOrder method, but
  37. the logic to calculate the price is
    going to get a little more complicated.
  38. So let's move it into its own method.
  39. Then we can call it as many times
    as we want without copying and
  40. pasting the code everywhere.
  41. As you gain more experience in Android
    development, you'll gain better judgment
  42. on when to create a new method for
    something or when you don't need to.
  43. This will just come with more
    experience and more time.
  44. So, this is the first time
    we're going to be creating
  45. a new method in
    the MainActivity on our own.
  46. Instead of starting by writing it from
    scratch, we want to start by learning to
  47. recognize what is a correct method
    by reading some code snippets.
  48. So in the instructor notes, we're
    going to provide you three options.
  49. Read over these three code snippets and
    determine which
  50. option correctly implements the method
    to calculate the price of the order.
  51. When you click on the link
    in the instructor notes,
  52. you'll see this just has code for
    options A, B, and C.
  53. The goal here is for
  54. you to become comfortable with reading
    code that you've never seen before.
  55. We haven't gone over the exact rules for
    what makes a valid method, but
  56. I want you to make your best guess on
    which option you think is most correct.
  57. I also encourage you
    to Google search for
  58. more information on how
    to define Java methods.
  59. As you click through
    the different search results,
  60. you'll come across information in the
    documentation that you don't understand.
  61. But I want you to become comfortable
    with not understanding everything on
  62. those pages, because it's perfectly
    normal to not understand everything.
  63. All you need to do is just skim
    the articles for what you understand and
  64. what you think is relevant
    to the problem at hand.
  65. So, go ahead and read the code for
  66. these three options, look up some
    information online, and then make your
  67. best guest on how to correctly
    define the calculatePrice method.