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

4 Answers

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

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.

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.

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like