ROB SCHULT: OK, hello, everybody.
Welcome to AP daily practice video number two.
I'm Rob Schultz.
And I teach at Bellbrook High School in Bellbrook, Ohio.
TIM GALLAGHER: And I'm Tim Gallagher.
And I teach you at Winter Springs High
School located just outside of beautiful Orlando, Florida.
And today, on this video, we're going
to be doing a free response question.
All right, so you know there are four types of free response
questions that we have on the AP Computer Science A exam.
There's the method and controlled structures.
A class implementation question.
An array and ArrayList question, as well as a 2D array question.
Today, we're going to be looking at the method
and controlled structures question.
And this is a question from the 2022 exam,
the one we just took last year.
This is question number one.
This is the game level question.
I want to take a couple of minutes
to go through some of the aspects of the questions, some
of the details.
And then we'll look at implementing
parts A and part B.
So first of all, this question involves the simulation
of the play and scoring of a single player video game.
And we're going to have a few classes that we have.
And it says, in the game, a player attempts
to complete three levels.
So first of all, we have the level
of the game represented by this level class.
Now, you'll notice, this public class
level that we have has a number of methods in there.
The first one is a goal reached method.
It returns a Boolean.
Says it returns true, if the player reached
the goal in this level.
Returns false otherwise.
The implementation is important.
So it's not being shown.
We also have a get points method.
And this is a method that returns the number of points.
It's a positive integer recorded for this level.
So those are the two important methods
that we're going to be using on our level objects
that we find in this question.
And there may be other instance variables and constructors that
are not shown, but they're not important
to this particular question.
So that's the level class.
The game class is represented here.
And we're going to write two methods of the game class.
But before we get to that, let's look at the game class itself.
You know you're going to be writing
two methods of this class.
So whenever I do a free response question,
I always recommend to my students
the first thing you should do, find
the class that you're going to be writing the methods for.
And what instance variables do you have?
In this case, we've got three instance variables.
Level one, level two, level three.
Students make mistakes of not using correct instance
variables a lot.
So I always tell my students.
Circle them, put a big old star next to them.
Make sure that you have the correct instance variable
names.
In this case, level one, level two, level three.
We also have some methods that we're going
to be using in this class.
We have the constructor.
That's not shown.
But we know that the instance variables have all
been initialized.
So we're not going to have to worry about that when we
use these instance variables.
And there's a few other methods here.
We have an is bonus method.
That's a Boolean method.
Returns true if it's a bonus game and false otherwise.
And then we have a play method.
Doesn't return anything, but it simulates
the play of a game and updates all of our relevant data.
So these are some other methods that we're
going to be using along with the methods of the level class
in implementing the methods that we're going
to be doing here in a moment.
So continuing with the game class,
here are the two methods that we're going to use
or that we're going to implement.
First of all, we have this get score method.
This is what we're going to implement in part A.
It returns the score earned by the most recently played game
as described.
We'll talk about that in a moment.
And then there's this.
That's part A. And then for the second part,
we have this play many times method.
And that's what we're going to implement in part B.
So let's get started with part A, the get score method.
So part A says, if you've downloaded the document
or if you are-- and you have the whole question printed out,
we're going to write the get score method.
It returns the score for the most recently played game.
And each game consists of three levels.
Now, we know we already have those three instance variables
we're going to be using.
The score for the game is computed using
the following helper methods.
We're going to use the is bonus method.
That returns true if it's a bonus game and false otherwise.
And then we're going to use the two level methods as well.
We're going to use go reached, which returns true
if the goal has been reached.
We're also going to use the get points method of the level
class.
And that returns the number of points recorded
for that particular level.
So how do we compute the score?
Well, it says we're going to use the following rules.
Level one points are earned only if level one goal is reached.
Level two points are only earned if you earn level--
if you reach level one and level two.
And level three is earned only if you reach all three levels.
You're going to compute the score by summing up
the points earned for whatever levels you earn points for.
And then if it happens to be a bonus game,
the score is going to be triple.
That's a lot to take in.
So let me show you a chart with some examples of what
a execution of this method might look like.
So let's say in this method that we're going to write,
let's say I find out that level one is reached.
Remember, there's a method that tells me if it's reached
or not.
Let's say, I get back true.
And then the points for that is going to be 200.
Let's say level two is reached.
And it also gives me some points.
Going to give me 100 points.
Level three is also reached true.
And it's going to give me 500 points.
And then is bonus is returning true.
So how do I calculate my score?
Well, notice here over on the side,
it's going to be the 200 plus 100 plus 500
because I reached all three levels.
And then times 3 because it is a bonus level.
So our bonus game.
So it's going to be a total of 2,400
because I reached all three levels
and I get all three points plus the bonus.
Another example might be, well, what
if I reached levels one and two, but I don't reach levels three?
And it's not a bonus.
So in this case, I'm only going to get points for level one
and level two, 200 and 100.
And my score is going to be 300 because it's not a bonus
and I don't get any points for level three.
Even though level three says 500, I don't get those points.
Another example might be, what if I only reach level one?
Level one returns true.
I get 200 points.
Level two is false, I'm done.
Now, notice, it says level three true.
But that doesn't matter because once I hit false for level two,
my play was over.
So I only get 200 points, but it's a bonus.
So I'm going to get times 3.
So I actually get 600 points for this game.
And then finally, the last score calculation.
What if I immediately get false for level one, That's it,
I don't earn any points.
I can only earn level one, if I reach level one.
And I can't earn any other points for any other levels
because I haven't reached level one.
So what we're supposed to do now is,
can we write the code for this score method,
knowing what instance variables we have,
knowing what methods we can call on those,
and knowing the algorithm that we
have to execute to calculate the score in this game class?
So this would be a great time to pause.
See if you can write the method based on everything
that we just talked about.
And we'll come back in a few minutes and see how you did.
OK, ready?
All right, here we go.
For part A, I'm going to put up the entire answer.
Here's what we call the canonical answer.
This is not the only correct way of doing it,
but it's a very common correct way.
You may have some other things that are correct in here
as well.
Now, it looks like a lot of code.
But we're going to go through here,
you'll notice that we have nested if statements.
If level one is reached, well, then I'm
going to get my level one score.
And then if level two is reached,
I get my level two score.
And then level three.
And afterwards, I'm going to calculate my bonus.
So having those nested if statements, having a score
variable that I initialized to 0 and I multiply by 3
if it's a bonus.
Notice, I have to return the score at the end.
This seems like a lot.
But one of the approaches I want you
to think about when you do a free response question is,
even if I don't understand every part,
can I do the parts that I do understand.
And what I want to do is look at the points
that you would receive when scoring this question.
And this is what we did last year
when we scored the 2022 exam.
You're going to get a point as a student, if you-- on part one,
if you call get points, goal reached, and is bonus.
Now remember, get points and go reached
are methods in the level class.
So you'll notice that I call level one get
points and two, goal reached, and the different goal reached
methods for level one, level two, and level three.
And then I also call the is bonus method.
But notice is bonus is called just by itself because that's
a method in the same game class, where I'm implementing
this get score method.
So if somewhere in your code, you
call a level with a get points, a level and a get reached--
a goal reached method, and then call the is bonus as well,
you're going to earn a point for point one.
Point two says, well, do you determine
if points are earned based on the goal reached return values?
This is my if statements.
Do you check your if statements?
And in your if statements, do you check the value return
from goal reached?
And based on that, do you then determine
if you're going to earn points?
The point number three says, do you guard the update of a score
for the bonus based on the return value
that you get from the is bonus method?
And then finally, if you do all those and get
the score initialized, which score is equal to 0.
And then the entire algorithm, did you get everything right?
Were they nested ifs, instead of separate ifs?
And did you only call the bonus after that
and multiply your total score by 3
and get the entire algorithm right?
Then you can earn point four as well.
So notice that these points are all separate.
So even if you don't get every aspect right,
you can still earn a lot of points
based on achieving each of these rubric points
that we have in order to get towards the total score
for your free response question.
I hope you did well.
Rob, I'm going to send it over to you for part B.
RON SCHULT: OK, great.
Hey, thanks, Tim.
So let's take a look at part B. Part B
says that we're going to write the play
many times method, which simulates
the play of num games.
So remember when Tim went through the class definition
that we were given.
Play many times has num games as a parameter.
So it has that parameter that's going
to represent the number of games that we're going to play.
And then we're going to return the highest game
score we earned.
For example, if we have play many times
with the parameter value for, with the argument for,
it means we're going to play the game four times.
And let's say we earn the scores of 75, 50, 90, and 20, OK,
then this method should return 90.
Because 90 is the highest game score
that we earned, all right.
It goes on to say that play of game
is simulated by calling the helper method play.
And it says, note that if play is called only one
time, followed by multiple consecutive calls to get score,
each call to get score will return
the score earned in that single simulated play of game.
So that's important to make note of as you're
thinking about how you're going to put this code together
for part B, OK.
So let's go through and let's actually take a look.
This would be a good point to pause again,
if you want to take a shot at writing
the code for part B for the play many times method.
So go ahead and pause.
Take a couple of seconds and see what you can flesh out
as far as the code.
And when you're ready to go through it,
press play to come back.
All right, welcome back.
How do we do?
Do you feel confident about it?
I hope so.
Let's check it out.
So here's some actual code.
Now again, this is the canonical solution.
And as Tim pointed out, this is not the only solution.
This is just a solution that was determined
to be a common solution that meets all of the marks we're
looking for, all right.
So as we look through this one, there
were five points on the rubric for this specific part
of the question, all right.
So let's run through real quick.
We know that we're going to-- when we call play many times,
we're going to pass in a value to num.
And that represents the number of simulated games
we're going to play.
So the very first point is, do we loop num times?
And in this case, yes.
We've got our for loop that's going to loop from I equal
to 0 up to, but not including, I equal num.
So as long as I is less than num,
we're going to cycle through.
The second quest-- or the second point, I should say,
do we call the play and get score methods Well, we do.
We have a call to play.
And then right below that, we have a call to get score.
Now I want to point out that it doesn't necessarily
say that we have to use those values appropriately,
as long as we're calling them appropriately to get the point.
So in this case, we call play inside our loop.
And then we call get score.
We figure out what our highest score is.
And again, get score is the method that
was created in part A. And I should also point out,
up here at the top, it says in our description
of this problem, that you must call play and get score
appropriately in order to receive full credit.
And it said, you can assume that get score works as intended,
regardless of what you wrote in part A.
So it's important to realize that because we wrote it
in part A, even if we made a couple of mistakes here
and there, we can assume it works correctly.
But we don't want to go back and rewrite get score
into this method.
We want to call it the way it's intended.
OK, the third point.
Do we compare a score to an identified max
or to another score?
Well, right here's our if statement.
We're comparing score to this value
that we've assigned as max, all right.
So that's going to represent our maximum score.
And we're going to use that within our algorithm
to determine what the maximum score is, OK.
Point four is our algorithm point.
This is the one, where we make sure our algorithm is correct
and all of the code is correct to actually identify
the true maximum score.
So as we look through, we're initializing max equal to 0.
We call the play method to simulate
playing all of the levels.
And we call get score to calculate our scores.
We store that score.
We check it against our max score.
And if score is greater than max,
if it's greater than whatever our previously found max
score was, it becomes the new max score.
So in this case, our algorithm is correct.
And then our last point.
And I'm going to be honest, this is the one
that I kind of cringe because I know
there are going to be students that
are so excited about making sure they get the algorithm right
and so focused on the algorithm, they
forget to return the answer.
So the last point is, do we return our identified
maximum score?
And that's important because it's
easy to get focused on the algorithm
and then forget to return it back.
So yes, we have to make sure we return max.
In this case, we're returning max,
which is what we've identified as our maximum score.
And that's the end of our solution.
This has been video number two, which
focused on free response question number
one, the methods and control structures question.
Some takeaways from this one.
First and foremost, as Tim pointed out before,
make sure you do what you can do.
These free response questions are not all or nothing scores.
If you know how to do certain parts,
do what you know how to do and get the points that you can.
And don't worry about the things that you're unsure of.
The other takeaways, make sure you read carefully.
Make sure you underline the important points.
Star your instance variables so that you get all those instance
variables that you need.
And the main thing is just take your time
and read questions carefully.
And make sure you don't miss any of the details.
I hope this video has been beneficial.
And we look forward to seeing you in the next one.