Java – GradeBook Java Program. Help calling class

arraysclassjavapublic

The assignment goes as follows.

A teacher has five students who have taken four tests. The teacher uses the following grade scale to assign a letter grade to a student, based on the average of his or her four test scores.

90-100:  A
80-79:  B
70-79:  C
60-69:  D
0-59:  F

Write a class that uses a String array on an arraylist object to hold the five students' names, an array of five characters to hold the five students' letter grades, and five arrays of four doubles each to hold each student's set of test scores. The class should have methods that return a student's name, the average test score, and a letter grade based on the average.

Demonstrate the class in a program that allows the user to enter each student's name and his or her four test scores. It should then display each student's average test score and letter grade.

Program should not accept a score lower than 0 or higher than 100

The following code is here:

Student class:

public class Student{
    private String name;
    private double[] test = new double[4];


    public Student(){
        name = " ";
    }

    public Student(String n, double[] t){
        name = n;
        test = t;
    }

    public void setName(String n)
    {
        name = n;
    }

    public String getName(){
        return name;
    }

    public void setTest(double t,int i)
    {
        test[i] = t;
    }

    public double getTest(int i)
    {
        return test[i];
    }


    public double getTestAvg(){

        double sum = 0;
        double avg;
        for(int i = 0; i < test.length; i++)
        {
            sum += test[i];
        }
         avg = sum / test.length;
         return avg;
    }

    public char getLetterGrade(){

        double average = getTestAvg();
        char grade=0;

        if(average >= 90)
            grade = 'A';
        else if (average >= 80)
            grade = 'B';
        else if (average >= 70)
            grade = 'C';
        else if (average >=60)
            grade = 'D';
        else if (average < 60)
            grade = 'F';

        return grade;
    }

    public String toString(){
        String str = "";
        str += "\nName of student: " + name;
        str += "\nAverage test score: " + getTestAvg();
        str += "\nLetter grade: " + getLetterGrade();
        return str;
    }
}       

The Main Program:

import java.util.Scanner;
import java.io.*;

public class GradeBook {

    public static void main(String[] args) throws IOException {

        Student[] students = new Student[5];

        getStudentData(students);

    }

    public static double getStudentData(Student[] array) {
        Scanner scan = new Scanner(System.in);
        String[] student = new String[5];
        double[] test = new double[4];
        for (int i = 0; i < student.length; i++) {
            System.out.println("Enter the name of the student : ");
            student[i] = scan.nextLine();
            for (int j = 0; j < test.length; j++) {
                System.out.println("Enter score " + (j + 1) + " for the student");
                test[j] = scan.nextDouble();
                scan.nextLine();
            }
            array[i] = new Student(student[i], test);

        }
        return 0;

    }
}

The problem is, is that I can enter all the information, but nothing will display after all the numbers have been entered in. Any help would be greatly appreciated!

Best Answer

You're not seeing anything displayed because you never make the call to display anything. You are populating the "array", so you need to iterate over it now.

After the outer for loop (the first one), you'll want to iterate over the populated array and print out the student objects. This will work since you already have a toString() method.

ex:

for(Student student : array)
{
    System.out.println(student);
}