Java Reference
In-Depth Information
9. The output would be the following (the main change from Display 8.4 is shown
in blue):
This is the Sale class.
This is the DiscountSale class.
No surprises so far, but wait.
discount2 is a DiscountSale object in a Sale variable.
Which definition of announcement() will it use?
This is the
DiscountSale
class.
Did it use the Sale version of announcement()?
10. Yes. See Display 8.7 .
11. Yes, you can have a variable whose type is an abstract class.
12. Yes, you can have a parameter whose type is an abstract class.
13. Yes, it is legal to have an abstract class in which all methods are abstract.
14. Yes, it would be legal to replace the version of
equals
for the abstract class
Employee
with this improved version. In fact, the version of
Employee
on the
accompanying website does use the improved version of
equals
.
15. No, you can still use constructors to hold code that might be useful in derived
classes. The constructors in the derived classes can—in fact, must—include invoca-
tions of constructors in the base (abstract) class. (Recall the use of
super
as a name
for the base class constructor.)
Programming Projects
Visit
www.myprogramminglab.com
to complete select exercises online and get instant
feedback.
1. In Programming Project 7.3 from Chapter 7 , the
Alien
class was rewritten to use
inheritance. The rewritten
Alien
class should be made
abstract
because there
will never be a need to create an instance of it, only its derived classes. Change this
to an abstract class and also make the
getDamage
method an abstract method. Test
the class from your
main
method to ensure that it still operates as expected.
2. Create a class named
Movie
that can be used with your video rental business.
The
Movie
class should track the Motion Picture Association of America (MPAA)
rating (e.g., Rated G, PG-13, R), ID Number, and movie title with appropri-
ate accessor and mutator methods. Also create an
equals()
method that over-
rides
Object
's
equals()
method, where two movies are equal if their ID number
is identical. Next, create three additional classes named
Action
,
Comedy
, and
Drama
that are derived from
Movie
. Finally, create an overridden method named
calcLateFees
that takes as input the number of days a movie is late and returns
the late fee for that movie. The default late fee is $2/day. Action movies have a late
fee of $3/day, comedies are $2.50/day, and dramas are $2/day. Test your classes
from a
main
method.
3. Extend the previous problem with a
Rental
class. This class should store a
Movie
that is rented, an integer representing the ID of the customer that rented the
movie, and an integer indicating how many days late the movie is. Add a method
VideoNote
Solution to
Programming
Project 8.1