开发者

Not getting correct output for temperature conversions

开发者 https://www.devze.com 2023-03-26 09:43 出处:网络
Ok so my program compiles and runs perfectly well, but the purpose is to convert between celsius, fahernheit, and Kelvin, and when the program runs with the client, it shows the correct units (F,C,K)

Ok so my program compiles and runs perfectly well, but the purpose is to convert between celsius, fahernheit, and Kelvin, and when the program runs with the client, it shows the correct units (F,C,K) but gives a value of 50 for all temperatures. How do I fix this?

public class Temperature
{
private double value;
private String scale;

public Temperature()  // default constructor
{
  this.value = 0;
  this.scale = "C";
}

public Temperature(double value, String scale)
{
 this.value = value;
 this.scale = scale;     
}

public double getValue()
{
 return this.value;
}

 public String getScale()
 {
 return this.scale;
 }

 public double getCelsius()
 {

 if (scale.equalsIgnoreCase ("C"))
 {
     return this.value;
 }
 else if (scale.equalsIgnoreCase("F"))
 {
     double faren = ((this.value - 32) * (5/9));
     return faren;
 }
 else
 {
     double kelvin = (this.val开发者_运维问答ue - 273.15);
     return kelvin;
 }
 } // end getCelcius

 public double getFaren()
 {
 if (scale.equalsIgnoreCase ("F"))
 {
     return this.value;
 }
 else if (scale.equalsIgnoreCase("C"))
 {
     double celsius = ((this.value * 1.8) + 32);
     return celsius;
 }
 else
 {
     double kelvin = ((this.value * 1.8) - 459.67);
     return kelvin;
 }
 } // ene getFaren

 public double getKelvin()
 {
 if (scale.equalsIgnoreCase("K"))
 {
     return this.value;
 }
 else if (scale.equalsIgnoreCase("C"))
 {
     double celsius = (this.value + 273.15);
     return celsius;
 }
 else
 {
     double faren = ((this.value + 459.67) * (5/9));
     return faren;
 }
 } // end getKelvin

 public void setTemperature(Temperature t)
 {
 this.value = t.value;
 }

 public void setType(double degree, String measure)
 {
 this.value = degree;
 this.scale = measure;
 }

 public void setValue(double degree)
 {
 this.value = degree;
 }

 public void setScale(String measure)
 {
 this.scale = measure;
 }

 public double convertToCelsius()
 {
 this.scale = "C";
 return this.value = getCelsius();
 }

 public double convertToFaren()
 {
 this.scale = "F";
 return this.value = getFaren();
 }

 public double convertToKelvin()
 {
 this.scale = "K";
 return this.value = getKelvin();
 }

 public Temperature clone()
 {
 return new Temperature(this.value, this.scale);
 }

 public boolean equals()
 {
 return this.value == this.value && this.scale == this.scale; 
 }

 public String toString()
 {
 String temps = ("The temperature is " + this.value + "(" + this.scale + ")");
 return temps;
 }

 }


You should change

public double convertToKelvin() {
    this.scale = "K";
    return this.value = getKelvin();
}

to

public double convertToKelvin() {
    this.value = getKelvin()
    this.scale = "K";
    return value;
}

The same for other scales. You are setting the scale first, so the object see that the scale is the same and don't do the conversion.

0

精彩评论

暂无评论...
验证码 换一张
取 消