ROB SCHULTZ: Hey, everybody.
Welcome to AP Computer Science A, AP daily practice
video number 3.
In this video, we're going to look at some free response
questions.
My name is Rob Schultz, and I teach at Bellbrook High School
in Bellbrook, Ohio.
TIM GALLAGHER: And I'm Tim Gallagher,
and I teach at Winter Springs High
School, located just outside of beautiful Orlando, Florida.
Let's go ahead and get started.
So as I mentioned, we are doing a free response question today.
As you know, there are four types
of free response questions on the AP Computer Science A exam.
We have a method and controlled structures question,
class implementation question, an array/arraylist question,
and a 2D array.
Today, we're focusing on a class implementation question.
And in fact, we're going to be looking at the question number
two from last year, the 2022 exam called the Textbook.
And it involves writing an entire class.
And, again, you may have looked at this question before,
but along the way, Rob and I are hoping
to give you some hints and good practice
skills that you can use when writing your own question
number two in writing your entire class on the AP Computer
Science day exam yourself.
So let's look at this one.
It says a book class is used to store information about a book.
And we have a partial book class definition shown below.
So inside this class book, we have some instance variables.
We have a string, which is the title of the book.
We also have a double, representing
the price of a book.
And then we also have a constructor
for the book, where we can show that we're
passing some parameters.
We know we're going to initialize our instance
variables.
Now, we don't show the implementation.
Not really important here.
The rest of the class continues with a getTitle method that
returns the title of the book, and then a getBookinfo method,
which returns the title concatenated with a hyphen
and concatenated with the price.
So it gives us all the information
about the book in one string.
And there could be some other instance variables,
constructors, and methods that are not shown, but are not
really germane to this particular question.
So this question says that we're going
to write a class called Textbook,
which is a subclass of Book.
Now, right off the bat, that should give you a hint.
You know you're going to write a class, what type of class?
Is it a standalone?
Or is it a subclass, which means we're going to be using
extends?
This is an inheritance.
Not every question 2 is going to be inheritance,
but this one happens to be.
So we know that.
Let's read some definition about the Textbook class.
It says a Textbook has an edition number,
and that's going to be a positive integer used
to identify different versions of the book.
We know it's going to have a getBookinfo method, kind
of similar to the book class itself,
which when called on a textbook returns a string that
also includes the addition information as shown
in the examples.
We know that two other things are going
to have to be true about this.
Information about the book title and the price
must be maintained in the book class,
so we're really going to have to use true inheritance here.
And we know that information about the edition
is going to have to be maintained in the Textbook
class.
So that it also says the Textbook class
contains an additional method, canSubstituteFor.
It returns true if a Textbook is a valid substitute
for another Textbook, and returns false otherwise.
So we're going to see how that works.
The current Textbook is a valid substitute
for the Textbook referenced by the parameter
for canSubstituteFor.
If the two textbook methods have the same title,
and if the current edition of Textbook
is greater than or equal to the edition of the parameter,
the Textbook parameter that we're sending in.
So how is this going to work?
Well, again, we have a table here
with some examples that you should definitely
spend some time reading, if you ever see a chart with examples
here, because it's going to give you
some really good insight into how your class should
be implemented.
So here are some examples of using the Textbook class
that you're about to right.
Now, notice, before we even get into them,
there's things like bio2015 and bio2019.
These are not examples that you should write in the class.
These are examples of how the class is going to be used.
You're writing the class.
The blueprint for which these objects are
going to be created, but the objects that you see
created here in these examples will not appear in your code.
So be careful there.
The first one says Textbook bio2015 equals new Textbook.
And what that means is we're creating a textbook
called bio2015 with the title of biology, a price of 49.75,
and an edition number of 2.
Those are my three parameters.
Here's another example of creating a textbook bio2019.
Three different parameters.
Biology as a title, price 39.75, and the edition of 3.
So now when I call bio2019.getEdition,
what's going to happen here?
Again, this is how I use the class I'm about to write.
So keep in mind what should be going on behind the scenes
while you read these examples.
I'm going to return a value of 3,
so getEdition is an accessor method,
and the edition gets returned.
When I call getBookinfo on bio2019.
It's a textbook, so it's going to return
a string containing the title, the price,
and the edition of bio2019.
Here's where that new method canSubstituteFor it gets used.
If I say bio2019 can substitute for bio2015,
it's going to return true because bio2019 is
a valid substitute for bio2015.
Why?
Because the titles are the same.
They're both biology, and the edition of 2019
is greater than or equal to the addition of bio2015.
Bio2019 has an edition of 3, bio2015, if you remember,
has an addition of 2.
But what if I switch it?
What if I say bio2015 can substitute for bio2019?
That's going to return false because bio2015 is not
a substitute for bio2019 because the addition of bio2015
is less than the edition for bio 2019.
Finally, in this last example, I have
another completely different textbook called Math.
It's got a title of calculus.
Price 45.25, and the edition of 1.
If I try to substitute bio2015 for canSubstituteFor math?
That's going to be false.
I don't even have to worry about the addition,
the titles are completely different.
So this would not be a good substituteFor.
So based on all of those examples
and what we've talked about here,
your next job is to write the complete Textbook class.
Think about the methods that you have, the methods
you inherit, the methods you might override,
and the methods that are going to be new to this class.
So this would be a great time to hit pause,
take your best shot at writing everything you
can about this Textbook class.
And Rob, I'm going to hand it over to you
to go over the solution.
ROB SCHULTZ: OK.
Great.
Thanks, Tim.
So welcome back to those that paused.
I hope you had a chance to take some time
and actually think through what all of the different methods
will be.
Let's take a look at some actual code.
So again, here's our canonical.
And as we said in the last video,
the canonical solution isn't the only solution.
It's just a solution that seemed like it
was going to be one of the more common solutions,
but there are other possible solutions
to this that could work.
So let's check and see how you did.
Every for your response question is worth 9 total points,
and because the purpose of this question
was to write the entire class, all 9 points
are in just the code that you see on the screen.
So let's look at the first four points.
So the first thing is we have to make sure we declare a class
header because in this question, we're
writing the complete class, which means we have to write it
from the very first class header line, all the way down
to the final bracket.
So our very first point is just, hey,
did we declare the class header correctly?
We have to make sure it's not private.
We want it to be public.
And we have to make sure that when we create our class
Textbook it extends book because, again, part
of the purpose of this is to make sure that our Textbook is
a subclass of the book class.
So this kind of goes back to what
we talked about maybe a little in the last video.
Make sure you do what you know how to do
because we're writing a class.
We know that there's going to be probably
a point that's going to make sure we have the correct class
header.
So if you know how to write the class header,
make sure you get the class header in.
So that's one quick point.
The second point is do we declare a constructor.
Oh, there we go.
I forgot my highlight.
Do we do we declare a constructor header?
We have to make sure that we're taking the three
parameters that were kind of shown and demonstrated
in that table that Tim went through with us.
So we have to make sure we've got
a string parameter for the title,
a double parameter for the price,
and an int parameter for the addition.
So all three of those have to be there,
and they have to be in the correct order.
Now, I should point out it really
doesn't necessarily matter what you name your variables,
but always use meaningful variable names.
I know it's kind of tempting to use
some maybe cute variable names or something like that,
but keep your code easy to read.
That's another kind of piece of advice
that I pass on to my own students.
Don't try and put things in to be cute or be funny
as you're writing these questions because that's
when you maybe get distracted by something you're trying to do,
and you end up missing some of the key points.
So keep things straightforward.
Keep things simple.
Keep things easy to read.
So in this case, we've got our correct constructor header.
We've got public Textbook.
Remember the name of the constructor
has to match the name of the class.
We've got all of our parameters in the correct order.
So the next point is, do we actually
call super as the first line with the appropriate Parameters
And in this case, we do.
We've collected a title and a price.
And the instruction said that title and price
had to be maintained in the book class, which
means we need to pass those arguments up to the book class
by calling the super constructor from the book class,
and giving them the title on the price to keep track of.
The fourth point is do we declare
appropriate private instance variable?
And in this case, we do.
We have addition.
And then we use this third parameter, this third argument
that we've given to go into this TB addition parameter,
and we use it to set the addition for our textbook
because, remember addition is something specific
to a textbook, not a book.
So now we're taking that third parameter, our edition,
and we're storing it to the addition of instance
variable for our textbook.
So those are the first four points of the rubric.
Let's go on and let's look at the next few.
All right.
So here's 0.5.
Do we declare at least one?
We don't necessarily have to declare all,
but we have to declare at least one of the required methods,
and all of the declared headers are correct.
So we've got three methods that we said we needed.
We needed to git edition method.
We need to canSubstituteFor method,
and we need the getBookinfo method.
So, again, this kind of goes back
to the idea of doing what you know how to do.
If you're not really sure how to do all of them, at least
make sure you get one because with this one, the way
the point was written, all I have to do
is correctly write one of these headers.
And, again, I have to make sure I do it correctly.
For whatever it is I attempt to do,
I have to make sure it's correct.
But as long as I do one of them and it's correct,
then I get that point.
Now, if I do one correctly, but I kind of
make a mistake in the next one, I might not get the point.
Make sure that you're attempting all of them.
And think about what the return type, think
about what the parameters are, think about what the name is,
think about your method signatures
as you're putting your method headers together.
OK.
The sixth point is now we start looking at
do the methods do what they're supposed to do.
So our first method we're going to look at is getEdition.
When we look at the getEdition method,
really all getEdition needs to do is just return
the value of the instance variable.
So this is our accessor method that Tim mentioned
when he described the problem.
All we have to do is make sure that public int getEdition
returns the edition instance variable,
and that's worth a point.
All right.
We're in the home stretch.
We've got three more points to look at.
So let's take a look at point seven.
So now we're going to look at the canSubstituteFor method.
And we earn the point for canSubstituteFor
if canSubstituteFor determines whether true or false should
be returned based on the comparison of book
titles in edition.
So this is kind of our algorithm point.
We have to make sure our algorithm is
correct for this specific method.
If you remember, we pass in another textbook,
and then we compare it to make sure
that the titles are the same, and to make sure
that the edition of our current textbook
is greater than or equal to the edition of the other textbook
that we're comparing to.
So in this case, we've got a Boolean comparison.
other.getTitle.e quals(getTittle).
Now, we're comparing strings, so make sure you're using .equals
instead of equal equal.
And then we're also comparing notice the
and because both of these conditions have to be true.
We're also comparing our editions.
The edition that's our instance variable to make sure that
it's greater than or equal to other .edition.
So now we can check both of those.
We're going to return a Boolean value,
and this is our algorithm point.
OK.
So that's our point seven.
Point eight is to getBookinfo Method.
So getBookinfo calls super.getBookinfo.
So when you remember, you know, think back or look
at the paper in front of you, if you look back
at the getBookinfo method for the book class,
it returned to the title and a dash and the price.
Well, we want to build a little bit extra on to that.
So we're going to call super.getBookinfo
because that's where our title and our price are stored.
It's going to return a string to us, and all we're going to do
is we're going to further concatenate
an additional dash, and the edition of our book
onto that string that was returned so that we can return
that newly updated, that newly concatenated string, back
to the method that called getBookinfo.
All right.
So we're calling super.getBookinfo,
and then our final point is do we
do that construction of that string correctly?
Do we do the correct concatenation
to take the string that we received from getBookinfo, add
the extra hyphen, the extra dash, and the edition.
And notice that this time, the return is not assessed.
So if by some chance you forget to do the return on this one,
if you construct it and maybe you save it to a variable,
but you forget to put the return in, for this one,
you didn't lose a point if you forgot to return.
All that we were looking for for this one or all
that this rubric point looks for is
to make sure that the string is actually constructed correctly.
OK.
So there are the nine points for this entire Textbook class.
All right this has been free response question number two,
the class question.
And some of the key takeaways are
things like, again, as we've said,
make sure you read carefully.
Make sure you underline, I should
say, and make sure you identify the specific variables
that you need.
Take your time.
Make sure you're looking at things like extends, if we
have the word super in there.
Tim, what other words of advice do you have to throw in there?
TIM GALLAGHER: Making sure you can identify
that it is an inheritance question
versus a standalone class, and then
making sure that your code is going
to match all of the examples given, if someone
were to build an object out of the class that you just wrote.
ROB SCHULTZ: Excellent.
All right, great.
Well, hey, thank you, everybody for your time.
On behalf of Tim and I, have a great day,
and hope to see you in the next one.