所以我應該創建一個程序,將溫度設置爲攝氏和華氏。在它裏面我創建了一個equals方法。然後,我將創建一個驅動程序來運行它,並使用equals方法測試溫度以查看它們是否相等。我無法得到正確的布爾值,它只是繼續吐出假。在這裏打牆。代碼如下:無法讓我的布爾值工作
public class溫度 { public enum Scale {CELSIUS,FARENHEIT};
public static final double DEFAULT_DEGREES = 0.0;
private double temperature;
private Scale scale;
public Temperature()
{
this(Temperature.DEFAULT_DEGREES);
}//default
public Temperature(double newTemperature)
{
this(newTemperature, Scale.CELSIUS);
}//ending bracket of constructor double
public Temperature(Scale newScale)
{
this(0, newScale);
}//end of constructor scale
public Temperature(double newTemperature, Scale newScale)
{
this.setTemperature(newTemperature);
this.setScale(newScale);
}//end bracket of constructor degrees and scale
public Scale getScale()
{
return this.scale;
}//end of method getScale
public void setScale(Scale newScale)
{
this.scale=newScale;
}//end of method setScale
public double getTemperature()
{
return this.temperature;
}//ending bracket of metho getTemperature
public void setTemperature(double newTemperature)
{
if(newTemperature < Temperature.DEFAULT_DEGREES)
{
this.temperature=Temperature.DEFAULT_DEGREES;
}
else
{
this.temperature = newTemperature;
}//ending of if
}//ending bracket of method setTemperature
public double getTemperatureInFarenheit()
{
double rv;
if(this.getScale() == Scale.CELSIUS)
{
rv= this.getTemperature();
}
else
{
rv = ((this.getTemperature()* 1.8) + 32);
}//end of if
return rv;
}//end of bracket of method getweightinfarenheit
public double getTemperatureInCelsius()
{
double rv;
if(this.getScale() == Scale.FARENHEIT)
{
rv = this.getTemperature();
}
else
{
rv= ((this.getTemperature()- 32) * 0.5556);
}
return rv;
}//end of method gettemperatureincelsius
public boolean equals(Object otherObject)
{
boolean rv = false;
if(otherObject instanceof Temperature)
{
Temperature otherTemperature = (Temperature)otherObject;
if((this.getTemperature()== otherTemperature.getTemperature())
&&(this.getScale()== otherTemperature.getScale()))
{
rv = true;
}//end of nested if
}//end of if
return rv;
}//end of bracket method equals
} //
類這的結尾是我的司機:
公共類TemperatureDriver {
public static void main(String[]args)
{
Temperature w1 = new Temperature();
w1.setTemperature(32);
w1.setScale(Temperature.Scale.FARENHEIT);
Temperature w2 = new Temperature();
w2.setTemperature(0);
w2.setScale(Temperature.Scale.CELSIUS);
System.out.println(w1.equals(w2));
Temperature w3 = new Temperature();
w3.setTemperature(-40.0);
w3.setScale(Temperature.Scale.FARENHEIT);
Temperature w4 = new Temperature();
w4.setTemperature(-40.0);
w4.setScale(Temperature.Scale.CELSIUS);
System.out.println(w3.equals(w4));
Temperature w5 = new Temperature();
w5.setTemperature(212);
w5.setScale(Temperature.Scale.FARENHEIT);
Temperature w6 = new Temperature();
w6.setTemperature(100);
w6.setScale(Temperature.Scale.CELSIUS);
System.out.println(w5.equals(w6));
}//end of method main
} //類的結束Temperaturedriver
而不是在使用十進制近似值進行轉換時測試是否相等,可能更有意義的是查看兩件事物的差異是否小於epsilon。另外,我發現GetTemperatureinCelsius令人困惑,因爲它在標度爲攝氏但不是華氏時轉換。這是倒退嗎? –
不幸的是,我所擁有的教授堅持我們在進行轉換時測試平等。至於getTemperatureInCelsius的問題,如果你看上面那些...在它上面的InFarenheit中,我應該把它們作爲一個塊並且使它成爲if else語句嗎? – Thalazon
所以我認爲1.8會起作用(儘管我可能更喜歡乘以9併除以5),但是我認爲你需要* 5並且在你有0.5556的情況下除以9,因爲在其他情況下它們幾乎是相等的。不過,我對平等測試沒有任何問題。我建議你通過測試溫度差的絕對值是否小於epsioln而不是在實數上使用==來實現等式。 –