JavaSpawn101 JavaSpawn101 - 6 months ago 23
Java Question

java basic accessor methods for temperature converter

My objective is to create a class "Temperature" that represents temperature in both Celsius and Fahrenheit. The class requires four constructors of the make up below. The part i need a hand with would be the Two accessor methods since I'm not all that familiar with it yet. I have written code but am unsure if it would work out id appreciate some insight

Four constructors:
1. one for the number of degrees
2. one for the scale
3. one for both the degrees and the scale
4. default constructor

Two accessor methods:


  1. one to return the temperature in degrees Celsius

  2. the other to return it in degrees Fahrenheit



w/ the formulas given below
C = 5 ( F – 32) / 9
F = 9 * C/5 + 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

please don't hate i am only a novice and might have fatal errors

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;
}


}

Answer

Below is the commented correct version of your code. But I do not see why you have instance variables as you have never used them. In your case the constructors are also extraneous. For elegant code you can either follow coding implementation of Alexander Kleinhans or you can label your conversion methods (i.e getTempCels and getTempFehr) static and you do not have to create an instance to use those methods. You can view this link to know about static vs non static methods.

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;
        }
    }

}
Comments