Advertisement
English transcript
Okay Katherine, our app is coming along, but there is one major problem with it.
>> What's the problem?
>> Well it only orders two cups of coffee.
>> What's wrong with two cups of coffee?
>> Well three is better than two, four is better than three,
sometimes I need more.
>> Okay I get it, you need more coffee.
So what I can do is just change the code, so
that instead of two it just says three.
>> Okay, so in our code here, we will need to change
the number of cups of coffee >> Here and here.
So what if I want ten cups of coffee for myself and my friends later on?
>> You have that many friends?
Oh. Well, we need something else, then.
>> Yeah, we need a better solution.
>> How about, oh, I know, we can use variables.
>> Variables?
As in something that varies?
>> Yeah, a variable doesn't have a fixed value.
You can think of a variable like a box [SOUND] like this.
>> Okay.
>> Yeah, so this is a variable.
It's a box and you can put a value inside of it.
>> It says int.
>> Yeah, int stands for integer.
>> Okay, so you can put an integer, or a number, inside of this box.
>> Exactly!
And you can even give it a name.
>> Let's call it number of cups of coffees ordered.
>> Okay.
You had to pick a long name.
Ordered Barely fits.
>> Okay. >> Okay.
>> So now we can refer to this variable using this name
in a code instead of putting the value two.
>> Okay.
>> Later we can also update it if we want.
Let's put the value inside now.
How are we going to do that?
Well, I have some Post-its.
>> Where did those come from?
>> Let's call the number of cups of coffees ordered to be 2.
>> Why only 2?
>> One for me and one for my friend, Katherine.
>> Thank you.
[LAUGH] So now we have a variable that is called numberOfCoffeesOrdered and
it holds the value 2.
>> Let's implement this in our code.
>> Before we were using a literal value to represent
the quantity of coffee cups ordered.
A literal means a fixed value, like the number 2.
Technically the 5 and this 2 are also literal values but
we're just talking about using literals for the quantity right now.
So if we say the quantity is 2 cups of coffee ordered, then
when we calculate the price, we have to do 2 times 5, since each cup is $5.
If we want to charge the customer for a paper cup because they didn't bring
their own cup and they like to waste trees, then we charge them $2 per cup.
So that's 2 cups times $2.
When we use a literal, you can see that it's not ideal because we have to
keep repeating the quantity number over and over again.
On the other hand,
we can use a variable to represent the quantity of coffee cups ordered.
So if we say the quantity is this variable called numberOfCoffees,
then when we calculate the price, we can just use that variable name,
numberOfCoffees, and multiply it by $5 a cup.
To calculate the charge for the paper cup, again we take the variable name,
numberOfCoffees, and we just multiply it by 2, for $2.
So the difference between these two is that instead of the actual literal
value, we use a variable.
And then the value of that variable
will be substituted when we actually evaluate this expression.
So when we go and calculate the price,
we just have to go check what this value is before we multiply by 5.
The one additional step that you need to do when you use a variable is,
in the beginning,
to set the variable, numberOfCoffees, to have an initial value.
In this case we set it to an initial value of 2, which is also a literal.
But this time we only use the literal in one place instead of in multiple places.
The end result is still going to be the same,
because of numberOfCoffees will map to the value 2.
So the quantity will still be 2, the price will still be 10,
and the paper cup charge will still be 4.
The real advantage to this approach is that you can
change the variable value at any time.
Say I want to change the numberOfCoffees to be 3.
I just need to change it in one place here and
then everything else is still accurate.
The quantity is still the numberOfCoffees,
which now has a value of 3.
The price is now numberOfCoffees times 5.
And the value is 3 times 5, and
the paper cup charge is still numberOfCoffees times 2.
Which is now 3 times 2.
If we were using a literal,
then we would have to change the quantity to be 3 here.
We would have to update the price calculation to be 3 times 5.
And the paper cup charge to be 3 times 2.
So when we use a variable,
we don't have to refer to the specific value each time.
We can just refer to the variable name and
that will automatically get us the latest value in that variable.
We can look more closely at what the actual code would look like for
creating and using a variable.
To create the variable, we say int numberOfCoffees = 2.
This creates a variable for us with this name numberOfCoffees and
it puts the value 2 inside that variable box.
These next two lines use the variable that we created on the first line
in order to update this screen.
We can display the number of coffees, which involves looking at that variable
and checking the value inside, which is two.
So we show 2 here.
To display the price.
We look at the value of the variable, numberOfCoffees, which has 2 inside.
So, we say 2 x 5 equals 10, and so the price is $10.
I can easily change this code, so
that the numberOfCoffees ordered is 3 instead of 2.
For the box that represents our variable, numberOfCoffees,
now we put 3 as initial value inside this variable.
These two lines of code don't have to change though,
because we are using the variable name.
When we called display numberOfCoffees.
The value in this variable is 3.
So we show the quantity 3.
To display price we look at the value for the numberOfCoffees which is 3.
And we multiply that by 5 so that gives us $15.
This is what the code change would like in Android Studio.
Currently it's using the literal value of 2 for the quantity.
Instead let's create a variable, I'll type int numberOfCoffees 2.
So now I have a variable with the value 2.
By the way, this text is grayed out and it says variable number of coffees is
never used because I never referred to it again, but I am about to use it now.
Instead of displaying the literal value 2,
I'm going to switch it to use the variable name.
It pops up as an autocomplete option, so you can just hit Enter and
it fills it in automatically for you.
Then this variable name is no longer gray because now we've used it
in at least one place.
I can do the same for displayPrice.
Instead of the using the literal value of 2, I'm going to change
it to be the variable numberOfCoffees, and I just hit Enter to auto complete.
Technically I could also create a variable for the price value of 5.
However, it's only used in one place so
I might as well just leave it as a literal value.
Okay, now let's save the code and run it on the device.
And this is what it looks like on the device.
When you hit Order, the quantity is 2, and the price is $10.00.
This is exactly the same behavior as before, but now our code is more robust.
Robust means that the code is less prone to errors in the future.
For example, if someone else is trying to change this code,
and they were trying to use the quantity in many other places,
then they would have to change the number 2 in so many different places.
Instead if we used a variable, numberOfCoffees, then we only have to
change it in one place and then everything will automatically update.
This way someone won't mistakenly miss a line of code
that needs to have the latest numberOfCoffees ordered.
They only have to change it in one single place.
Now before you go ahead and do this in Android Studio,
I want you to to first practice doing it in this quiz.
Go ahead and type out this line of code right below it.
This line of code will create a new integer variable, and
these two lines will use the integer variable.
The reason why I want you to practice this first is that you can pay
attention to every single character when you type it.
Otherwise, if you miss a character, you may get an error in Android Studio,
and it may be hard to read the error message.
So go ahead and type it exactly as you see it.
This is helping you become aware of every single character here,
whether it's a capital letter or a lower case, or if there's no spaces or
if there is a space.
You'll also start getting used to the rules of Java
in terms of creating a variable or using a variable, and as you learned before,
every Java code statement also has to end with a semicolon.
If you type this incorrectly, for example,
the variable name, that would give you an error in Java.
However, other things are more conventional and best practices.
For example adding a space before and after operators like this.
If you don't follow this spacing, it won't give you an explicit error however
it's just better programming practice to follow these code style conventions.
That way it's easier to read your code,
when you're done typing this out go ahead and continue to the next video.
>> What's the problem?
>> Well it only orders two cups of coffee.
>> What's wrong with two cups of coffee?
>> Well three is better than two, four is better than three,
sometimes I need more.
>> Okay I get it, you need more coffee.
So what I can do is just change the code, so
that instead of two it just says three.
>> Okay, so in our code here, we will need to change
the number of cups of coffee >> Here and here.
So what if I want ten cups of coffee for myself and my friends later on?
>> You have that many friends?
Oh. Well, we need something else, then.
>> Yeah, we need a better solution.
>> How about, oh, I know, we can use variables.
>> Variables?
As in something that varies?
>> Yeah, a variable doesn't have a fixed value.
You can think of a variable like a box [SOUND] like this.
>> Okay.
>> Yeah, so this is a variable.
It's a box and you can put a value inside of it.
>> It says int.
>> Yeah, int stands for integer.
>> Okay, so you can put an integer, or a number, inside of this box.
>> Exactly!
And you can even give it a name.
>> Let's call it number of cups of coffees ordered.
>> Okay.
You had to pick a long name.
Ordered Barely fits.
>> Okay. >> Okay.
>> So now we can refer to this variable using this name
in a code instead of putting the value two.
>> Okay.
>> Later we can also update it if we want.
Let's put the value inside now.
How are we going to do that?
Well, I have some Post-its.
>> Where did those come from?
>> Let's call the number of cups of coffees ordered to be 2.
>> Why only 2?
>> One for me and one for my friend, Katherine.
>> Thank you.
[LAUGH] So now we have a variable that is called numberOfCoffeesOrdered and
it holds the value 2.
>> Let's implement this in our code.
>> Before we were using a literal value to represent
the quantity of coffee cups ordered.
A literal means a fixed value, like the number 2.
Technically the 5 and this 2 are also literal values but
we're just talking about using literals for the quantity right now.
So if we say the quantity is 2 cups of coffee ordered, then
when we calculate the price, we have to do 2 times 5, since each cup is $5.
If we want to charge the customer for a paper cup because they didn't bring
their own cup and they like to waste trees, then we charge them $2 per cup.
So that's 2 cups times $2.
When we use a literal, you can see that it's not ideal because we have to
keep repeating the quantity number over and over again.
On the other hand,
we can use a variable to represent the quantity of coffee cups ordered.
So if we say the quantity is this variable called numberOfCoffees,
then when we calculate the price, we can just use that variable name,
numberOfCoffees, and multiply it by $5 a cup.
To calculate the charge for the paper cup, again we take the variable name,
numberOfCoffees, and we just multiply it by 2, for $2.
So the difference between these two is that instead of the actual literal
value, we use a variable.
And then the value of that variable
will be substituted when we actually evaluate this expression.
So when we go and calculate the price,
we just have to go check what this value is before we multiply by 5.
The one additional step that you need to do when you use a variable is,
in the beginning,
to set the variable, numberOfCoffees, to have an initial value.
In this case we set it to an initial value of 2, which is also a literal.
But this time we only use the literal in one place instead of in multiple places.
The end result is still going to be the same,
because of numberOfCoffees will map to the value 2.
So the quantity will still be 2, the price will still be 10,
and the paper cup charge will still be 4.
The real advantage to this approach is that you can
change the variable value at any time.
Say I want to change the numberOfCoffees to be 3.
I just need to change it in one place here and
then everything else is still accurate.
The quantity is still the numberOfCoffees,
which now has a value of 3.
The price is now numberOfCoffees times 5.
And the value is 3 times 5, and
the paper cup charge is still numberOfCoffees times 2.
Which is now 3 times 2.
If we were using a literal,
then we would have to change the quantity to be 3 here.
We would have to update the price calculation to be 3 times 5.
And the paper cup charge to be 3 times 2.
So when we use a variable,
we don't have to refer to the specific value each time.
We can just refer to the variable name and
that will automatically get us the latest value in that variable.
We can look more closely at what the actual code would look like for
creating and using a variable.
To create the variable, we say int numberOfCoffees = 2.
This creates a variable for us with this name numberOfCoffees and
it puts the value 2 inside that variable box.
These next two lines use the variable that we created on the first line
in order to update this screen.
We can display the number of coffees, which involves looking at that variable
and checking the value inside, which is two.
So we show 2 here.
To display the price.
We look at the value of the variable, numberOfCoffees, which has 2 inside.
So, we say 2 x 5 equals 10, and so the price is $10.
I can easily change this code, so
that the numberOfCoffees ordered is 3 instead of 2.
For the box that represents our variable, numberOfCoffees,
now we put 3 as initial value inside this variable.
These two lines of code don't have to change though,
because we are using the variable name.
When we called display numberOfCoffees.
The value in this variable is 3.
So we show the quantity 3.
To display price we look at the value for the numberOfCoffees which is 3.
And we multiply that by 5 so that gives us $15.
This is what the code change would like in Android Studio.
Currently it's using the literal value of 2 for the quantity.
Instead let's create a variable, I'll type int numberOfCoffees 2.
So now I have a variable with the value 2.
By the way, this text is grayed out and it says variable number of coffees is
never used because I never referred to it again, but I am about to use it now.
Instead of displaying the literal value 2,
I'm going to switch it to use the variable name.
It pops up as an autocomplete option, so you can just hit Enter and
it fills it in automatically for you.
Then this variable name is no longer gray because now we've used it
in at least one place.
I can do the same for displayPrice.
Instead of the using the literal value of 2, I'm going to change
it to be the variable numberOfCoffees, and I just hit Enter to auto complete.
Technically I could also create a variable for the price value of 5.
However, it's only used in one place so
I might as well just leave it as a literal value.
Okay, now let's save the code and run it on the device.
And this is what it looks like on the device.
When you hit Order, the quantity is 2, and the price is $10.00.
This is exactly the same behavior as before, but now our code is more robust.
Robust means that the code is less prone to errors in the future.
For example, if someone else is trying to change this code,
and they were trying to use the quantity in many other places,
then they would have to change the number 2 in so many different places.
Instead if we used a variable, numberOfCoffees, then we only have to
change it in one place and then everything will automatically update.
This way someone won't mistakenly miss a line of code
that needs to have the latest numberOfCoffees ordered.
They only have to change it in one single place.
Now before you go ahead and do this in Android Studio,
I want you to to first practice doing it in this quiz.
Go ahead and type out this line of code right below it.
This line of code will create a new integer variable, and
these two lines will use the integer variable.
The reason why I want you to practice this first is that you can pay
attention to every single character when you type it.
Otherwise, if you miss a character, you may get an error in Android Studio,
and it may be hard to read the error message.
So go ahead and type it exactly as you see it.
This is helping you become aware of every single character here,
whether it's a capital letter or a lower case, or if there's no spaces or
if there is a space.
You'll also start getting used to the rules of Java
in terms of creating a variable or using a variable, and as you learned before,
every Java code statement also has to end with a semicolon.
If you type this incorrectly, for example,
the variable name, that would give you an error in Java.
However, other things are more conventional and best practices.
For example adding a space before and after operators like this.
If you don't follow this spacing, it won't give you an explicit error however
it's just better programming practice to follow these code style conventions.
That way it's easier to read your code,
when you're done typing this out go ahead and continue to the next video.