所以我应该创建一个程序,将温度设置为摄氏度和华氏度。在其中我创建了一个equals方法。然后我要创建一个运行它的驱动程序,并使用equals方法测试温度以确定它们是否相等。我无法获得正确的布尔值,它只是继续吐出错误。在这里打墙。代码如下:
公共课温度 { 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
答案 0 :(得分:0)
所以我看到了几个问题。首先,在你的实施中 getTemperatureInCelsius()
if(this.getScale() == Scale.FARENHEIT)
应该是,我认为,
if(this.getScale() == Scale.CELSIUS)
,反之亦然,以实现getTemperatureInFarenheit。
一旦这些问题得到解决,我认为你的平等主要问题是坚持规模是一样的。你在哪里
if((this.getTemperature()== otherTemperature.getTemperature())
&&(this.getScale()== otherTemperature.getScale()))
应该是
if(this.getTemperatureInFarenheit()== otherTemperature.getTemperatureInFahrenheit())
我故意在Farenheit中进行比较,以避免在摄氏转换因子中进行舍入。这可能会使equals工作(一旦我为getTemperatureInFarenheit提到了修复)。
我仍然认为我会更喜欢说像
这样的话if(Math.abs(this.getTemperatureInFarenheit()-otherTemperature.getTemperatureInFahrenheit())<0.001)
比我上面的建议,如果上述失败,我会尝试。