This is a lab I am working for for a CSE 201. The program is supposed to read information about students and their scores from a file, and output the name of each student with all his/her scores and the total score, plus the average score of the class, and the name and total score of the students with the highest and lowest total score.
The exact assignment can be seen here: http://www.cse.ohio-state.edu/cse201/labs/lab7/lab7.html
I'm having a little trouble getting it to compile, especially with the variable "students
".
Any help would be great, thanks so much everyone.
-Kyle
/*
* The program will read information about students and their
* scores from a file, and output the name of each student with
* all his/her scores and the total score, plus the average score
* of the class, and the name and total score of the students with
* the highest and lowest total score.
*/
import java.util.Scanner;
public class Lab7
{
public static void main(String[] args)
{
// Input file name
Scanner in = new Scanner(System.in);
String filename = getFileName(in);
// Input number of students
int Student[students] = getStudents(FileIOHelper.getNumberOfStudents(filename));
// Input all students records and create Student array and
// integer array for total scores
int[] totalScores = new int[students.length];
for(int i = 0; i < students.length; i++){
for(int j = 1; j < 4; j++){
totalScores[i] += students[i].getScore(j);
}
}
// Compute total scores and find students with lowest and
// highest total score
int maxIndex = 0, minIndex = 0;
for(int i = 0; i < students.length; i++){
if(totalScores[i] > totalScores[maxIndex]){
maxIndex = i;
}else if(totalScores[i] < totalScores[minIndex]){
minIndex = i;
}
}
// Compute average total score
int average = 0;
for(int i = 0; i < totalScores.length; i++){
average += totalScores[i];
}
average /= students.length;
// Output results
outputResults(students, totalScores, maxIndex, minIndex, average);
}
// Given a Scanner in, this method prompts the user to enter
// a file name, inputs it, and returns it.
private static String getFileName(Scanner in)
{
System.out.print("Enter input file name: ");
return in.nextLine();
}
// Given the number of students records n to input, this
// method creates an array of Student of the appropriate size,
// reads n student records using the FileIOHelper, and stores
// them in the array, and finally returns the Student array.
private static Student[] getStudents(int n)
{
Student[] student = new Student[n];
for(int i = 0; i < student.length; i++){
student[i] = FileIOHelper.getNextStudent();
}
return student;
}
// Given an array of Student records, an array with the total scores,
// the indices in the arrays of the students with the highest and
// lowest total scores, and the average total score for the class,
// this method outputs a table of all the students appropriately
// formatted, plus the total number of students, the average score
// of the class, and the name and total score of the students with
// the highest and lowest total score.
private static void outputResults(
Student[] students, int[] totalScores,
int maxIndex, int minIndex, int average
)
{
System.out.println("\nName \t\tScore1 \tScore2 \tScore3 \tTotal");
System.out.println("--------------------------------------------------------");
for(int i = 0; i < students.length; i++){
outputStudent(students[i], totalScores[i], average);
System.out.println();
}
System.out.println("--------------------------------------------------------");
outputNumberOfStudents(students.length);
outputAverage(average);
outputMaxStudent(students[maxIndex], totalScores[maxIndex]);
outputMinStudent(students[minIndex], totalScores[minIndex]);
System.out.println("--------------------------------------------------------");
}
// Given a Student record, the total score for the student,
// and the average total score for all the students, this method
// outputs one line in the result table appropriately formatted.
private static void outputStudent(Student s, int total, int avg)
{
System.out.print(s.getName() + "\t");
for(int i = 1; i < 4; i++){
System.out.print(s.getScore(i) + "\t");
}
System.out.print(total + "\t");
if(total < avg){
System.out.print("-");
}else if(total > avg){
System.out.print("+");
}else{
System.out.print("=");
}
}
// Given the number of students, this method outputs a message
// stating what the total number of students in the class is.
private static void outputNumberOfStudents(int n)
{
System.out.println("The total number of students in this class is: \t" + n);
}
// Given the average total score of all students, this method
// outputs a message stating what the average total score of
// the class is.
private static void outputAverage(int average)
{
System.out.println("The average total score of the class is: \t" + average);
}
// Given the Student with highest total score and the student's
// total score, this method outputs a message stating the name
// of the student and the highest score.
private static void outputMaxStudent(
Student student,
int score
)
{
System.out.println(student.getName() + " got the maximum total score of: \t" + score);
}
// Given the Student with lowest total score and the student's
// total score, this method outputs a message stating the name
// of the student and the lowest score.
private static void outputMinStudent(
Student student,
int score
)
{
System.out.println(student.getName() + " got the minimum total score of: \t" + score);
}
}