-
Title:
Add or Remove Parameters
-
Description:
-
I'm going to walk you through
an example of a method with a different
-
number of input parameters.
-
I started by creating a new app from
the project wizard in Android Studio, so
-
this is completely unrelated
to the Just Java app.
-
I added this method
in the main activity.
-
It's called createWeatherMessage.
-
There's no input parameters, and
the return value is a string.
-
This method is supposed to create a new
message that specifies the city name and
-
the temperature.
-
You can think of a method as
being a template of instructions.
-
So I can make temperature
be an input parameter.
-
And it's a number, so
I'm going to say, int temperature.
-
And then I can use this right here.
-
So, this is a string literal, and
-
then it's concatenated with
the temperature integer.
-
And then it's concatenated with another
string that says degrees Fahrenheit.
-
So now the message can be customized
based on the temperature because it's
-
an input parameter.
-
Oh yeah, I forgot to mention this
earlier, but in the onCreate method of
-
the main activity, which is called
when the activity gets created,
-
I can call the createWeatherMessage.
-
Before I passed in no arguments, but
now we have one required parameter.
-
So I'm going to pass in 77 for
the temperature.
-
Now that makes the error go away,
and this method call is correct.
-
The other thing I should modify
is this comment on the method.
-
I'm going to add a blank line, and
-
then I'll start to describe the input
parameter which is temperature here.
-
I'm going to type @param temperature,
and
-
then I can add a description saying that
this is the temperature of the city.
-
Sometimes the parameter names
are a little hard to understand, so
-
that's why the description is useful.
-
Other times if your method is public
then other people will be able
-
to call this method, and
-
they might not know what
are the instructions inside this method.
-
So reading this java doc and
-
the description of the parameters
is really useful to them.
-
I mentioned earlier that
a method is like a template.
-
So, what if we want to change the city
so that it's not always San Francisco?
-
I can make city be an input parameter.
-
So I can just add a comma here.
-
And since city name is text I'm
going to make it a string data type.
-
Then I can delete San Francisco and
replace it with a variable city name.
-
And remember to add a space before and
after here as well.
-
Cool, so
now this method can create a message
-
that's customizable to
a certain city and temperature.
-
I also remembered to
modify the java doc so
-
that it has this extra parameter and
description for a city name.
-
Since I changed the method signature,
now I have an error up here.
-
The method no longer works
with just one argument.
-
I need to pass in two arguments.
-
So I add a comma and
then pass in San Francisco string.
-
Now the error goes away.
-
So when I call createWeatherMessage(77,
it's going to be value of temperature,
-
and San Francisco will be
the value of city name.
-
And it will create
the proper string here.
-
Now we're going to go back to
the calculate price method,
-
and we're going to modify
the method signature so
-
that it has a different
number of input parameters.
-
We're going to define it with zero
input parameters, one input parameter,
-
and lastly,
try it with two input parameters.
-
You'll see how changing the method
signature here to have a different
-
number of input parameters with affect
the way that the method is called.
-
Normally when you're building your app,
-
you will just define it one way with
a certain number of input parameters.
-
And usually that number of input
parameters is going to be determined
-
based off of what's
happening inside the method.
-
The best practice is to
only pass input parameters
-
that you're actually going
to need inside this method.
-
There's no point in passing over more
information if you're not going to
-
use it inside here.
-
Here are the instructions for this task.
-
First define the calculate price method
in your app in the main activity.
-
See the instrructor notes for
a code snippet that you can start with.
-
We're going to modify
the calculatePrice method so
-
that it only has one input parameter.
-
Then in the submitOrder method
call calculatePrice and
-
pass in quantity as the input.
-
Then try to modify the calculatePrice
method to have two input parameters.
-
That way in the submitOrder method you
will call calculatePrice quantity and
-
then you can pass in the price
which could be $10 for example.
-
And lastly, try changing calculatePrice
so that it has zero input parameters.
-
That way in the submitOrder method
you would just call calculatePrice
-
like this.
-
Let's do the first one together so
you understand what I mean.
-
I'm going to open up the link
in the instructor notes.
-
In this gist we provided code for
the calculatePrice method.
-
So go ahead and
select everything and then copy it.
-
In Android Studio,
I'm going to paste in this method.
-
I'm just going to paste it in
right below submitOrder, and
-
I'll make sure that there's a blank
line in between each method here.
-
That makes it easier to read.
-
Then I can call this method
from the submitOrder method.
-
I'm just going to insert the call
at the very bottom of the method.
-
Later we're going to integrate
it with the code up here.
-
But for now, this is good.
-
Oops, I forgot that we have one input
parameter, so I need a pass quantity.
-
Now do the same for
-
the remaining two tasks by modifying
the input parameters here.
-
To complete this task,
if you need examples to reference,
-
you can always do a Google search for
Java methods.