CS256: Lab 5


Due: See Moodle for the due date and time

Goals

1)    More experience with nested loops

2)    More experience using methods

Requirements

1.     Write a class named Grades that inputs and calculates grades for a number of students.  Each student has five grades.  The output of the method main() must consist of (a) the average score of the student with the highest average, (b) the average score of the student with the lowest average, (c) the overall average for all students.  You must have a public method named studentAve() that does the following:

·         reads the five grades for a single student,

·         returns the average of this student’s grades,

·        does not treat negative grades any differently than positive grades

The studentAve() method takes no parameters and returns a double, so its method signature must be:

public static double studentAve()

 

main() must read a double from the user that indicates whether or not there is another student.  If this number is negative, main() should output the required averages.

 

The autograder requires you use this copy of IO.java, which you should save into the same directory where you saved Grades.java and use to read doubles from the user.

In IO.java, the methods that read data from the user are named read<Type>(“prompt: “), e.g.:

double d = IO.readDouble("Your input: ");

 

So, what you do with two statements using the methods in TextIO.java, you do with a single statement using the methods in IO.java. 

Here is the input for a sample run (the ‘|’ represents typing the ‘Enter’ key – like in the autograder):
0|
// this number is read by main() to indicate that there is information for another student
1|2|3|4|5|
0|
// indicates there is another set of student grades to read
2|4|6|8|10|
-1|    
// indicates there are no more students to process

Here is the output for the input above (again, the ‘|’ represents println() printing an end-of-line character – like in the autograder):
Low Average: 3.0; High Average: 6.0; Overall Average: 4.5|

 

Here is another sample run:

0| // this number is read by main() to indicate that there is information for another student
1|2|-2|4|5|
-1|    
// indicates there are no more students to process
Low Average: 2.0; High Average: 2.0; Overall Average: 2.0|

 

Here is one more sample run:

-.5|     // indicates there are no more students to process
Low Average: 0.0; High Average: 0.0; Overall Average: 0.0|

 


You may print anything you like using System.err.println(), but, in order to pass the autograder, you must print exactly the required output and nothing else using System.out.println().

Hints:
a) If the first number main() reads is negative, print 0.0 for low, high, and overall averages.
b) in studentAve() use a loop and a counter, and loop until you have read 5 grades then return.  The loop in studentAve() does NOT keep reading until it sees a negative input.

 

2. Write answers to the following synthesis questions in a comment at the top of your Grades.java file:

·         How does the existence of the studentAve() method simplify the main() method?

·         When a method returns a value, where does this value go?

·         How is returning a value from a method different than printing that value?

  

3. Thoroughly test your methods then turn in your lab using the autograder

The autograder advertises what the inputs and outputs should be.  All outputs that the autograder advertises that it is expecting must be printed using System.out.println() all other outputs must be printed using System.err.println() in order to pass the autograder.

 

Note:  The autograder will give a score of up to 100 for the ‘functional’ portion of the lab.  From this score, I will remove points for: missing name at the top of the code, poor indentation, poorly structured code, missing or incorrect synthesis question answers.