Java Reference
In-Depth Information
22.
Modify the
Point
class from Chapter 8 so that it implements the
Colored
interface and
Point
s have colors. (You
may wish to create a
ColoredPoint
class that extends
Point
.)
23.
Declare a method called
getSideCount
in the
Shape
interface that returns the number of sides that the shape has.
Implement the method in all shape classes. A circle is defined to have 0 sides.
Section 9.6: Case Study: Financial Class Hierarchy
24.
What is an abstract class? How is an abstract class like a normal class, and how does it differ? How is it like an interface?
25.
Consider writing a program to be used to manage a collection of movies. There are three kinds of movies in the
collection: dramas, comedies, and documentaries. The collector would like to keep track of each movie's title, the
name of its director, and the year the movie was made. Some operations are to be implemented for all movies, and
there will also be special operations for each of the three different kinds of movies. How would you design the
class(es) to represent this system of movies?
Exercises
1.
Write the class
Marketer
to accompany the other law firm classes described in this chapter. Marketers make
$50,000 ($10,000 more than general employees) and have an additional method called
advertise
that prints
"Act now, while supplies last!"
Make sure to interact with the superclass as appropriate.
2.
Write a class
Janitor
to accompany the other law firm classes described in this chapter. Janitors work twice as
many hours per week as other employees (80 hours/week), they make $30,000 ($10,000
less
than general employ-
ees), they get half as much vacation as other employees (only 5 days), and they have an additional method
clean
that prints
"Workin' for the man."
Make sure to interact with the superclass as appropriate.
3.
Write a class
HarvardLawyer
to accompany the other law firm classes described in this chapter. Harvard lawyers
are like normal lawyers, but they make 20% more money than a normal lawyer, they get 3 days more vacation, and
they have to fill out four of the lawyer's forms to go on vacation. That is, the
getVacationForm
method should
return
"pinkpinkpinkpink"
. Make sure to interact with the superclass as appropriate.
4.
For the next four problems, consider the task of representing types of tickets to campus events. Each ticket has a
unique number and a price. There are three types of tickets: walk-up tickets, advance tickets, and student advance
tickets. Figure 9.10 illustrates the types:
• Walk-up tickets are purchased the day of the event and cost $50.
• Advance tickets purchased 10 or more days before the event cost $30, and advance tickets purchased fewer than
10 days before the event cost $40.
• Student advance tickets are sold at half the price of normal advance tickets: When they are purchased 10 or more
days early they cost $15, and when they are purchased fewer than 10 days early they cost $20.
Implement a class called
Ticket
that will serve as the superclass for all three types of tickets. Define all common
operations in this class, and specify all differing operations in such a way that every subclass must implement them.
No actual objects of type
Ticket
will be created: Each actual ticket will be an object of a subclass type. Define the
following operations:
• The ability to construct a ticket by number.
• The ability to ask for a ticket's price.
• The ability to
println
a ticket object as a
String
. An example
String
would be
"Number: 17, Price: 50.0"
.
Search WWH ::
Custom Search