java 温度转换器的基本访问方法

java basic accessor methods for temperature converter

我的 objective 是创建一个 class "Temperature" 来表示摄氏度和华氏度的温度。 class 需要以下四个构造函数。我需要帮助的部分是两种访问器方法,因为我还不太熟悉它。我已经编写了代码,但不确定它是否会成功 id 感谢您的一些见解

四个构造器: 1.一个为度数 2.一个为规模 3. 一个用于度数和规模 4.默认构造函数

两种访问器方法:

  1. 一比 return 摄氏温度
  2. 另一个 return 华氏度

w/ 下面给出的公式 C = 5 ( F – 32) / 9 F = 9 * C/5 + 32

从我目前的设置方式来看,我相信我正准备只从摄氏度转换为华氏度...我怎样才能让它互换

请不要嫌弃我是新手,可能会有致命错误

package temperatureapparatus;

public class Temperature {

    private float degrees;
    char scale;
    public static final float fahrenheitForm = ((9.0*(degrees/5.0))+32.0);

    public Temperature(){
        degrees = 0;
        scale = 'C';
    }

    public Temperature(float degrees){
        this.degrees = degrees;
        degrees = 0;
    }

    public Temperature(char scale){
        this.scale = scale;
        scale = 'C';          
    }

    public Temperature(float degrees, char scale){
        this.degrees = degrees;
        this.scale = scale;      
    }

    public float getTempCels (float degrees, char scale){
        return degrees;
    }

    public float getTempFehr (float degrees, char scale){
        return fahrenheitForm;
    }                
}

您需要将 farenheitForm 移动到访问器方法中。如果它是一个私有字段,它只会在 class if 构造时被初始化并保持在 32.

public float getTempFehr (float degrees, char scale){
    return (9 * degrees / 5f) + 32;
}

from the way i have it setup currently i BELIEVE I'm gearing towards converting from celsius to fahrenheit ONLY ... how could i make it interchangeable

如果你想用度数或华氏度来实例化 class 你应该修改你的第二个构造函数来使用一个标志来表示传入的度量;或者创建另一个采用 double 参数而不是浮点数的构造函数来表示华氏度形式。一旦进入构造函数,您就可以将华氏度值转换为度数并将其存储在私有字段中。

public Temperature(double farenheit){
    degrees = 5 * (farenheit - 32) / 9;
}

在这一点上,添加另一个构造函数来获取华氏值和比例也是值得的——以便与等效度数构造函数保持一致。

这可能不是您正在寻找的答案,但我认为构造函数不应该这样设置。另外,我认为您应该针对接口进行编程。

此外,java 对数字有点不适应。我第一次尝试编译你的代码时,我得到:

 incompatible types: possible lossy conversion from double to float

在我对所有内容进行类型转换之前,使用那些硬编码转换的任何内容都是浮动的。

如果我要以不同的方式执行此操作,我将编程为某种具有明确定义的访问器方法的接口。对于您当前的构造函数,它的使用方式非常不清楚,而且在 IMO 中很危险。

我会像这样设置一个界面:

package temp;

public interface TemperatureInterface
{
        // Sets internal degrees to celcius
        public void setCelcius(float degreesCelcius);
        // Sets internal degrees to fahrenheit
        public void setFahrenheit(float degreesFehrenheit);
        // Gets internal degrees in celcius
        public float getDegreesCelcius();
        // Gets internal degrees in fahrenheit
        public float getDegreesFahrenheit();
}

像这样的实现:

package temp;

public class Temperature 
        implements TemperatureInterface 
{
        private boolean temperatureSet;
        private float celciusInternal;
        private float _convertToCelcius(float degreesFehrenheit)
        {   
                return (((float)9.0*((float)degreesFehrenheit/(float)5.0))+(float)32.0); 
        }   
        private float _convertToFehrenheit(float degreesCelcius)
        {   
                return (((float)degreesCelcius*(float)1.8)+(float)32.0); 
        }   
        public Temperature() {
                this.temperatureSet = false;
        }   
        // Sets internal degrees to celcius
        public void setCelcius(float degreesCelcius)
        {   
                this.temperatureSet = true;
                // We need to set the internal
                // degrees in celcius, so just
                // set it.
                this.celciusInternal = degreesCelcius;
        }   
        // Sets internal degrees to fahrenheit
        public void setFahrenheit(float degreesFehrenheit)
        {   
                this.temperatureSet = true;
                // We need to set the internal
                // degrees in celcius, so first 
                // convert it.
                float degreesCelcius = this._convertToCelcius(degreesFehrenheit);
                this.celciusInternal = degreesCelcius; 
        }   
        // Gets internal degrees in celcius
        public float getDegreesCelcius()
        {   
                // First make sure the temperature
                // has been set.
                if (this.temperatureSet == false) {
                        System.out.println("Error: no temperature set.");
                        System.exit(1);
                }   
                // We already have degrees celcius,
                // so just give the value back.
                return this.celciusInternal;
        }  
        // Gets internal degrees in fahrenheit
        public float getDegreesFahrenheit()
        {   
                // First make sure the temperature
                // has been set.
                if (this.temperatureSet == false) {
                        System.out.println("Error: no temperature set.");
                        System.exit(1);
                }   
                // First we have to convert the
                // internal degrees celcius.
                float celcius = this._convertToFehrenheit(this.celciusInternal);
                return celcius;
        }
}

注意我先写了界面,你只要知道那个界面就知道如何使用class。通常我会抛出异常而不是 System.ou.println("ERROR THING"),但现在这并不重要。

无论如何,这可能对您的作业问题没有帮助,但我认为应该这样写 class。

以下是您代码的注释正确版本。但我不明白为什么你有实例变量,因为你从未使用过它们。在您的情况下,构造函数也是无关紧要的。对于优雅的代码,您可以遵循 Alexander Kleinhans 的编码实现,或者您可以将您的转换方法(即 getTempCels 和 getTempFehr)标记为静态,并且您不必创建实例来使用这些方法。您可以查看 this link 以了解静态方法与非静态方法。

public class Temperature {

    private float degrees;
    char scale;


    public Temperature(){
        degrees = 0;
        scale = 'C';
    }
    public Temperature(float degrees){
        this.degrees = degrees;
        scale = 'C';
    }

    public Temperature(char scale){
        // change the given scale to uppercase
        scale = Character.toUpperCase(scale);
        this.scale = 'C';
        // if the given scale is 'F' then change the instance variable scale (this.scale) to scale passed in parameter
        if(scale == 'F'){
            this.scale = scale;
        }
        this.degrees = 0;

    }
    public Temperature(float degrees, char scale){
        scale = Character.toUpperCase(scale);
        this.scale = 'C';
        if(scale == 'F'){
            this.scale = scale;
        }
        //set instance variable degrees (this.degrees) to the degrees passed in parameter
        this.degrees = degrees;  
    }

    public float getTempCels (float degrees, char scale){
        scale = Character.toUpperCase(scale);

        // if the given scale is celsius then just return whatever was passed in parameter
        if(scale == 'C'){
            return degrees;
        }

        // else if the given scale is fahrenheit then use the formula to convert to celsius and return the result
        else if(scale == 'F'){
            return ((degrees - 32.0f) * (5.0f/9.0f));
        }

        // if the given scale is anything else than celsius or fahrenheit then print a message and return zero.
        else{
            System.out.println("Invalid scale. Try again with scale either C (for celsius) or F (for fahrenheit)"); 
            return 0;
        }
    }

    public float getTempFehr (float degrees, char scale){
        scale = Character.toUpperCase(scale);

        //if the given scale is fahrenheit then just return whatever was passed in parameter
        if(scale == 'F'){
            return degrees;
        }

        // else if the given scale is celsius then use the formula to convert to fahrenheit and return the result
        else if (scale == 'C'){
            return ((9.0f*(degrees/5.0f))+32.0f);
        }

        // if the given scale is anything else than celsius or fahrenheit then print a message and return zero.
        else{
            System.out.println("Invalid scale. Try again with scale either C (for celsius) or F (for fahrenheit)"); 
            return 0;
        }
    }

}

您的上述编码方法需要在其他方面进行更正,以避免代码重复和可能的错误。

package temperatureapparatus;

public class Temperature {

  private char scale;
  private float degrees;

  // Constructor
  public Temperature(float degrees, char scale) {
    this.scale = scale;
    this.degrees = degrees;
  }

  // reuse already defined constructor
  public Temperature(char scale){
   this(0, scale);
  }

  public float getTemp(char scale) {
    if (this.scale == scale) return degrees;

    if (scale == 'F')
      return Temperature.convertToFah(degrees);
    else 
      return Temperature.convertFromFah(degrees);
  }


  public static float convertFromFah(float degrees) {
   return (float) ((degrees-32.0)*(5.0/9.0));
  }

  public static float convertToFah(float degrees) {
   return (float) ((9.0*(degrees/5.0))+32.0);
  }

}