Search code examples
javaarraysinheritancesubclasssuperclass

JAVA Inheritance student, undergrad and gradstudent code


For a class project I was asked to create three codes. Student Class

  1. First, the student class containing three Parameters.

    • Name(String)
    • TestScores( int array),
    • Grade(String).
  2. An empty Constructor. sets the Name and Grade to empty Strings. The TestScores Array will be initialised with three zeros.

  3. Another Constructor(String n, int[] tests, String g)- This will set the name, testScores, and grade.

  4. Three methods:

    • getName() to return the name
    • getGrade() to return the grade
    • setGrade() to set grade
    • getTestAverage() to return the average of the test scores.
  5. This is where I am having difficulty The method computeGrade(), which, if the average is greater than or equal to 65, the grade is a "Pass". Otherwise it is a "Fail".

The second class is called UnderGrad. This class is a subclass of Student.

  1. We had to create an empty Constructor and another Constructor (String n, int[] tests, String g).

  2. We were instructed to override the computeGrade() method so that an UnderGrad() student must get a 70 or higher to pass.

The third class is the GradStudent a subclass of Student.

  1. We have to create 1 instance variable, int MyGradID, and an empty constructor, which calls super, and set the IDs to 0.

  2. And another constructor (String n, int[] tests, String g, int id)- Remember to call the super constructor and set ID.

  3. Again where I am having challenges. We had to write the method getId(), to return the ID number. Again we needed to override the computeGrade() method And, if the average is greater than or equal to 65, the grade is a "Pass". Otherwise it is a "Fail". But if the test average is higher than 90, the grade should be "Pass with distinction".

I have great difficulty with this task. I attached the GradStudent code. Can you find the errors please? I don't fully understand how to override the superclass private instance variables.

public class GradStudent extends Student {

    private int MyGradID;

    public void GradStudent() {
        super();
        MyGradID = 0;
    }

    public void GradStudent(String n, int[] tests, String g, int id) {
        super(n, tests, g);
        MyGradID = id;
    }

    public int getId() {
        return MyGradID;
    }

    @Override public void computeGrade() {
        if (testScores.getTestAverage() >= 65) {
            super.setGrade("Pass");
        } else if (testScores.getTestAverage() > 90) {
            grade = "Pass with distinction";
        }
    }
}

This is my Student class. I'm not sure if I am referencing my super class correctly, so I am adding it. Hopefully you can explain it to me.

public class Student {
    private String name;
    private int[] testScores = new int[3];
    private String grade;

    public Student() {
        name = "";
        grade = "";
        testScores[0] = 0;
        testScores[1] = 0;
        testScores[2] = 0;
    }

    public Student(String n, int[] tests, String g) {
        name = n;
        testScores = tests;
        grade = g;
    }

    public String getName() {
        return name;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String newGrade) {
        grade = newGrade;
    }

    public int getTestAverage() {
        int average = 0;
        int count = 0;
        for (int testScore : testScores) {
            count++;
            average = average + testScore;
        }
        return average / testScores.length;
    }

    public void computeGrade() {
        grade = "Fail";
        if (this.getTestAverage() >= 65) {
            grade = "Pass";
        }
    }
}

Solution

  • What jumps out at me is

        if(testScores.getTestAverage>=65)
    

    Try changing that to

        if(testScores.getTestAverage()>=65)
    

    Easy mistake to make. Hope this helps! Edit - I see that twice.

    Also, I don't think constructors should have a return type void (or any) though I could be wrong.