實現一個班學生。爲了這個練習的目的,一個學生有一個名字和一個總測驗分數。提供適當的構造方法和方法getName()
,addQuiz(int score)
,getTotalScore()
和getAverageScore()
。要計算後者,您還需要存儲學生參加的測驗次數。在哪裏添加java類的成員
...
我有一個特別難的時間與分數和名稱。我是否將分數添加到Student.java
和StudentTester.java
文件或只有測試儀?我無法弄清楚這一點。
這裏是我的代碼:
/** A student has taken a number of quizzes and has an average score
based on the quizzes that were taken.
*/
public class Student
{
private String name;
private double totalscore;
private int numquiz;
}
// Constructs a student object with the name "MccStudent" and with zero total of quiz scores
public Student(String "mccStudent")
{
this.name = studentname;
numquiz = 0;
totalscore = 0;
}
public String getName()
{
return name;
}
// Adds the number of quizzes taken
public void addQuiz(double quizscore)
{
totalscore+=quizscore;
numquiz++;
}
// Returns the total quiz score
public double getTotalScore()
{
return totalscore;
}
// Returns the avaerage grade
public double getAverageScore()
{
return totalscore/numquiz;
}
}
/** Create a class to test the Student class.
*/
public class StudentTester
{
/**
Tests the methods of the Student class.
*/
public static void main(String[] args)
{
// Create an object
Student mccStudent = new Student();
mccStudent.addQuiz(100);
mccStudent.addQuiz(80);
mccStudent.addQuiz(95);
mccStudent.addQuiz(97);
System.out.println(mccStudent.getName());
System.out.println(mccStudent.getTotalScore());
// Display average quiz score
System.out.println(mccStudent.getAverage.Score());
}
}
是您的代碼編譯? – SMA 2015-02-23 09:58:02
沒有自己運行它的一般想法看起來不錯,雖然在這裏,可能會有一些語法錯誤。但我無法弄清楚你在問什麼。是關於在哪裏添加分數?那麼,你做到這一點似乎沒問題。 – yoshi 2015-02-23 09:59:33
是的,主要關於在哪裏添加比分。 – LinaC 2015-02-23 10:09:33