Sei sulla pagina 1di 1

F0102

Laboratory Exercise 2: Abstract Classes and


Inheritance
At the end of the exercise, the students should be able to:
Create an abstract class.
Define abstract methods.
Compare the difference between abstract class and concrete class.
Implement polymorphism.
Material:
PC with Java SDK and JCreator installed
Perform the following instructions:
Write an abstract class called Employee with two concrete subclasses (PartTimeEmployee and
FullTimeEmployee), and a test program (TestEmployee). Each of these classes is described below.
The abstract Employee class has a name field and a salary rate (in peso per hour) field. The Employee
class has an abstract instance method computeWeeklyPay(int hours) (each subclass of Employee will
have its own way of computing weekly pay). The Employee class should have appropriate constructors,
appropriate (concrete) set and get methods for the name and salary fields, and it should also have a
(concrete) toString() method that returns the name and salary rate in an appropriate String.
The PartTimeEmployee class has a boolean instance field called isUnionMember and a String instance
field called unionName (which should be set to null if isUnionMember is false). The PartTimeEmployee
class has a computeWeeklyPay(int hours) method that implements the same method from the super class
(an hourly Employee gets paid for the actual number of hours worked, with overtime over 40 hours paid at
time and a half). The PartTimeEmployee class should have appropriate constructors (that make use of the
super class constructors), appropriate set and get methods, and it should also have a toString() method
that both overrides and calls the toString() method in the super class, and that returns the name, salary
rate, and union status in an appropriate string.
The FullTimeEmployee class has an int instance field called subordinates that records the number of
Employees that the salaried Employee manages. The FullTimeEmployee class has a
computeWeeklyPay(int hours) method that implements the same method from the super class (a salaried
Employee always gets paid for 40 hours, no matter what the actual hours worked is). The
FullTimeEmployee class should have appropriate constructors (that make use of the super class
constructors), appropriate set and get methods, and it should also have a toString() method that both
overrides and calls the toString() method in the super class, and that returns the name, salary rate, and
number of subordinates in an appropriate string.
Write a test class called TestEmployee. It should create an array of type Employee and fill it with some
PartTimeEmployee and some FullTimeEmployee objects. Then the program should loop through the array
and use polymorphism to compute the pay and print out all of the information for each object in the array.
The test class should also show that your constructor, get, and set methods all work.
Name your four programs as Employee.java, PartTimeEmployee.java, FullTimeEmployee.java and
TestEmployee.java.

Laboratory Exercise 2: Abstract Classes and Inheritance * Property of STI


Page 1 of 1

Potrebbero piacerti anche