-
Title:
Calculate Price Method
-
Description:
-
Earlier, we saw that we could pass
a number into the display method.
-
Let's talk more about the different
inputs and output of a method.
-
Let's look at the submitOrder method
in the MainActivity as an example.
-
Say you have a bunch of lines
of code in that method.
-
When the user clicks
on the Order button,
-
it will trigger this submitOrder
method to be called.
-
The Android device will execute each of
these instructions from top to bottom,
-
and when it hits the calculatePrice
method call it will
-
jump over to where the calculatePrice
method is defined.
-
Say that this is defined at
the bottom of the MainActivity file.
-
The Android device will execute each of
these instructions from top to bottom of
-
the method, and then it will jump
back to the submitOrder method.
-
Then, it will continue executing these
instructions until it hits the end
-
of the method.
-
When the calculatePrice
method is called,
-
we can pass a bunch of inputs over so
-
that when we're executing these lines
of code, we can use these input values.
-
As a result of this method,
-
we can pass a single output
value back to the caller.
-
That way, the output value can be
used in the subsequent lines of code.
-
In Java, we call the inputs to
a method input parameters, and
-
we can have zero or
more input parameters.
-
In Java,
we call the output a return value,
-
and we can have zero or
one return value.
-
An example of an input parameter
is passing the quantity
-
over to the calculatePrice method.
-
That way, this method can use the
quantity to figure out the total price
-
of the whole order.
-
Then, we can pass that total
price back as a return value.
-
And then in the submitOrder method,
down here,
-
we can use this total price to do other
things, like display it onscreen, or
-
create receipt, or anything like that.
-
So to summarize, this is where we
define the calculatePrice method, and
-
this is where we call it.
-
We want to create a method to
calculate the price of a coffee order.
-
Currently, we calculate the price
directly in the submitOrder method, but
-
the logic to calculate the price is
going to get a little more complicated.
-
So let's move it into its own method.
-
Then we can call it as many times
as we want without copying and
-
pasting the code everywhere.
-
As you gain more experience in Android
development, you'll gain better judgment
-
on when to create a new method for
something or when you don't need to.
-
This will just come with more
experience and more time.
-
So, this is the first time
we're going to be creating
-
a new method in
the MainActivity on our own.
-
Instead of starting by writing it from
scratch, we want to start by learning to
-
recognize what is a correct method
by reading some code snippets.
-
So in the instructor notes, we're
going to provide you three options.
-
Read over these three code snippets and
determine which
-
option correctly implements the method
to calculate the price of the order.
-
When you click on the link
in the instructor notes,
-
you'll see this just has code for
options A, B, and C.
-
The goal here is for
-
you to become comfortable with reading
code that you've never seen before.
-
We haven't gone over the exact rules for
what makes a valid method, but
-
I want you to make your best guess on
which option you think is most correct.
-
I also encourage you
to Google search for
-
more information on how
to define Java methods.
-
As you click through
the different search results,
-
you'll come across information in the
documentation that you don't understand.
-
But I want you to become comfortable
with not understanding everything on
-
those pages, because it's perfectly
normal to not understand everything.
-
All you need to do is just skim
the articles for what you understand and
-
what you think is relevant
to the problem at hand.
-
So, go ahead and read the code for
-
these three options, look up some
information online, and then make your
-
best guest on how to correctly
define the calculatePrice method.