2017-08-07 25 views
-6
package studentclient; 

import java.util.Scanner; 
public class StudentClient { 

public static void main(String[] args) { 

    /* Declare two object references of type Student s1 and s2 and instantiate two Student objects passing three arguments to the constructor for the class. Use different values for each class object */ 

    Student s1 = new Student("Dick Grayson", "666-66-6666", 3.80); 
    Student s2 = new Student("Jason Todd", "666-77-7777", 3.50); 

    Scanner scan = new Scanner(System.in); 

    /* Output the name, social security number and GPA of the student from object reference s1 using the appropriate accessor methods(Get methods) to obtain the data */ 
    System.out.println("Student Information"); 

    System.out.println("Student Name: " + s1.getName()); 

    System.out.println("Student Social Security Number: " + s1.getSsn()); 

    System.out.println("Student GPA: " + s1.getGpa()); 

    /* Output the name, social security number and GPA of the student from object reference s2 using the toString method to return the data */ 
    System.out.println("Student Information"); 

    System.out.print(s2); 

    /* Using the equals method and a selection control structure (if statement), compare objects s1 and s2 and output an appropriate message indicating if the objects are equal */ 

    if(s1.equals(s2)){ 

    System.out.println("Student's identities are equal!"); 

    }else { 

    System.out.println("Student's identities are not equal!"); 

    } 
    /* Using the appropriate mutator methods(Set Methods) on student object s2, change the name, social security number and GPA to the same values as in object s1. Use the set methods. */ 
     System.out.println("Enter new Student Information for Student 2: "); 

     s2.setName("Dick Grayson"); 
     System.out.println("Student Name: " + s2.getName()); 

     s2.setSsn("666-66-6666"); 
     System.out.println("Student Social Security Number: " + s2.getSsn()); 

     s2.setGpa(3.80); 
     System.out.println("Student GPA: " + s2.getGpa()); 

     /* Again, using the equals method and a selection control structure (if statement), compare objects s1 and s2 and output an appropriate message indicating if the objects are equal */ 
     if(s1.equals(s2)){ 
      System.out.println("Student's identities are equal!"); 
     }else{ 
      System.out.println("Student's identities are not equal!"); 
     } 
    } 
} 

問題是,對於第二組信息s2顯然等於s1,不平等。我該怎麼辦? 當我運行它,我得到這些結果我是編程新手,我不知道是否因爲我的語法而發生此錯誤

學生信息 學生姓名:迪克·格雷森 學生的社會安全號碼:666-66-6666 學生GPA:3.8 學生信息 名稱:傑森·託德 社會安全號碼: 666-77-7777 GPA:3.5 學生的身份不相同! 爲學生2輸入新的學生信息: 學生姓名:Dick Grayson 學生社會安全號碼:666-66-6666 學生GPA:3.8 學生的身份不相同! BUILD SUCCESSFUL(總時間:0秒)

+1

小心妥善格式化您的問題。 [如何問](https://stackoverflow.com/help/how-to-ask) –

+0

也許你想發佈[**最小**,完整和可驗證的例子](https://stackoverflow.com /幫助/ MCVE)。 – khelwood

+3

問題在於學生的等值方法 –

回答

0

您正在使用equals方法與您的自定義對象Student

您需要在您的Student類中覆蓋它。在你的情況,如果你想的s1各個領域匹配s2,您的實現應該是這樣的:

//Overriding equals 
public boolean equals(Object obj) 
    { 
    if (this == obj) return true; 
    if (obj == null) return false; 
    if (this.getClass() != obj.getClass()) return false; 
    Student that = (Student) obj; 
    if (!this.name.equals(that.name)) return false; 
    if (!this.ssn.equals(that.ssn)) return false; 
    if (!this.gpa.equals(that.gpa)) return false; 
    return true; 
    } 
} 

這樣做會給做s1.equals(s2)當您預期的結果。

但是,舉例來說,如果您嘗試將這些相同的對象s1s2添加到Set,它會將它們視爲不同。原因是,兩者的哈希碼將會不同。

因此,當您覆蓋equals時,還應該覆蓋hashcode方法。

它實現示例,考慮你有你的Student類中的一些Id領域是:

@Override 
public int hashCode() 
{ 
    final int PRIME = 31; 
    int result = 1; 
    result = PRIME * result + getId(); 
    return result; 
} 

您應該使用相同的字段來實現這兩個equalshashcode方法。

這樣做後,你的代碼應該給你預期的結果。

希望有幫助!

+1

在'equals'方法中小心使用'getClass'。如果你有一些使用相同比較方法的子類(所以不要重新定義方法),這會失敗,因爲你可以將'Student'與他的一個子類進行比較,即使它們應該是等價的,它也不起作用。 (如果你真的想使用'instanceof') – AxelH

相關問題