| |
Due by 8:00 AM on Monday, November 15, 1999
Clarifications:
(1) Student ID numbers should start with 1 (not zero) and increase
by 1 each time you add a new student.
(2) When you first add students, the ID numbers will be closely related
to positions in the array of students. So why, you may ask, store
a separate ID number field? Because if you sort the array by name
(menu option 8), this correspondence will be messed up.
|
| | For this homework, you will
write a program Database that maintains a database of student records
for the registrar. (This program expands on ideas from week 9's
homeworks and this week's lab problems.) The program will maintain a
database of student records for the registrar. Each record (an object
of the class Student) will contain the following
information:
- ID Number -- An
int
- Last Name -- A
String
- First Name -- A
String
- Expected Year of Graduation -- An
int
- Major Cumulative GPA -- A
float
- Overall Cumulative GPA -- A
float
The program begins by asking the user how many records they would like
the program to be able to handle. It then allocates an array of
that many Student object references. It should not,
however, allocate the actual objects at that point. As the
array will be filled incrementally and may never be totally filled,
it would be a waste of space to allocate storage in advance
for all the objects that might potentially be used.
Note that since not all of the cells in the array are in use at any one time, it
will be necessary to keep track of how much has actually been used.
This will be stored as an int value and passed along with the
array to any method that needs to manipulate the database.
(The point is, the .length attribute will have a fixed value
once the array has been allocated, but we won't be using all that allocated
space at a given point. So, we need to keep track of how much is really
in use.) (Java actually provides a class of objects called Vectors,
which act like growable arrays. If we used them instead of arrays we would not need to
do this bit ourselves. But at this point you need to get more comfortable
with arrays before moving on to more complex structures.)
Once the array has been allocated, the program
continues by displaying the following status message and menu:
There are currently 0 student records in the database.
Please Choose an action from the following list:
1 Display All Student Records
2 Display Student Records for Given Year
3 Display Honor Roll
4 Display Students in Trouble
5 Display Individual Record by ID Number
6 Display Individual Record by Name
7 Add a Student Record
8 Sort Database by Name
99 Quit
Choice:
If the user picks a valid choice, that action should be
performed. Otherwise an error should be displayed. In either case,
the status message and menu should then be redisplayed and the process repeated, until
the user chooses the Quit option.
|
| |
You will simplify all the report generating methods if you first write a method called
displayStudent which takes a Student object and displays its
information on a single line. Print the field values in the order in which they were
listed above. Use 15 character fields for each part of the name and 10 character
fields for each of the other parts of the record.
It will also make your life easier to write a method called
displayReportHeader which takes no parameteres and prints out the labels for
the columns of a report whose lines will be printed by displayStudent.
The menu actions should behave as follows:
- Display All Student Records --
Display all of the student records in a line-oriented report.
Call
displayStudent from the
method displayAllStudents which takes as parameters
the array of all the records and the number of records in use.
It should print a heading for the report, labeling the columns,
and then call displayStudent repeatedly (in a loop)
to print the data for each student in turn.
- Display Student Records for Given Year --
Prompt the user for a year of graduation. Display a report similar to the
one for the last option, but restricted to students
that have the specified year as their year of graduation. Display the report with the method
displayYearStudents which takes
the record array, the number of records in the array, and a year of graduation to restrict
the report to.
- Identify Honor Roll --
As above, but restrict the report by prompting for a cutoff GPA and passing it (along with the
array and number of records) to
displayHonorRoll,
which displays the records of those students for whom BOTH
of their GPAs are at or above the given cutoff.
- Identify Students in Trouble --
As above, but the method
displayTroubleList
prints a report on those students for whom EITHER
of their GPAs is at or below the given cutoff.
- Display Individual Record by ID Number --
Prompt the user for a Student ID Number to search for. Pass the array of records,
the number of records in the array, and the desired ID number to
findStudentByID,
which should return the desired student's index in the array, or -1 if that student is not in
the database. Depending on the result, either display the student's record (with column labels)
or print an apprpriate error message.
Note, findStudentByID does not display the record, it merely returns the index to the
calling method. It is that method's responsibility to print column headings and
call displayStudent.
You may assume ID numbers are unique.
- Display Individual Record by Name --
As above, but prompt for a first and last name rather than an id number, and pass these,
along with the array and number of records to
findStudentByName. If there is more
than one student with a the given name, return the index of the first one in the database.
- Add a Student Record --
Write a method
inputStudent which takes a Student ID Number
as a parameter, declares and allocates a local Student object,
puts that ID number in the
record, prompts the user for values for the rest of the fields, and then returns that
object as the return value. Use this function to add a new record for the record array.
You should assign the student's ID number as the current number of records in the database
(by incrementing it and then passing it to inputStudent).
This way (since we are not allowing records to be deleted) the ID numbers
will automatically be unique.
- Sort Database by Name --
Sort the database by name as would be done in a telephone directory.
Make sure you take into account both first and last names.
Do not display the database after, just return to the menu.
To test a sample solution, click here.
|