【问题标题】:JAVA Inheritance student, undergrad and gradstudent codeJAVA继承学生、本科和研究生代码
【发布时间】:2016-06-07 16:31:35
【问题描述】:

对于一个课堂项目,我被要求创建三个代码。 学生班

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

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

  3. 另一个构造函数(字符串 n,int[] 测试,字符串 g)- 这将设置 nametestScoresgrade

  4. 三种方法:

    • getName() 返回name
    • getGrade() 返回grade
    • setGrade() 设置grade
    • getTestAverage() 返回测试分数的平均值。
  5. 这是我遇到困难的地方方法computeGrade(),如果平均值大于或等于65,则等级为“及格”。否则就是“失败”。

第二类称为UnderGrad。这个类是Student的子类。

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

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

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

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

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

  3. 我又遇到了挑战。我们必须编写方法 getId() 来返回 ID 号。我们再次需要覆盖computeGrade() 方法,如果平均值大于或等于65,则成绩为“及格”。否则就是“失败”。但如果测试平均分高于 90 分,则成绩应该是“优等生”。

我很难完成这项任务。我附上了GradStudent 代码。请问你能找出错误吗?我不完全理解如何覆盖超类私有实例变量。

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。我不确定我是否正确引用了我的超级类,所以我添加了它。希望你能给我解释一下。

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

【问题讨论】:

  • 请重新格式化您的问题,它很难阅读。
  • 我认为getTestAverage() 在您的Student 课程中没有任何问题,但count 变量没有任何作用。它是递增的,然后从不使用。

标签: java arrays inheritance subclass superclass


【解决方案1】:

如果您有 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 类很好(尽管getTestAverage() 中的count 是一个无意义的变量,它不做任何事情,并且创建了一个int[],所有值都初始化为0)。

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

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

【讨论】:

  • 谢谢,但您指出的修复不正确。另一个答案帮助我完成了整个过程。 “ _我遇到的问题是如何应用超类方法。testScores 是 int[] 类型,getTestAverage() 属于 Student 类。我相信你想要 super.getTestAverage() 而不是 testScores.getTestAverage() – _” -Calvin P。不过,谢谢。
  • computeGrade() 中为GradStudentint testAverage = getTestAverage(); 解决。
【解决方案2】:

让我眼前一亮的是

    if(testScores.getTestAverage>=65)

尝试将其更改为

    if(testScores.getTestAverage()>=65)

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

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

【讨论】:

  • 另外,我认为构造函数不应该有返回类型 void(或任何类型),尽管我可能是错的。 正确,构造函数不需要返回类型。正确的实现是public GradStudent(String n, int[] tests String g, int id)
  • 谢谢,是的,这很有帮助。我不断收到错误 ./GradStudent.java:25: error: testScores has private access in Student if(testScores.getTestAverage()>=65) 如果它是私有的,我如何在 gradStudent 中引用 testScores?
  • 哇哦!我帮助了。 :)
  • 只要在同一个类中有一个公共的getter,即使变量是私有的,你也应该可以使用它
  • @vmjiron 对于初学者来说,testScoresint[] 类型,getTestAverage() 属于 Student 类。我相信你想要super.getTestAverage() 而不是testScores.getTestAverage()
猜你喜欢
  • 2019-07-12
  • 1970-01-01
  • 1970-01-01
  • 2014-04-20
  • 2010-09-22
  • 1970-01-01
  • 2016-06-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多