CSci 161 C

Fifth exercise set

Due:  Monday, Nov. 13

An exercise in arrays (20 points)

The file STUDENTS.DAT in the BlueJ handouts folder on Hedwig contains records for ten students organized in the following way:  Each record contains a student number (String), student name (String), and four exam scores (double).  Each field is separated from the other by spaces.  Below I have placed some code demonstrating how to read a text file (one of several methods).  The file is organized by student number.

Write a class called Student with the above fields together with a (double) exam average.  The exam scores should be stored as an array.  The constructor should accept (as arguments only - no input/output statements in methods of class Student) a student number, student name, and the four exam scores.  You should have a (void) method addAll which uses a for-loop to add up the exam scores and compute the average (placing it into the appropriate field.  Over-ride the toString method to produce a formatted printout of a Student object. Add an accessor method to return the current exam average for the student.

Then write a second class called ClassRoom which contains an array of ten Student records.  The constructor should read in the student records from the file and calculate the exam average for each one (use the addAll method of the Student object).  Then write methods as follows:

Turn in:  Source listing for the class and the class documentation together with a copy of all output.  The output should include:

To print the output:

Notes on opening and using files:

Things to import

Sample code to read a file "EMP.DAT" in the current directory (the one with all the Java files in it) and to print out information on the console:

public void readFile() throws IOException
{
	Scanner reader = new Scanner(new FileInputStream("EMP.DAT"));
	while (reader.hasNext()) {
		String eNo = reader.next();
		String eName = reader.next();
		double sal = reader.nextDouble();
		String supervisor = reader.next();
		System.out.println(eNo + "\t" + eName + 			"\t" + sal + "\t" + 
			supervisor);
	}
	System.out.println("\n\n\tDONE!\n");
}

The statement "throws IOException" is required just in case the file is not in the right spot.  We will talk about exceptions in chapter 12 (probably the last chapter to be covered this semester).

All of this is in the folder EmpRead in the BlueJ folder in my handouts folder on Hedwig.