Java Reference
In-Depth Information
account with the interest rate and the number of months to maturity. In the
addInterest
method, decrement the count of months. If the count is
positive during a withdrawal, charge the withdrawal penalty.
Exercise P10.3. Implement a subclass
Square
that extends the
Rectangle
class. In the constructor, accept the x- and y-positions of the
center and the side length of the square. Call the
setLocation
and
setSize
methods of the
Rectangle
class. Look up these methods in
the documentation for the
Rectangle
class. Also supply a method
getArea
that computes and returns the area of the square. Write a sample
program that asks for the center and side length, then prints out the square
(using the
toString
method that you inherit from
Rectangle
) and the
area of the square.
Exercise P10.4. Implement a superclass
Person
. Make two classes,
Student
and
Instructor
, that inherit from
Person
. A person has a
name and a year of birth. A student has a major, and an instructor has a
salary. Write the class definitions, the constructors, and the methods
toString
for all classes. Supply a test program that tests these classes
and methods.
Exercise P10.5. Make a class
Employee
with a name and salary. Make a
class
Manager
inherit from
Employee
. Add an instance field, named
department
, of type
String
. Supply a method
toString
that prints
the manager's name, department, and salary. Make a class
Executive
inherit from
Manager
. Supply appropriate
toString
methods for all
classes. Supply a test program that tests these classes and methods.
Exercise P10.6. Write a superclass
Worker
and subclasses
HourlyWorker
and
Salaried-Worker
. Every worker has a name
and a salary rate. Write a method
computePay(int hours)
that
computes the weekly pay for every worker. An hourly worker gets paid the
hourly wage for the actual number of hours worked, if
hours
is at most
40. If the hourly worker worked more than 40 hours, the excess is paid at
time and a half. The salaried worker gets paid the hourly wage for 40
hours, no matter what the actual number of hours is. Supply a test program
that uses polymorphism to test these classes and methods.