0

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;
    }                
}
Nathan Hughes
  • 85,411
  • 19
  • 161
  • 250
  • I like your approach, keeping the temp in one form and converting to the other. However, you need to ask a real question. Try it out, does it work? What problems are you having? – Pete B. May 12 '16 at 18:08
  • @PeteBelford i did not write a driver yet ... maybe i should? it does seem practical now that you point it out lol ... but! my whole public static final float fahrenheitForm = ((9.0*(degrees/5.0))+32.0); kinda gives me anxiety... would it work like that? – JavaSpawn101 May 12 '16 at 18:10
  • @JavaSpawn101 In Java, you can't write methods like this. You could look into private methods instead to convert Fahrenheit to Celsius and vice versa! – TimoStaudinger May 12 '16 at 18:20
  • @TimoSta not sure what you mean by this exactly.would you mind showing me some example code? – JavaSpawn101 May 12 '16 at 18:23
  • t does not make sense to provide constructor only for the degree (and without the scale). When you want to convert it to F or C nobody knows what "animal" it is and what formula should be used. The same apply to the non-parameter constructor. – MaxZoom May 12 '16 at 21:12

4 Answers4

0

You need to move your farenheitForm into the accessor method. If it's a private field it will only be initialised when the class if constructed and remain at 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

If you want to instatiate the class with either degrees or farenheit you should modify your 2nd constructor to take a flag representing the metric being passed in; or alternatively create another constructor taking a double argument instead of a float, to represent a farenheit form. Once inside the constructor you can convert the farenheit value to a degrees value and store it in the private field.

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

It would also be worth, at this point, to add another constructor to take both a farenheit value, and a scale - as to be consistent with the equivalent degree constructors.

James Buck
  • 1,565
  • 6
  • 13
0

This might not be the answer that you're looking for, but I don't think the constructors should be set up like that. Also, I think you should be programming to an interface.

Also, java is a little bit wonky with numbers. The first time I tried to compile your code, I got:

 incompatible types: possible lossy conversion from double to float

For anything using those hard-coded conversion floats until I type-casted everything.

If I was to do this differently, I would program to some sort of an interface with clearly defined accessor methods. With your current constructors, it's very unclear as to how it could be used and is dangerous IMO.

I would set up an interface like so:

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

With an implementation like so:

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

Note that I wrote the interface first and you only have to know that interface to know how to use the class. Normally I would throw exceptions instead of System.ou.println("ERROR THING"), but it hardly matters right now.

Whatever, it probably doesn't help your homework problem but this is how I think a class like this should be written.

Alexander Kleinhans
  • 4,984
  • 6
  • 43
  • 89
0

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

}
Community
  • 1
  • 1
Bijay Regmi
  • 90
  • 14
0

Your coding approach to the above needs to be corrected in other to avoid code repetitions and possible errors.

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

} 
MaxZoom
  • 6,949
  • 5
  • 24
  • 41