2015-02-23 98 views
0

實現一個班學生。爲了這個練習的目的,一個學生有一個名字和一個總測驗分數。提供適當的構造方法和方法getName(),addQuiz(int score),getTotalScore()getAverageScore()。要計算後者,您還需要存儲學生參加的測驗次數。在哪裏添加java類的成員

...

我有一個特別難的時間與分數和名稱。我是否將分數添加到Student.javaStudentTester.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()); 
    } 

}​ 
+0

是您的代碼編譯? – SMA 2015-02-23 09:58:02

+0

沒有自己運行它的一般想法看起來不錯,雖然在這裏,可能會有一些語法錯誤。但我無法弄清楚你在問什麼。是關於在哪裏添加分數?那麼,你做到這一點似乎沒問題。 – yoshi 2015-02-23 09:59:33

+0

是的,主要關於在哪裏添加比分。 – LinaC 2015-02-23 10:09:33

回答

1

首先嚐試瞭解構造函數是什麼。這裏是有很好的例子的oracle文檔:Constructor。我爲你寫一個簡單的例子。學生是具有字符串名稱屬性的新對象。在Student.java

public Student(String name,double totalscore,int numquiz) 
{ 
    this.name = studentname; 
    this.numquiz = numquiz; 
    this.totalscore = totalscore; 
} 

public class Student { 

    public String name; //name of student 

    public Student(String name) {//Constructor for student, receiving name when u create new object Student 
    this.name = name; //set received name to this public String name 
    } 


    /** 
    * When u call this method you will get inputed name from constructor 
    * so if u call Student stud = new Student("John"); 
    * new Student("John") is constructor! 
    * with stud.getName(); you will get "John". 
    * This is called getter. 
    * @return name of student 
    */ 
    public String getName() { 
     return name; 
    } 
} 
+0

非常感謝,我會更努力! – LinaC 2015-02-23 10:18:59

0

有一些重大問題在這裏,對於一個第一支柱是聲明你的實例變量後關閉,所以對於學生的代碼的其餘部分是超出範圍。刪除第一個大括號,這應該有所幫助。

另一個問題是你使用你的構造函數的方式(說公共學生的部分(String「mccstudents」))。你需要在那裏提供一個變量名,然後每當你創建一個新的對象時,你傳入一個字符串,這將取代變量名。

不是聽起來像你的教導之一,你真的不應該離開這個到最後一分鐘爲了跳過任何數量的人誰將寫回復這個的槍,這個網站的目的是幫助具體問題,而不是分析整個程序。

是否有特定的問題和概念,你不理解我可以幫忙?

+0

我解決了,謝謝。現在我只有2個錯誤,而不是超過20個。 我知道,我很抱歉。我一直在研究它幾天,但我很難理解它。我看了一些教程,以便更好地理解它,但由於工作和其他課程,我幾乎沒有足夠的時間來吸收我學習的內容。再次抱歉。 賦值是否告訴我只能使用「getName」,「addQuiz」,「getTotalScore」和「getAverageScore」,還是使用諸如「numquiz」,「totalscore」之類的東西? – LinaC 2015-02-23 10:06:53

+0

只要按照賦值指定的方式更改函數的名稱就沒有任何問題。將函數名稱添加到函數名稱的開頭是一種常用於這些函數類型(稱爲訪問器或getter)的約定。我認爲所有的任務都是要求你爲這些方法添加功能(你似乎正處在正確的軌道上),如果你瞭解如何構造代碼,這不應該花太長時間。善於修復錯誤,繼續努力! – Stevo 2015-02-23 10:16:20

+0

這不提供問題的答案。要批評或要求作者澄清,在他們的帖子下留下評論 - 你總是可以評論你自己的帖子,一旦你有足夠的[聲譽](http://stackoverflow.com/help/whats-reputation),你會能夠[評論任何帖子](http://stackoverflow.com/help/privileges/comment)。 – 2015-02-23 10:41:24

0

您將這些字段添加到類本身(Student.java)。

只有執行測試的代碼纔會出現在測試類(StudentTester.java)中。

0

第一個正確構造你可以簡單地Student.java的對象StudentTester.java。 ,然後通過構造函數值傳遞這樣的:

Student student = new Student("foo", 5 , 80); 

現在你可以用學生對象執行Student.java類的任何功能。 例如:

student.addQuiz(100); 
+0

非常感謝! – LinaC 2015-02-23 10:19:17