2015-10-20 121 views
3

我有一個構造函數類作爲這麼叫賦值函數方法:從一個單獨的類

public class Student 
{ 

    //declare variables 
    public int studentID; 
    public String courseName; 
    public int courseScore; 
    public char courseGrade; 

    //default constructor 
    public Student() 
    { 

     studentID = 1; 
     courseName = "CIS 2085"; 
     courseScore = 89; 

    } 

//======================================== 

    //constructor with parameters 
    public Student(int id, String course, int score) 
    { 

     setStud(id, course, score); 

    } 

//======================================== 

    public String toString() 
    { 

     return (studentID + " " + courseName + " " + courseScore); 

    } 

//======================================== 

    public void setStud(int id, String course, int score) 
    { 

     studentID = id; 
     courseName = course; 
     courseScore = score; 

    } 

//======================================== 

    public int getStudentID() 
    { 

     return studentID; 

    } 

//======================================== 

    public String getCourseName() 
    { 

     return courseName; 

    } 

//======================================== 

    public int getCourseScore() 
    { 

     return courseScore; 

    } 

//======================================== 

    public char getCourseGrade() 
    { 

     return courseGrade; 

    } 

//======================================== 

    public static char calculateGrade(int courseScore) 
    { 

     if (courseScore >= 94) 
     courseGrade = 'A'; 

     else if ((courseScore >= 85) && (courseScore <= 93)) 
     courseGrade = 'B'; 

     else if ((courseScore >= 75) && (courseScore <= 84)) 
     courseGrade = 'C'; 

     else if ((courseScore >= 65) && (courseScore <= 74)) 
     courseGrade = 'D'; 

     else 
     courseGrade = 'F'; 

     return courseGrade; 

    } 

//======================================== 

} 

,我有我的實現類,到目前爲止,像這樣:

import java.util.*; 

public class StudentImplementation 
{ 

    //allow 'console' to receive input from the user 
    static Scanner console = new Scanner(System.in); 

    //method main 
    public static void main(String[] args) 
    { 

     //declare variables 

     //object 1: first. Uses the default constructor. 
     Student first = new Student(); 

     //System.out.println(first); 

     //object 2: second. 
     Student second = new Student(3, "Advanced JAVA Programming", 83); 

     //System.out.println(second); 

     //use calculateGrade method to receive grade for object 1 

     char grade = Student.calculateGrade(first.getCourseScore); 
     System.out.println(grade); 

    } 

} 

所以我想使用我的calculateGrade方法根據我的對象courseScore輸出適當的分數。

Student類與我的實現類位於不同的文件中。

當我運行這個程序,我收到錯誤:

Exception in thread "main" java.lang.NoSuchMethodError: Student.calculateGrade(I)C 
    at StudentImplementation.main(StudentImplementation.java:27) 

編輯

+0

你能否解釋一下是什麼問題?我不明白什麼是錯的。 – dguay

+0

我不明白你有什麼問題。你不能簡單地調用calculateGrade方法嗎? –

+0

我想調用方法CalculateGrade在我的實現文件中工作。以便它將採用對象1(第一個)的courseScore並給我courseGrade。 – burgoyne

回答

0

怎麼樣

public char CalculateGrade() 
    { 

     if (this.courseScore >= 94) 
     courseGrade = 'A'; 

     else if ((this.courseScore >= 85) && (this.courseScore <= 93)) 
     courseGrade = 'B'; 

     else if ((this.courseScore >= 75) && (this.courseScore <= 84)) 
     courseGrade = 'C'; 

     else if ((this.courseScore >= 65) && (this.courseScore <= 74)) 
     courseGrade = 'D'; 

     else 
     courseGrade = 'F'; 

     g = courseGrade; 

     return g; 

    } 

,並把它作爲

System.out.println(first.CalculateGrade()); 
+0

這是我的錯誤,當我輸入:線程「main」 java.lang.NoSuchMethodError 異常:Student.CalculateGrade()C \t在StudentImplementation.main(StudentImplementation.java:25) – burgoyne

+0

和默認的構造函數,課程成績爲89. – burgoyne

+0

您在哪裏定義了CalculateGrade()方法 – AbtPst

0

創建Student對象後,只需調用你的方法...

Student first = new Student(); 
first.CalculateGrade(); 

,並從CalculateGrade(char gg)刪除g參數,你不需要它。

3

您calculateGrade方法更改爲:

public char calculateGrade() { 
    courseScore = this.getCourseScore(); // this refers to the student from whom you called the calculaGrade() method --> first.calculateGrade() 
    if (courseScore >= 94) { 
     courseGrade = 'A'; 
    } else if ((courseScore >= 85) && (courseScore <= 93)) { 
     courseGrade = 'B'; 
    } else if ((courseScore >= 75) && (courseScore <= 84)) { 
     courseGrade = 'C'; 
    } else if ((courseScore >= 65) && (courseScore <= 74)) { 
     courseGrade = 'D'; 
    } else { 
     courseGrade = 'F'; 
    } 
    return courseGrade; 
} 

就用first.calculateGrade() 並打印結果。

Student first = new Student(); 
char firstStudentGrade = first.calculateGrade(); 
System.out.println(firstStudentGrade); 

PS:方法名稱不以大寫字母開始;)

+0

如果對象已經初始化,那麼它應該給出屬性的正確值。 – AbtPst

+0

線程「main」中的異常java.lang.NoSuchMethodError:Student.calculateGrade()C \t at StudentImplementation.main(StudentImplementation.java:25) 這就是我在嘗試的時候收到的。 – burgoyne

+0

您是否在Student類中將CalculateGrade(char g)方法更改爲calculateGrade()? – Fundhor

3

這是你的學生類的清潔版本,你想要它做的事情。我已經拿出了不必要的變量和方法,並將其清理了一下。

我這樣做的原因是因爲現在,你的代碼包含很多沒有意義的東西(例如,向方法中添加一個參數,以便可以在方法本身中設置參數值,然後返回它,在你的構造函數中使用setter方法)。

public class Student { 
    public int studentID; 
    public String courseName; 
    public int courseScore; 

    public Student() { 
     this(1, "CIS 2085", 89); 
    } 

    public Student(int id, String course, int score) { 
     this.studentID = id; 
     this.courseName = course; 
     this.courseScore = score; 
    } 

    @Override 
    public String toString() { 
     return studentID + " " + courseName + " " + courseScore; 
    } 

    public int getStudentID() { 
     return studentID; 
    } 

    public String getCourseName() { 
     return courseName; 
    } 

    public int getCourseScore() { 
     return courseScore; 
    } 


    public char getCourseGrade() { 
     if (courseScore >= 94) { 
      return 'A'; 
     } else if (courseScore >= 85 && courseScore <= 93) { 
      return 'B'; 
     } else if (courseScore >= 75 && courseScore <= 84) { 
      return 'C'; 
     } else if (courseScore >= 65 && courseScore <= 74) { 
      return 'D'; 
     } 
     return 'E'; 
    } 

} 

下面是一個例子,以實現與附和:

public class StudentImplementation { 

    public static void main(String[] args) { 
     Student first = new Student(); //default constructor 
     System.out.println(first.getCourseGrade()); 
     Student second = new Student(3, "Advanced JAVA Programming", 83); 
     System.out.println(second.getCourseGrade()); 
    } 

} 
+1

謝謝。我想知道這是什麼。函數呢?我從來沒有聽說過。 – burgoyne

+0

這是指調用函數的對象。所以當我們做first.getCourseGrade()時,這指的是'第一個'對象 – AbtPst

+1

'this'指的是對象的當前實例。例如,您的「學生」類,每個「學生」對象包含課程成績,課程名稱和學生ID。這意味着可以有多個'Student'對象的實例,每個實例都有它們自己的值 - 'this'關鍵字有助於Java編譯器知道如何區分它們。如果您需要更多信息,請參閱Oracle的其他文檔:https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html – nbokmans