-
Title:
Use Return Value from Method - Solution
-
Description:
-
First we're going to modify
the calculatePrice method signature
-
to have the correct return data type.
-
We're also going to assume that
there's 0 input parameters.
-
This is where the calculatePrice
method is defined.
-
The purpose of this method is to
calculate the total price of the order
-
and then return that.
-
The price should be an integer so
-
we should change the return
data type from void to int.
-
That takes care of the first task.
-
The quiz also said to make sure that
there's no input parameters, and
-
that's what we have here.
-
Now the second task is to
modify the inside of the method
-
to make sure that we're computing
the correct price for the order.
-
And assuming $5.00 for a cup of coffee.
-
Inside this method we're taking
the quantity of cups ordered.
-
And we're multiplying it
by $5 per cup of coffee.
-
This equals the total order price.
-
So this line of code is cracked.
-
But there's two issues that I see,
the variable price is never used and
-
there's an error that says
missing return statement.
-
It's expecting us to return an integer,
but we're not returning anything.
-
Right now our code is the same
as if we just said this.
-
We need a line that says return and
then actually put a value here and
-
the value on return is a price.
-
So, I'm just going to put price,
which is a variable we defined up here.
-
Cool, so now all the errors go away.
-
We should also modify the Java DOC.
-
So that we specify that we're
turning the total price.
-
That way when someone's calling this
method, they know what to expect.
-
Okay, so now we finished all
the parts for defining this method.
-
Now, let's move on to where
the method is getting called.
-
Within the submitOrder method, we
should call the calculatePrice method.
-
Currently calculate price,
is floating in the middle of nowhere.
-
I'm going to copy it, and
I'm going to paste it up here.
-
Because before we would calculate
the price of the order in line,
-
in the submit order method, but
now we have a method for it.
-
So I'm going to paste this here.
-
Now get rid of the extra semicolon.
-
You'll notice that price is a local
variable in this method, and
-
also in this method.
-
It's okay if they have the same name,
-
because these are different
variable scopes.
-
So the price variable here is totally
unrelated with the variable here.
-
They're updated and
created independently from each other.
-
And once this method finishes executing,
this price variable is gone.
-
Okay back to our submitOrder method.
-
So we calculated a price and
-
stored it in a variable, and then we
can use it to create the price message.
-
So, actually,
all of this code can stay the same.
-
We're concatenating the integer,
-
price, to the string total
with the currency symbol.
-
Then we concatenate "Thank you!" and
then just display it on the screen.
-
So I'm going to erase these extra,
blank lines, and save it, and
-
then we can run it on our device.
-
Okay, so I changed the quantity, and
-
hit ORDER, then it correctly
shows $25 as the price.
-
Awesome, it worked.
-
But how can I be sure that
calculatePrice is actually
-
working because there's no user visible
change compared to our app from before.
-
Here are a couple of techniques that
you can do to verify that your code is
-
actually running.
-
I could have it return a really
crazy number, like 700.
-
Save my code and then run it.
-
Now what I expect to see is
if I change the quantity and
-
hit the ORDER button,
it should saw that the price is $700.
-
That way I'll know that this calculate
price method is actually getting called.
-
And it does, cool.
-
Now I can revert this change.
-
Another way to check that this
method is getting executed
-
is to add a break point here.
-
So I'm going to click on
this little bug icon and
-
then our apple enter debug mode.
-
If I change the quantity and
-
I hit order, then I expect that
the app will pause at this point.
-
And it does.
-
So now we're in
the calculate price method.
-
There is a global variable, quantity.
-
And if I step over to the next line then
I see that there's now a local variable,
-
price And it's set to be $15 because
quantity is 3, 3 times 5 is 15, cool.
-
So, it's going through
the calculated price method, and
-
if I hit Resume,
then it updates properly on this screen.