【发布时间】:2016-11-27 03:15:35
【问题描述】:
所以我应该创建一个将温度设置为摄氏度和华氏度的程序。在其中我创建了一个 equals 方法。然后我将创建一个驱动程序来运行它并使用 equals 方法测试温度以查看它们是否相等。我无法获得正确的布尔值,它只是不断吐出错误。在这里撞墙。代码如下:
公共课温度 { 公共枚举标度{ 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
}//温度驱动类结束
【问题讨论】:
-
当您使用十进制近似值进行转换时,与其测试相等性,不如看看两个事物的差异是否小于 epsilon。此外,我发现 GetTemperatureinCelsius 令人费解,因为它会在标度为摄氏度而不是华氏度时进行转换。这是倒退吗?
-
不幸的是,我的教授坚持在进行转换时测试是否相等。至于 getTemperatureInCelsius 的问题,如果你看上面有 ...InFarenheit ,我应该把它们放在一个块中,让它成为一个 if else 语句吗?
-
所以我认为 1.8 会起作用(尽管我可能更喜欢乘以 9 并除以 5),但我认为你需要 *5 并除以 9 得到 0.5556,因为事情会否则只能几乎相等。不过,我对平等测试没有任何问题。我建议您通过测试温差的绝对值是否小于 epsioln 来实现您的等式,而不是在实数上使用 ==。
-
另外,getTemperatureIn 的两个函数也没有问题。困扰我的是,在我看来,当规模是所需的规模时,你正在转换,而当你应该转换时,你就不管了。也就是说,如果您的刻度是摄氏度,并且您想以摄氏度为单位,您应该返回温度。
-
哦,但是你的 equals 有一个更大的问题。它坚持认为,两个温度必须具有相同的标度。你想说的是,有getTemperatureinCelsius是一样的。
标签: boolean driver equals temperature