Advertisement
English transcript
In the next set of quizzes,
I am going to be showing you how variables are actually used.
All the programs that you are about to do in part 2, will compile.
Which means that the computer can understand the code that's been written
for them, and we'll be able to create an app.
Now, while the programs will be able to run on the phone,
there's a chance that they might not do what the intended goal was.
This is something known as a logical error.
A logical error means that there's something wrong with the logic which you
used to write the code.
Let's look at a short program and
an example scenario and I'll walk you through how you solve one of these.
Okay, so here's an example.
Each of these programs will have a goal, which will be written out here.
The program itself will be in the green box.
Okay, so the goal of this program is to calculate something called sleep debt.
Now, how much sleep you need varies a little bit by age, but on average,
people need about eight hours of sleep a night.
So what this program is trying to do is calculate the sleep debt in one week.
The user of the program will go ahead and estimate how much they sleep on
an average weekday, and how much they sleep on an average weekend day.
For this example, we're assuming the user sleeps five hours on a weekday and
nine hours on a weekend.
The code for this task is here in the green box.
And the first thing that you're trying to do is read through this code and
decide whether or not the goal was accomplished.
Then what you're also going to do is go through each of the statements that
happened in the code and decide what was actually displayed to the screen.
Assume that these method calls here, the things that look like display and
have parentheses and
something in between them, will display content to the screen.
Now, some of these problems will have multiple pieces of content that they
need to display, so you might see display1, display2, display3.
Okay, so if you're still watching this video,
you're probably wondering how to solve one of these problems.
This isn't the first time you're interacting with multiple variables,
the values of which might change a few times during the problem.
So let me do the first one with you before I set you loose.
Now I'm going to use a technique called hand simulation and what this technique
involves is trying to figure out what the computer is doing, but
without just sticking it to the computer.
For us really it's just an organized way to keep track of what's going on with
the variables.
Okay, now you're going to go line by line so let me start this with you.
In the first line here I see a declaration of a variable, so
I'm going to go ahead and write the variable name which is weekday.
It's also initialized to a value which is 5,
so under weekday I'm going to put the value 5.
This is representing that the variable weekday currently stores 5.
Okay, so that was the first line, I'm going to move to the next line.
We have a variable called weekend, so I'm going to write that out,
and the value of that variable is 9.
Okay, so that's looking good so
I'm going to move on to the next line which is a declaration and
instantiation of the variable optimalHours, so I'll do the same thing.
Okay, so I'm not going to write the value 7*8.
I'm actually going to calculate that out because that's
what gets stored in optimalHours.
So, with a little bit of mental math, 7 times 8 is 56.
Okay, we have another declaration here.
We make a variable called actualHours, so I'll write that out.
Well, it's equal to the value of weekday, and
this is the current value of weekday.
It's not some past value of weekday or future value of weekday.
Now, we're keeping track of what the current value of weekday is over here.
And so I could just look here and see that it's 5.
So I should really think about this as the number 5.
So it's saying int actualHours is equal to 5, so
I'm going to go ahead and write that there.
OK we'll move on to the next line.
So now we're changing actual hours again to be equal to the current value
of actualHours plus weekend times 2.
So I can look over here, the current value of actualHours is 5 and
the current value of weekend is 9.
And now I have this little equation here.
So, I do the multiplication before the addition, so,
9 times 2 is 18 and then I add 5.
And with a little bit more mental math, 18 plus 5 is 23.
Okay, so this all equals 23.
And what I have to do is cross out the old value of actual hours, and
write in the new value of actual hours.
Okay, so that's done.
Let's move on to the next line.
We have n solution, so that's a variable declaration.
And it's being set equal to something, but
I'm going to write out the word solution first.
And solution is equal to optimal hours minus actual hours.
So let's go over here, optimal hours is 56 and actual hours is 23.
Now it's okay to use your calculator,
I happen to do that earlier and I know that 56 minus 23 is 33.
Okay, and then we move on to the next and
final line which says display solution.
So, we can look over here to see what the value of solution is, which is 33.
So it is going to display the number 33.
So that is how I, or you, or
anyone, could figure out what this program just did.
But the real question is, did it do the right thing?
Is there a logical error somewhere in here or
is somebody who sleeps five hours on a weekday and nine hours on a weekend
night going to have thirty three hours of sleep debt at the end of the week.
And I want to know what you think.
All right, quick side note, now you've created this practice set 2 app, so
I'm just going to show you how you could also put the code into the practice
set 2 app if you wanted to test it out.
I really recommend hand simulating first,
because it's going to give your brain a better grasp about what's going on.
But it's also go to be able to put the code into Android Studio and
play around with it a bit.
So my suggestion is that you copy and paste the code for the problem,
which will be provided in the instructor notes, into the onCreate method.
That's the first method you'll see in your main activity class here.
Okay, just to be very clear, here's what your onCreate looks like
before it's modified and then you can paste over the code, like I've done.
Okay, the other thing you're going to need to copy and paste over,
which will make this line of code work, is down here.
It is this code, and I've also included this in the instructor notes.
The one final thing that you'll need to do is go to your XML, and
make sure your that your text view has an Id/display_text_view,
and this should exactly match what's shown here.
Ok, as long as those two things match when you run this
code, you'll get the value printed out to the screen.
It's a little bit hard to see but it says 33 right there.
Okay, and with all of that, then I'll try to hand simulate and
some help with getting it up and running on your phone.
Go ahead and try to figure out is this program doing what it's supposed to do.
I am going to be showing you how variables are actually used.
All the programs that you are about to do in part 2, will compile.
Which means that the computer can understand the code that's been written
for them, and we'll be able to create an app.
Now, while the programs will be able to run on the phone,
there's a chance that they might not do what the intended goal was.
This is something known as a logical error.
A logical error means that there's something wrong with the logic which you
used to write the code.
Let's look at a short program and
an example scenario and I'll walk you through how you solve one of these.
Okay, so here's an example.
Each of these programs will have a goal, which will be written out here.
The program itself will be in the green box.
Okay, so the goal of this program is to calculate something called sleep debt.
Now, how much sleep you need varies a little bit by age, but on average,
people need about eight hours of sleep a night.
So what this program is trying to do is calculate the sleep debt in one week.
The user of the program will go ahead and estimate how much they sleep on
an average weekday, and how much they sleep on an average weekend day.
For this example, we're assuming the user sleeps five hours on a weekday and
nine hours on a weekend.
The code for this task is here in the green box.
And the first thing that you're trying to do is read through this code and
decide whether or not the goal was accomplished.
Then what you're also going to do is go through each of the statements that
happened in the code and decide what was actually displayed to the screen.
Assume that these method calls here, the things that look like display and
have parentheses and
something in between them, will display content to the screen.
Now, some of these problems will have multiple pieces of content that they
need to display, so you might see display1, display2, display3.
Okay, so if you're still watching this video,
you're probably wondering how to solve one of these problems.
This isn't the first time you're interacting with multiple variables,
the values of which might change a few times during the problem.
So let me do the first one with you before I set you loose.
Now I'm going to use a technique called hand simulation and what this technique
involves is trying to figure out what the computer is doing, but
without just sticking it to the computer.
For us really it's just an organized way to keep track of what's going on with
the variables.
Okay, now you're going to go line by line so let me start this with you.
In the first line here I see a declaration of a variable, so
I'm going to go ahead and write the variable name which is weekday.
It's also initialized to a value which is 5,
so under weekday I'm going to put the value 5.
This is representing that the variable weekday currently stores 5.
Okay, so that was the first line, I'm going to move to the next line.
We have a variable called weekend, so I'm going to write that out,
and the value of that variable is 9.
Okay, so that's looking good so
I'm going to move on to the next line which is a declaration and
instantiation of the variable optimalHours, so I'll do the same thing.
Okay, so I'm not going to write the value 7*8.
I'm actually going to calculate that out because that's
what gets stored in optimalHours.
So, with a little bit of mental math, 7 times 8 is 56.
Okay, we have another declaration here.
We make a variable called actualHours, so I'll write that out.
Well, it's equal to the value of weekday, and
this is the current value of weekday.
It's not some past value of weekday or future value of weekday.
Now, we're keeping track of what the current value of weekday is over here.
And so I could just look here and see that it's 5.
So I should really think about this as the number 5.
So it's saying int actualHours is equal to 5, so
I'm going to go ahead and write that there.
OK we'll move on to the next line.
So now we're changing actual hours again to be equal to the current value
of actualHours plus weekend times 2.
So I can look over here, the current value of actualHours is 5 and
the current value of weekend is 9.
And now I have this little equation here.
So, I do the multiplication before the addition, so,
9 times 2 is 18 and then I add 5.
And with a little bit more mental math, 18 plus 5 is 23.
Okay, so this all equals 23.
And what I have to do is cross out the old value of actual hours, and
write in the new value of actual hours.
Okay, so that's done.
Let's move on to the next line.
We have n solution, so that's a variable declaration.
And it's being set equal to something, but
I'm going to write out the word solution first.
And solution is equal to optimal hours minus actual hours.
So let's go over here, optimal hours is 56 and actual hours is 23.
Now it's okay to use your calculator,
I happen to do that earlier and I know that 56 minus 23 is 33.
Okay, and then we move on to the next and
final line which says display solution.
So, we can look over here to see what the value of solution is, which is 33.
So it is going to display the number 33.
So that is how I, or you, or
anyone, could figure out what this program just did.
But the real question is, did it do the right thing?
Is there a logical error somewhere in here or
is somebody who sleeps five hours on a weekday and nine hours on a weekend
night going to have thirty three hours of sleep debt at the end of the week.
And I want to know what you think.
All right, quick side note, now you've created this practice set 2 app, so
I'm just going to show you how you could also put the code into the practice
set 2 app if you wanted to test it out.
I really recommend hand simulating first,
because it's going to give your brain a better grasp about what's going on.
But it's also go to be able to put the code into Android Studio and
play around with it a bit.
So my suggestion is that you copy and paste the code for the problem,
which will be provided in the instructor notes, into the onCreate method.
That's the first method you'll see in your main activity class here.
Okay, just to be very clear, here's what your onCreate looks like
before it's modified and then you can paste over the code, like I've done.
Okay, the other thing you're going to need to copy and paste over,
which will make this line of code work, is down here.
It is this code, and I've also included this in the instructor notes.
The one final thing that you'll need to do is go to your XML, and
make sure your that your text view has an Id/display_text_view,
and this should exactly match what's shown here.
Ok, as long as those two things match when you run this
code, you'll get the value printed out to the screen.
It's a little bit hard to see but it says 33 right there.
Okay, and with all of that, then I'll try to hand simulate and
some help with getting it up and running on your phone.
Go ahead and try to figure out is this program doing what it's supposed to do.