|CSC 153||Grinnell College||Spring, 2005|
|Computer Science Fundamentals|
This lab provides experience using techniques of generalization, polymorphism, and exception handling within the context of a typical problem.
A campus directory is to contain information on students, staff, and faculty. All entries will contain a person's first name, last name, and e-mail address. However, other fields may depend upon whether an individual is a student, staff member, or faculty member. One organization of these entries is illustrated in the following diagram:
Utilizing these data fields, a directory class should contain these methods:
Additional details and an alternative hierarchy may be found in today's readings. Implementation of this class hierarchy involves classes Entry, Student, and Faculty for individuals and class SchoolDirectory for the overall structure. The development of the Staff class is left as an exercise.
Program ~walker/java/examples/schoolDirectory/Entry.java contains a simple version of an Entry class.
Copy Entry.java to your account, compile and run it, and check that you understand how it works.
With Entry defined, we can define the Student class by extending Entry and adding only the few new required fields. The resulting code is ~walker/java/examples/schoolDirectory/Student.java
Copy Student.java to your account. Again, compile and run the program, and check that you understand how it works.
Copy Faculty.java to your account, run it, and review how it works.
In Faculty.java, the current toString method relies on Entry's toString method to begin the process. Suppose, however, that we want to change the format of output, so a faculty member's department follows on the line immediately after the person's name rather than later in the listing. Modify toString in Faculty.java to make this change. DO NOT CHANGE Entry in any way!
Program ~walker/java/examples/schoolDirectory/SchoolDirectory.java places various entries within an array. The variable maxSize indicates the actual size of the array, and a separate variable currentSize keeps tracks of how many items are actually stored within the array. We can keep adding entries to the array until currentSize equals maxSize. For additional entries, we generate a larger array (perhaps twice the size), copy the old array to the new, larger one, and then make the insertion.
Copy ~walker/java/examples/schoolDirectory/SchoolDirectory.java to your account, and review the code. Be sure you can describe how the methods add and print work. Also, be sure you understand the purpose of the method toString.
Add an update capability to this application, as follows:
Add an update method to class Entry and each of its subclasses to provide a framework to change data within an entry (except the name). update should use no parameters. Rather, the method should prompt the user at the keyboard for new values of all fields within the Entry, except for the person's name which should remain unchanged.
Add an update method to class SchoolDirectory. This update method should use two String parameters, for the person's first and last name. update then should use the lookup to locate the correct Entry within the directory, and the Entry's update method to effect the change. If the specified name is not in the directory, a warning message should be printed.
Add a remove method to class SchoolDirectory to delete a person with a given first and last name from the directory.
Extra Credit: Write a Staff class which extends Entry, according to the field specification given at the start of this lab.
The program ~walker/java/examples/schoolDirectory/SchoolDirectoryAlt.java uses a NoSuchElementException if a search determines that a person is not present in the directory.
Explain how processing within SchoolDirectoryAlt.java works when there is an attempt to locate a person in the directory when that person is not found.
Modify the update and remove methods in SchoolDirectory to respond appropriately if lookup throws a NoSuchElementException exception. Note that your changes should affect the methods (and testing) in SchoolDirectory only. No changes should be made in Entry or any of its subclasses.
This document is available on the World Wide Web as
created 2 May 2000 by Henry M. Walker
last revised 2 May 2005
|For more information, please contact Henry M. Walker at email@example.com.|