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

时间:2016-05-12 17:56:51

标签: java constructor accessor

我的目标是创建一个“温度”类,表示摄氏和华氏温度。该类需要下面组成的四个构造函数。我需要一只手的部分将是两种存取方法,因为我还不熟悉它。我已经编写了代码,但我不确定它是否会成功,我会欣赏一些洞察力

四个构造函数:  1.一个度数  2.一个用于比例  3.一个学位和学位  4.默认构造函数

两种访问方法:

  1. 一个以摄氏度返回温度
  2. 另一个以华氏度数返回
  3. 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;
        }                
    }
    

4 个答案:

答案 0 :(得分:0)

您需要将farenheitForm移动到访问者方法中。如果它是私有字段,则只有在构造的类时才会初始化它并保持在32

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

从我现在设置的方式我相信我只是想要从摄氏温度转换为华氏温度......我怎么能让它可以互换呢

如果要使用degrees或farenheit对类进行实例化,则应修改第二个构造函数以获取表示传入的度量标准的标志;或者创建另一个构造函数,使用double参数而不是float,来表示farenheit形式。进入构造函数后,您可以将farenheit值转换为度数值并将其存储在私有字段中。

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

此时,还应该添加另一个构造函数来同时获取farenheit值和一个scale - 以与等效的构造函数一致。

答案 1 :(得分:0)

这可能不是您正在寻找的答案,但我不认为构造函数应该像这样设置。另外,我认为你应该编程到界面。

另外,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;
        }
}

请注意,我首先编写了接口,您只需要知道该接口就知道如何使用该类。通常我会抛出异常而不是System.ou.println("ERROR THING"),但现在几乎不重要。

无论如何,它可能对你的家庭作业问题没有帮助,但这就是我认为应该写出这样的课程。

答案 2 :(得分:0)

以下是您的代码的注释正确版本。但我不明白为什么你有实例变量,因为你从未使用它们。在你的情况下,构造函数也是无关紧要的。对于优雅的代码,您可以遵循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;
        }
    }

}

答案 3 :(得分: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);
  }

}