CS
1713 Recitation Exercise 12:
Object
Hierarchies and Inheritance
Objectives:
-
Use an abstract class.
-
Practice with the is-a relationship.
-
Use a helper class to handling I/O.
-
Use type-casting in a more complex program.
-
Use the instanceof operator to distinguish among subclasses.
-
Use protected class members to share information among classes
in the hierarchy.
Prior to the recitation:
-
Read Section 7.4 of the textbook carefully. We will be using the classes
in Listings 7.15, 7.18, and 7.19. Be sure to bring your textbook to class.
-
Make index cards for the
StaffMember, StaffMemberIO,
Employee,
Executive and Hourly classes.
-
Print out this recitation and read it over.
-
To save time during the lab, do the setup before your lab hour
Hand-in Requirements:
Hard-copy of the source for: DepartmentStore.java (10 pts)
and DepartmentStoreTest.java (10 pts).
Overview:
You will be using the class hierarchy shown in Figure 7.7 of the textbook
to implement a DepartmentStore class that will track the employees
of a particular store. The classes shown in Figure 7.7 (StaffMember,
Employee,
Executive
and Hourly) are already written. Our department store doesn't
use volunteers, so the Volunteer class isn't included. A helper
class StaffMemberIO is also provided to create StaffMember
objects of various types by reading data from a file. Part I explores the
use of this class. Part II guides you through the development of a
DepartmentStore
class that uses StaffMember objects of various sorts.
Setup:
Part I: Using the StaffMemberIO class.
-
Do the prelab setup as described above to create the
DepartmentStoreTest
project as a standalone console application. Build and run.
-
Instantiate a StaffMemberIO object called
empIn in DepartmentStoreTest.
Use
employees.txt as the file name parameter. When you instantiate
a StaffMemberIO object it opens the specified file for input.
-
The StaffMemberIO class has a method called
getNext that
returns a new object of one of the types derived from StaffMember
(e.g.,
Employee, Hourly, or Executive) based
on the next entry read from the file. (Why can't it return a StaffMember
object?) If all of the entries have already been read, or there is an error
of some kind,
getNext returns null. Add a loop to DepartmentStoreTest
to read in the objects from
employees.txt and output them.
-
Add code in your loop to determine what kind of object is returned (e.g.,
Employee,
Hourly, or Executive) by using instanceof and
output the type of object as well as the object. Notice that since
Executive
and Hourly objects are also Employee objects, you must
test for Executive and Hourly first.
-
Get a hard copy of your output before going on.
Part II: The DepartmentStore class
The DepartmentStore class keeps track of all the information about
the employees for a particular store by keeping an array of objects corresponding
to the information in employees.txt.
-
Comment out the code from Part I in DepartmentStoreTest
-
Write client code to instantiate a DepartmentStore object called
store.
Use employees.txt as the file name.
-
Write the initialize method that instantiates a StaffMemberIO
object for myFileName and reads the information into myEmployees.
Set
myNumberEmployees appropriately. Call resize if necessary.
-
Write the following print methods of
DepartmentStore
and add client code in DepartmentStoreTest to test them:
-
print - prints the information about every employee. Include their
classification in the printout.
-
printEmployees - prints all the information about each employee
who is an employee, but not an executive or hourly. (You will need to use
the instanceof operator here.)
-
printExecutives - prints all the information about each executive
employee. (You will need to use the instanceof operator here.)
-
printHourly prints all the information about each hourly employee.
(You will need to use the instanceof operator here.)
-
Write a findEmployee(String SSN) method in DepartmentStore
that returns a Employee object corresponding to the specified
employee or
null if the employee is not found. Add client code
to DepartmentStoreTest to test this method. (Notice that StaffMember
doesn't have a field corresponding to the social security number. This
is an example of the use of a protected field among subclasses.)
-
Write an updateHours(String SSN, int hours) method in DepartmentStore
that adds the specified hours to the specified hourly employee. If the
employee is not an
Hourly print out a warning message. Use
findEmployee.
-
Write a method awardBonus(double amount) that sets the bonus for
each executive to amount. Add client code to DepartmentStoreTest
to test this method.
Last revision: November 13, 2001 at 10:15 am.