2017-02-06 18 views
1
package restaurantclient; 

public class Restaurant extends Store { 

//Instance Variables 
private int peopleServed; 
private double averagePrice; 

//Constructor with 3 parameters 
public Restaurant(String storename, int peopleServed, double averagePrice) { 
super(storename); 
setPeopleServed(peopleServed); 
setAveragePrice(averagePrice); 
} 

//Getters (Accessors) 
public int getPeopleServed() { 
return peopleServed; 
} 

public double getAveragePrice() { 
return averagePrice; 
} 

//Setters (Mutators) 
public void setPeopleServed(int peopleServed) { 
this.peopleServed = peopleServed; 
} 

public void setAveragePrice(double averagePrice) { 
this.averagePrice = averagePrice; 
} 

//toString Method [Must Override] 
@Override 
public String toString() { 
    String information = "Store name: " + (super.getName()); 
    information += "\n" + "The number of people served: " + peopleServed; 
    information += "\n" + "The average price per person: $" + averagePrice; 

    return information; 
    } 

//Equals Method 
@Override 
public boolean equals (Object other) { 

    if (this == other) 
     return true; 
    if (other == null) 
     return false; 
    if (!(other instanceof Restaurant)) 
     return false; 

    Restaurant otherRestaurant = (Restaurant) other; 

    if (this.getName() == null) { 
     if (otherRestaurant.getName() != null) 
      return false; 
    } else if (!(this.getName().equals(otherRestaurant.getName()))) 
      return false; 

    if (peopleServed == -1) { 
     if (otherRestaurant.peopleServed != -1) 
      return false; 
    } else if (peopleServed != (otherRestaurant.peopleServed)) 
      return false; 

    if (averagePrice == -1) { 
     if (otherRestaurant.averagePrice != -1) 
      return false; 
    } 
     else if (averagePrice != (otherRestaurant.averagePrice)) 
      return false; 

    return true; 
} 


public double getAverageTaxes() { 
double total; 
total = this.getPeopleServed() * this.getAveragePrice() 
* super.CA_TAX_RATE; 
return total; 
} 
} 


    package restaurantclient; 


public class Store { 

//Instance Variables 
protected final double CA_TAX_RATE = 0.0884; 
private String storename; 

//Constructor 
public Store(String storename) { 
    setName(storename); 
    } 

//Getters (Accessors) 
public String getName() { 
    return storename; 
    } 

//Setters (Mutators) 
public void setName(String storename) { 
    this.storename = storename; 
    } 

//toString Method [Must Override] 
@Override 
public String toString() { 
    String directory = "Name of store: " + storename; 

    return directory; 
    } 

//Equals Method 
public boolean equals (Store storename) { 

    if (this == storename) 
     return true; 
    if (storename == null) 
     return false; 
    if (!(storename instanceof Store)) 
     return false; 

    return true; 

    } 

} 

以上是equals方法我打電話方法不行爲。它們顯示錯誤的答案:它應該是在一審,「他們是不是等於」,並在設定的一切彼此相等後的第二個實例,它應該顯示,「他們都是平等的」。我非常努力地解決這個問題,許多事情都沒有奏效。沒有明顯的錯誤它運行良好,但我做錯了什麼,一些精確的指導將有很大的幫助。大部分含糊的提示讓我無處可尋。如果這對你有幫助,我需要一些具體的東西。再次感謝您的幫助。以下是客戶端類:的Java等於預期

package restaurantclient; 

public class RestaurantClient { 
public static void main(String[] args) { 
    Restaurant r1 = new Restaurant("McDonald's", 1000000, 8.00); 
    Restaurant r2 = new Restaurant("KFC", 500000, 6.00); 

    System.out.println(r1.toString()); 
    System.out.println(r2.toString()); 
    System.out.println(); 

    r2.setAveragePrice(r1.getAveragePrice()); 
    r2.setPeopleServed(r1.getPeopleServed()); 

    System.out.println(r1.toString()); 
    System.out.println(r2.toString()); 

    if (r1.equals(r2)) { 
     System.out.println("The objects are equal."); 
     } 
    else { 
     System.out.println("The objects are not equal."); //SHOULD say "not equal" here EVERY TIME the second instance (next comment down) says "Equal"...this should never change. 
     System.out.println(); 
     } 

    System.out.println(); 
    r2.setName(r1.getName()); 

    System.out.println(r1.toString()); 
    System.out.println(r2.toString()); 

    if (r1.equals(r2)) { 
     System.out.println("The objects are equal."); //Now that everything is equal, it should print "The Objects are Equal" but it doesn't. It's in lock-step with the previous instance. Changing some things like return true to return false might make both these instances "Are equal" and some might change them to "Not Equal" but they are never the way I want them, which is when 2 changes are made, they are not equal (first case) and when the third and final change is made (like this case here on this line) it should say "the obj are equal" but it doesn't. 
    } 
    else { 
     System.out.println("The objects are not equal."); 
     System.out.println(); 
    }  

    System.out.println(); 
    System.out.print("The avg. annual taxes paid by the restaurant is: $"); 
    System.out.println(r1.getAverageTaxes()); 
    } 
} 
+0

什麼是輸入輸出?第一眼看起來很好! –

+0

等於方法使用的名稱,人們服務和平均價格...與該標準mcDonalds和kfc必須在等於方法返回false ... –

+2

是否有你使用'super.getName()'的原因? – AxelH

回答

1

,我看到的原因很簡單,你沒有得到相同的name

在平等,你與otherRestaurant.getName()

比較super.getName()如果餐廳的超有不同的格式或返回其他變量,因爲你把它比作Restaurant.getName(),這樣會比較不同的價值。使用this.getName()比較相同的變量(或變量的形式)更安全。 即使Restaurant.getName()只返回super.getName(),如果你改變了餐廳的方法,這將是安全的(因爲你喜歡它的其他方式)。

下面是一個例子:

餐廳:

public String getName(){ 
    return "A restaurant " + name; 
} 

超類:

public String getName(){ 
    return name; 
} 

將導致與"KFV"比較"A restaurant : KFC"

使用相同的getter向你保證返回相同的「格式」。


另外,您的邏輯錯誤。你想檢查一個值是否不同,如果是,return false。如果你達到了方法的結尾,那意味着沒有區別導致return false,你return true

if (this.getName() == null) { 
    if (otherRestaurant.getName() != null) 
     return false; 
} else if (!super.getName().equals(otherRestaurant.getName())) // added ! here 
     return false; 

if (peopleServed == -1) { 
    if (otherRestaurant.peopleServed != -1) 
     return false; 
} else if (peopleServed != (otherRestaurant.peopleServed)) // change to != here 
     return false; 

if (averagePrice == -1) { 
    if (otherRestaurant.averagePrice != -1) 
     return false; 
} 
    else if (averagePrice != (otherRestaurant.averagePrice)) // change to != here 
     return false; 

//No differences, then it is equals. 
return true; 

注:

這種情況可以縮短

if (averagePrice == -1) { 
    if (otherRestaurant.averagePrice != -1) 
     return false; 
} 
    else if (averagePrice != (otherRestaurant.averagePrice)) // change to != here 
     return false; 

既然是做同樣的事情(比較值):

if (averagePrice != (otherRestaurant.averagePrice)) 
    return false; 

編輯:

您有重寫問題。

在商店:

public boolean equals(Store s){} 

而且在餐廳

public boolean equals(Object o){} 

既然你調用一個Restaurant(的Store子類)的方法,JVM將使用Store.equals方法,因爲它匹配類型,Restaurant.equals不重寫它,它覆蓋Object中的方法。更改爲Store.equals(Object o)以更正此問題。

的方法equals來自Object所以應該始終接收Object,以防止任何問題,像這樣的,如果你指定的方法類型,將無法正確覆蓋的方法(根據類型)

+0

我將'otherRestaurant.getName()'更改爲'this.getName()',但問題仍然存在。我在「餐廳」類的equals方法內的兩個地方改變了它。 – SarahSanchez

+0

謝謝你和我住在一起,@AxelH,我必須承認,我確實遵循了Soumik的建議,並清理了干擾素的位置以及什麼。但是,這與您的建議一起尚未解決問題。我將更新我的代碼以反映我立即完成的工作。再次感謝。 – SarahSanchez

+0

我需要在我的課堂上有它(超級)......有沒有辦法解決這個問題超級?非常感謝! :) – SarahSanchez

0

似乎您檢查相等性,然後返回false,當您應檢查不等於返回false。

else if (!super.getName().equals(otherRestaurant.getName())) 
      return false; 

else if (peopleServed != (otherRestaurant.peopleServed)) 
      return false; 

else if (averagePrice != (otherRestaurant.averagePrice)) 
      return false; 

另外,按照要求,任何使用super.getName()的理由?

而且由於peopleServed & averagePrice不能爲空,不需要-1支票預期的結果,我們是一樣的平等檢查

最後,我猜到底回報應該是真實的,因爲它意味着它是一個對象的不同實例,但它們具有所有相同的屬性。

+0

super是我在構造函數中有一個約束,我需要使用它作爲參數。我按照你所說的設置了所有其他的東西,但即使它們在最後返回true,我仍然會得到不正確的顯示。 – SarahSanchez

+0

那麼你可以在你的測試用例中詳細說明你認爲它是真的嗎? – TheBakker

+0

細節。謝謝。 – SarahSanchez

0

在您equals()方法,如果super.name()等於otherRestaurant.name()你不應該回到true,在這裏:

else if (super.getName().equals(otherRestaurant.getName())) return false;

+0

我改變了!爲了解釋這一點,但它仍然出錯。 – SarahSanchez

+0

你可以請發佈輸出,即你正在得到的確切答案。還有誰是餐廳的超級類,並且它是否也實現了平等?如果你發佈了這個,也會有所幫助。 –

+0

如果'Store'是超類,那麼'equals'方法的實現是錯誤的。這是假設在if(this == other)返回true後面的意圖;'Restaurant.equals()'內的意圖是調用super檢查是否相等。目前'超'總是返回false,所以它永遠不會開始檢查名稱等點 –

-1

好吧,這一個在任何情況下工作:

@Override 
    public boolean equals (Object other) { 

     if (this == other) 
      return true; 
     if (other == null) 
      return false; 
     if (!(other instanceof Restaurant)) 
      return false; 

     Restaurant otherRestaurant = (Restaurant) other; 

     if (name == null) { 
      if (otherRestaurant.getName() != null) 
       return false; 
     } else if (name!=otherRestaurant.getName()) 
      return false; 

     if (peopleServed == -1) { 
      if (otherRestaurant.peopleServed != -1) 
       return false; 
     } else if (peopleServed != otherRestaurant.peopleServed) 
      return false; 

     if (averagePrice == -1) { 
      if (otherRestaurant.averagePrice != -1) 
       return false; 
     } 
     else if (averagePrice != otherRestaurant.averagePrice) 
      return false; 

     return true; 
    } 

檢查,並回應,如果它是確定

+0

我需要使用超級作爲構造函數內的參數。謝謝你的幫助,雖然Zheka。 – SarahSanchez

+0

所以改變它: _} else if(super.equals(otherRestaurant.getName()))_ –

+0

我改變了它,但仍然有一個錯誤:「商店名稱」是私人的。 – SarahSanchez