| CS 261 | University of Puget Sound | Spring, 2020 |
|
Computer Science II
|
||
|
Abstract Data Types and their Implementations,
Some Basic Algorithms,
Object-oriented Problem Solving, and Efficiency |
||
Warning:
This course is under development.
Although the basic structure of this course is largely established,
nothing on this Web site should be considered official or even possibly
correct.
DO NOT MAKE PLANS BASED ON THE CONTENTS OF THIS SITE UNTIL JANUARY, 2020.
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 persons 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 persons 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 Person, Student, and Faculty for individuals and class SchoolDirectory for the overall structure. The development of the Staff class is left as an exercise.
Program Person.java contains a simple version of an Person class.
Create a schoolDirectory package within Eclipse for use with all steps in this lab.
Copy Person.java to your account, compile and run it, and check that you understand how it works.
With Person defined, we can define the Student class by extending Person and adding only the few new required fields. The resulting code is 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 Person'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 Person in any way!
Program SchoolDirectory.java places various persons 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 persons to the array until currentSize equals maxSize. For additional persons, we generate a larger array (perhaps twice the size), copy the old array to the new, larger one, and then make the insertion.
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.
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 Person, according to the field specification given at the start of this lab.
Suppose directory information were supplemented to include time-related fields:
Revise Person and Student to include the time-related
fields and methods described above.
Note: Faculty and Staff do not need to be changed.
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!
The program 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.
|
created 2 May 2000 by Henry M. Walker revised 6 February 2012 time fields added 5 February 2013 revised 14 September 2018 revised replacing Person class with Person 18-19 January 2020 |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |