public class Student {
private String name;
private long id;
private String grade;
private int[] test;
private int NUM_TESTS;
public Student(){
name="Un";
id=0;
grade="Un";
test=new int[0];
NUM_TESTS=5;
}
public Student(String x, long z) {
name=x;
id=z;
}
public void setName(String n) {
name=n;
}
public void setID(long i) {
id=i;
}
public void setGrade(String g) {
grade=g;
}
/*public void setTestScore(int t,int s) {
test=t;
test=s;
}
public int getTestScore(int) {
return test;
}*/
public int getNumTests() {
return NUM_TESTS;
}
public String getName() {
return name;
}
public long getID() {
return id;
}
public String getGrade() {
return grade;
}
public String toString() {
return getTestScore()+getNumTests()+getName()+getID()+getGrade();
}
/*public void calculateResult() {
int sum=0;
for (int t:test)sum+=t;
double average= 1.0t*sum/5;*/
}
}
这是我的代码,我将出现问题的地方隔开了。我正在写一个学生子类,其中子类包括本科和研究生。 Here is the UML 如果它不是变量之一,我不明白如何正确实现testScore?没关系,我自己去解决计算结果。我也不确定我的构造函数是否正确。所有的学生都参加五门考试,这是一个常数
答案 0 :(得分:0)
首先,似乎Student
类应该是abstract
。因为每个学生都是UnderGraduate
或PostGraduate
。
第二,您应该extend
类的子类Student
。
我希望以下代码对您有所帮助:
abstract class Student {
private String name;
private long id;
private String grade;
private int[] test;
private final int NUM_TESTS = 5;
public Student(){
name = "UN";
id = 0;
grade = "UN";
test = new int[NUM_TESTS];
}
public Student(String name, long id){
this.name = name;
this.id = id;
}
@Override
public String toString() {
//TODO: write your desire toString method
return getNUM_TESTS()+getName()+getId()+getGrade();
}
abstract void claculateResult();
public int getTestScore(int testNumber){
if(testNumber >= NUM_TESTS)
return 0;
return test[testNumber];
}
public void setTestScore(int testNumber, int score){
if(testNumber >= NUM_TESTS)
return;
test[testNumber] = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public int[] getTest() {
return test;
}
public void setTest(int[] test) {
this.test = test;
}
public int getNUM_TESTS() {
return NUM_TESTS;
}
}
和UnderGraduate
类将是:
public class UnderGraduate extends Student{
public UnderGraduate(){
}
public UnderGraduate(String name, long id){
super();
}
@Override
void claculateResult() {
//TODO: DO whatever you want
}
}
请记住,PostGraduate
类与UnderGraduate
相同。
答案 1 :(得分:0)
setTestScore(int t, int s)
...我建议您使用仔细选择的名称(标识符)。例如,如果您仅将参数重命名为:setTestScore(int testNumber, int score)
,您会更加熟悉应该补充什么。test = new int[0];
不是您想要的。您想要test = new int[NUM_TESTS]
setTestScore(int testNumber, int score)
第一个参数实际上是test
数组中的索引,第二个参数是值。因此,您的方法应如下所示:
public void setTestScore(int testNumber, int score) {
test[testNumber] = score;
}
我刚刚为您提供了一些实施指南...