-3

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";
        }
    }
}
ifly6
  • 5,003
  • 2
  • 24
  • 47
vmjiron
  • 13
  • 1
  • 4

2 Answers2

0

This question simply wouldn't exist if you had an IDE (or, looked at the feedback from compile). I pasted the code into Eclipse and out popped a lot of problems.

Let's look at GradStudent.

public class GradStudent extends Student {

    private int MyGradID;

    // Constructors do not return anything.
    public GradStudent() {
        super();
        MyGradID = 0;
    }

    // Again, constructors do not return anything.
    public GradStudent(String n, int[] tests, String g, int id) {
        super(n, tests, g);
        MyGradID = id;
    }

    public int getId() {
        return MyGradID;
    }

    // Okay. Override annotation in place. Now, computeGrade() needs to get its data from someplace. Fortunately, we
    // have a method in Student to do that for us. That method is called 'getTestAverage()'. You do not need to
    // reference the array created in 'Student'.
    @Override public void computeGrade() {
        int testAverage = getTestAverage();

        if (testAverage >= 65) {    // Evaluate against that testAverage. There is no method (or parameter) associated
                                    // with an array that will compute its average.
            setGrade("Pass");
        } else if (testAverage > 90) {
            setGrade("Pass with distinction");
        }
    }
}

The Student class is just fine (though count in getTestAverage() is a pointless variable which doesn't do anything and an int[] is created with all values being initialised to 0).

Now I would question why the instructor would have told you to create a constructor to set the grade... when you don't actually know what the grade is... but whatever.

Where are you having trouble with the computeGrade() method in Student? It seems fine to me. The problem in GradStudent is fixed given the changes made above.

ifly6
  • 5,003
  • 2
  • 24
  • 47
  • Thank you, but the fixes that you have pointed out are incorrect. The other answer helped me work through the process. " _The issue I was having was how to apply the superclass methods. testScores is of type int[] and getTestAverage() belongs to the Student class. I believe you want super.getTestAverage() instead of testScores.getTestAverage() – _" -Calvin P. Thank you though. – vmjiron Feb 25 '16 at 05:25
  • In `computeGrade()` for `GradStudent`, `int testAverage = getTestAverage();` solves. – ifly6 Feb 25 '16 at 05:28
-1

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.

Ed Cooper
  • 79
  • 7
  • *Also, I don't think constructors should have a return type void (or any) though I could be wrong.* Correct, constructors need no return type. Proper implementation would be `public GradStudent(String n, int[] tests String g, int id)` – Calvin P. Feb 25 '16 at 04:41
  • Thank you, yes that is very helpful. I keep getting the error ./GradStudent.java:25: error: testScores has private access in Student if(testScores.getTestAverage()>=65) how can I reference testScores in gradStudent if it is private? – vmjiron Feb 25 '16 at 04:42
  • Woohoo! I helped. :) – Ed Cooper Feb 25 '16 at 04:49
  • As long as there is a public getter in the same class you should be able to use it even if the variable's private – Ed Cooper Feb 25 '16 at 04:49
  • @vmjiron for starters, `testScores` is of type `int[]` and `getTestAverage()` belongs to the `Student` class. I believe you want `super.getTestAverage()` instead of `testScores.getTestAverage()` – Calvin P. Feb 25 '16 at 05:04
  • That makes a lot of sense. Thank you so much. I actually see what is happening. Yes, it was supposed to be super.getTestAverage(). – vmjiron Feb 25 '16 at 05:15
  • So um could you un-minus one me? It's my second answer ever I hate to start this way lol – Ed Cooper Feb 25 '16 at 05:25
  • Thx :) It gave me an achievement! – Ed Cooper Feb 25 '16 at 05:35