Advertisement
English transcript
Okay Katherine.
>> Mm-hm. >> So
when we hit the plus button in that app.
The number of cups of coffee goes up.
>> Yeah it goes from two cups to three cups.
And what happens when we press the plus button again?
>> Well it actually just stays at three.
Yeah.
So no matter how many times we press the plus button in that app,
the number of cups of coffee stays at three.
>> Yeah. >> Why is that happening?
>> Well, I think it's because, in our code, we only update it to three.
And we never go beyond that.
>> Right. That number is hard coded right there.
>> Yeah. >> You know what this remind me of?
>> What is it?
>> Well, [LAUGH] my computer science professor in college used to say.
>> Yeah?
When you submit your final project.
>> Yeah?
>> Please, no magic numbers.
>> Wait, what's a magic number?
>> Well, three's kind of a magic number.
>> Okay. >> It's a magic number because,
we are doing the addition in our head, but then to the computer.
>> We're telling it the right answer.
>> Okay, yeah, it would be better if the computer actually calculated itself.
>> Wait.
When you talk about calculation in math.
>> Uh-huh. >> I need to start writing things down.
>> Okay.
Let's write some pseudocode.
Let's write the pseudocode for this.
Assume that we've already created quantity variable and set it to two.
When the plus button in the app is pressed,
we're going to take the current quantity value which is two.
>> Add one to it to make it three, and
then store that as the new value inside the quantity variable.
If we hit the plus button again, then we're going to take the current
quantity value which is now three, and add one to it which makes it four.
And then store that as the new value inside the variable,
and here's the actual Java code for it.
We initialize a variable first.
If the plus button is pressed again,
then we're going to take the existing quantity value, add a one to it,
and then store this as the new value for the quantity variable.
When you read these lines of code, you can imagine the boxes in your head.
Even though we're used to reading things from left to right,
I always start with what's on the right hand side of the equal sign first.
In this case, we have the current value of quantity.
Which is two, and then we add one to it, and that makes three.
Then we store the three inside the variable, like this.
The righthand side is an expression, which we learned about earlier.
And an expression can be made up variables, operators and
literal values, and more.
If we click on the plus button again, then we can say quantity = quantity + 1.
At this point the current value of quantity is three,
then we add one to it, and that makes four.
Then we store four as the new value inside the quantity variable like this.
Essentially we've have reduced it down to a common pattern,
that the computer can repeat each time the plus button is pressed.
Instead of having to write a new line of code that says the explicit value of
the number of cups of coffee, we can just do quantity = quantity + 1,
and continue to build off of the existing value of quantity.
This is a lot more flexible.
Say you You wanted to change initial quantity to 3,
then the rest of the lines of code wouldn't have to change at all.
Because we would always use the latest value of quantity, whatever it is,
which, in our old approach, if we wanted to update the quantity value to 3,
then after you hit the plus button, you would have to write a new line of code
that says quantity equals 4 and update this to be quantity equals 5.
When we update the quantity to a completely new literal value each time,
it's hard for the computer to know what the pattern is between them.
Instead if we update quantity based on the current value of that variable and
we increment it in a predictable way, then the computer will be able to do
this every single time that the plus button is pressed.
This approach will work a lot better for our coffee ordering app.
Each time we hit the plus button,
we can just increase the existing quantity value by 1.
Practice writing out each line of code by typing them into the boxes we've
provided here.
Pay attention to each character and symbol.
This is the first time you're updating a variable, using the variable itself, so
make sure you type out this variable name correctly.
When you're done, fill out this column that represents the current quantity
value after each line of code is executed.
>> Mm-hm. >> So
when we hit the plus button in that app.
The number of cups of coffee goes up.
>> Yeah it goes from two cups to three cups.
And what happens when we press the plus button again?
>> Well it actually just stays at three.
Yeah.
So no matter how many times we press the plus button in that app,
the number of cups of coffee stays at three.
>> Yeah. >> Why is that happening?
>> Well, I think it's because, in our code, we only update it to three.
And we never go beyond that.
>> Right. That number is hard coded right there.
>> Yeah. >> You know what this remind me of?
>> What is it?
>> Well, [LAUGH] my computer science professor in college used to say.
>> Yeah?
When you submit your final project.
>> Yeah?
>> Please, no magic numbers.
>> Wait, what's a magic number?
>> Well, three's kind of a magic number.
>> Okay. >> It's a magic number because,
we are doing the addition in our head, but then to the computer.
>> We're telling it the right answer.
>> Okay, yeah, it would be better if the computer actually calculated itself.
>> Wait.
When you talk about calculation in math.
>> Uh-huh. >> I need to start writing things down.
>> Okay.
Let's write some pseudocode.
Let's write the pseudocode for this.
Assume that we've already created quantity variable and set it to two.
When the plus button in the app is pressed,
we're going to take the current quantity value which is two.
>> Add one to it to make it three, and
then store that as the new value inside the quantity variable.
If we hit the plus button again, then we're going to take the current
quantity value which is now three, and add one to it which makes it four.
And then store that as the new value inside the variable,
and here's the actual Java code for it.
We initialize a variable first.
If the plus button is pressed again,
then we're going to take the existing quantity value, add a one to it,
and then store this as the new value for the quantity variable.
When you read these lines of code, you can imagine the boxes in your head.
Even though we're used to reading things from left to right,
I always start with what's on the right hand side of the equal sign first.
In this case, we have the current value of quantity.
Which is two, and then we add one to it, and that makes three.
Then we store the three inside the variable, like this.
The righthand side is an expression, which we learned about earlier.
And an expression can be made up variables, operators and
literal values, and more.
If we click on the plus button again, then we can say quantity = quantity + 1.
At this point the current value of quantity is three,
then we add one to it, and that makes four.
Then we store four as the new value inside the quantity variable like this.
Essentially we've have reduced it down to a common pattern,
that the computer can repeat each time the plus button is pressed.
Instead of having to write a new line of code that says the explicit value of
the number of cups of coffee, we can just do quantity = quantity + 1,
and continue to build off of the existing value of quantity.
This is a lot more flexible.
Say you You wanted to change initial quantity to 3,
then the rest of the lines of code wouldn't have to change at all.
Because we would always use the latest value of quantity, whatever it is,
which, in our old approach, if we wanted to update the quantity value to 3,
then after you hit the plus button, you would have to write a new line of code
that says quantity equals 4 and update this to be quantity equals 5.
When we update the quantity to a completely new literal value each time,
it's hard for the computer to know what the pattern is between them.
Instead if we update quantity based on the current value of that variable and
we increment it in a predictable way, then the computer will be able to do
this every single time that the plus button is pressed.
This approach will work a lot better for our coffee ordering app.
Each time we hit the plus button,
we can just increase the existing quantity value by 1.
Practice writing out each line of code by typing them into the boxes we've
provided here.
Pay attention to each character and symbol.
This is the first time you're updating a variable, using the variable itself, so
make sure you type out this variable name correctly.
When you're done, fill out this column that represents the current quantity
value after each line of code is executed.