CSC 207 Grinnell College Spring, 2012
 
Algorithms and Object-Oriented Design
 

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 Entry.java contains a simple version of an Entry class.

  1. Create a schoolDirectory package within Eclipse for use with all steps in this lab.

  2. 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 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 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 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 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 a remove method to class SchoolDirectory to delete a person with a given first and last name from the directory.

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

Adding Time-related Fields

Suppose directory information were supplemented to include time-related fields:

  1. Revise Entry and Student to include the time-related fields and methods described above.
    Note: Faculty and Staff do not need to be changed.

  2. Add a yearUpdate method to a SchoolDirectory. This method should update each object in the directory by adding 1 year (or equivalent) to each time-related field.
    Note: This should be easy!

Exceptions and Exception Handling

The program 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.walker.cs.grinnell.edu/courses/207.sp13/labs/lab-generalization.shtml

created 2 May 2000 by Henry M. Walker
revised 6 February 2012
time fields added 5 February 2013
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.


Copyright © 2011-2012 Henry M. Walker.
CC-BY-NC-SA
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License .