JAVA继承学生、本科生和研究生代码

JAVA Inheritance student, undergrad and gradstudent code

对于一个 class 项目,我被要求创建三个代码。 学生Class

  1. 首先,学生class包含三个参数。

    • 姓名(字符串)
    • TestScores(整数数组),
    • 年级(字符串)。
  2. 一个空的构造函数。将名称和等级设置为空字符串。 TestScores 数组将用三个零初始化。

  3. Another Constructor(String n, int[] tests, String g)- 这将设置 nametestScoresgrade

  4. 三种方法:

    • getName() 到 return name
    • getGrade()到returngrade
    • setGrade() 设置 grade
    • getTestAverage() 到 return 考试成绩的平均值。
  5. 这是我难点所在方法computeGrade(),如果平均分大于或等于65,成绩是一个"Pass"。否则就是一个"Fail"。

第二个class叫做UnderGrad。这个 class 是 Student 的子class。

  1. 我们必须创建一个空的构造函数和另一个构造函数 (String n, int[] tests, String g)。

  2. 我们被指示覆盖 computeGrade() 方法,以便 UnderGrad() 学生必须获得 70 或更高的分数才能通过。

第三个class是Student的GradStudent一个子class。

  1. 我们必须创建 1 个实例变量,int MyGradID,和一个调用 super 的空构造函数,并将 ID 设置为 0。

  2. 还有一个构造函数(String n, int[] tests, String g, int id)-记得调用超级构造函数并设置ID。

  3. 我又遇到了挑战。 我们必须将方法 getId() 写到 return ID 号。同样,我们需要重写 computeGrade() 方法并且,如果平均值大于或等于 65,则成绩为 "Pass"。否则就是一个"Fail"。但如果平均分高于90分,成绩应该是"Pass with distinction".

我很难完成这个任务。我附上了 GradStudent 代码。你能找到错误吗?我不完全明白如何覆盖 superclass 私有实例变量。

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";
        }
    }
}

这是我的Student class。我不确定我是否正确引用了我的 super class,所以我添加了它。希望你能给我解释一下。

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";
        }
    }
}

我突然想到的是

    if(testScores.getTestAverage>=65)

尝试将其更改为

    if(testScores.getTestAverage()>=65)

容易犯的错误。希望这可以帮助! 编辑 - 我看到了两次。

另外,我 认为 构造函数不应该有一个 return 类型 void(或任何),尽管我可能是错的。

如果你有IDE(或者,查看编译的反馈),这个问题就不会存在了。我将代码粘贴到 Eclipse 中,但出现了很多问题。

让我们看看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");
        }
    }
}

Student class 很好(尽管 getTestAverage() 中的 count 是一个无意义的变量,它什么都不做, int[] 是创建时所有值都初始化为 0)。

现在我会质疑为什么讲师会告诉你创建一个构造函数来设置成绩......当你实际上不知道成绩是什么时......但无论如何。

Student 中的 computeGrade() 方法哪里有问题?对我来说似乎很好。 GradStudent 中的问题已根据上述更改得到修复。