MIDTERM
CS202, Summer 2004
Please print out a hard copy of all your files and turn it in
at
the end of the exam. Do not forget to put your name on the hard copy. Before turn in your programs, try to
compile and run them and make sure they work correctly. If your program
does not even compile, you receive an automatic 50% credit deduction
for that
problem, no matter how "close" the program seems to be correct.
1.(15pt) Fill in the blank with a correct word to complete the sentence:
- When an object is no longer used in a program, it is marked for
automatic
collection.
- A subclass can access public and
members of a superclass, but not
members.
- The OO language feature that allows a superclass reference
pointing to an object of its subclass is called
.
- Explicit type conversion is called cast, and implicit
type
conversion is called
.
2. (14pt) Use no more than three sentences to answer each of the
following questions:
- What is the difference between associativity and precedence?
- What is the difference between a static class member and
a
non-static class member?
3. (16pt) Give one code example that shows overloading, overriding,
and dynamic dispatching in the same example. Use code comments
to indicate where overloading, overriding, or dynamic
dispatching takes place. Note that your example may consist of
multiple classes.
4. A class roster in the GET system is a list of student records, and
each student record is in the following format:
LastName,FirstName
[OptionalMiddleName] StudentID
College
A sample file of a class roster can be found here. For simplicity, we assume that a)
no class has
more than 8 students, b) a student ID is a 9-digit number which starts
with 2, and c) two students may have the same name, but student IDs are
unique.
(a) (15pt) Write a Java class Student which holds the
information of a student. The implementation of the class is up to you,
but the Student class must provide at
least the following method(s):
- Student( String id, String lname, String fname ) --
constructor. The constructor takes three String parameters, student ID,
last name, and first name. By default we assume that a student does not
have a middle name, and the student is from College of Engineering
& Technology.
- String toString() -- returns the student information as
a string.
(b) (40pt) Write a Java class Roster which holds the student
records of a class. Again the implementation is up to you, but the
Roster class must provide at least
the following method(s):
- Roster( String filename ) -- constructor. The
constructor reads the student records from a file, whose name is
provided as the parameter of the constructor.
- int numOfStudents() -- returns the number of students in
the class.
- int numOfStudents( String college ) -- returns the
number of students in the class who are from a specific college.
- void list() -- prints out a list of the student names in
the form FirstName LastName, e.g.
John Doe
Mary Watson
Peter Parker
- void add( Student s ) -- add a new student to the
roster.
- If the class is
already full, e.g. there are already 8 students in the class, this
method throws a RosterException with the message "Class is
already full".
- If student s is already in the class, this method throws a RosterException
with the message "Student is already enrolled".
Test your Student and Roster classes with the sample roster file and the test program RosterTest.java