Return to Video

Inputs to a Method

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

12-08 Inputs to a Method

more » « less
Video Language:
English
Team:
Udacity
Projekt:
UD837 - Android for Beginners
Duration:
04:27

English subtitles

Felülvizsgálatok Compare revisions