CSC 153 Grinnell College Spring, 2005
 
Computer Science Fundamentals
Laboratory Exercise
 

Generalization, Polymorphism, and Exceptions

Goals

This lab provides experience using techniques of generalization, polymorphism, and exception handling within the context of a typical problem.

A Student, Staff, and Faculty Directory

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:

a class hierarchy

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.

The Entry Class

Program ~walker/java/examples/schoolDirectory/Entry.java contains a simple version of an Entry class.

  1. Copy Entry.java to your account, compile and run it, and check that you understand how it works.

The Student Subclass

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

  1. Copy Student.java to your account. Again, compile and run the program, and check that you understand how it works.

The Faculty Subclass

Program ~walker/java/examples/schoolDirectory/Faculty.java provides an implementation of the Faculty class, analogous to Student.
  1. Copy Faculty.java to your account, run it, and review how it works.

  2. 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!

The School Directory Class

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.

  1. 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.

  2. Add an update capability to this application, as follows:

    1. 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.

    2. 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.

  3. Add a remove method to class SchoolDirectory to delete a person with a given first and last name from the directory.

  4. Extra Credit: Write a Staff class which extends Entry, according to the field specification given at the start of this lab.

Exceptions and Exception Handling

The program ~walker/java/examples/schoolDirectory/SchoolDirectoryAlt.java uses a NoSuchElementException if a search determines that a person is not present in the directory.

  1. 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.

  2. 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.

Work To Turn In:


This document is available on the World Wide Web as

http://www.cs.grinnell.edu/~walker/courses/153.sp05/labs/lab-generalization.shtml

created 2 May 2000 by Henry M. Walker
last revised 2 May 2005
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.