Question
Java program

Write a Temperature class that represents temperatures in degrees in both Celsius and Fahrenheit. Use a floating- point number for the temperature and a character for the scale, eitherでfor Celsius or F for fahrenheit. The class should have Four constructors: one for the number of degrees, one for the scale, one for both the degrees and scale, and a default constructor. For each of these constructors, assume zero degrees if no value is specified and celsius if no scale is given Two accessor methods: one to return the temperature in degrees Celsius, the other to return it in degrees Fahrenheit. Use the formulas from programming project 5 of chapter 3 and round to the nearest tenth of a degree o Degrees_C5(Degrees_F- 32)/9 o DegreesF (9(Degrees_C)5) 32) Three set methods: one to set the number of degrees, one to set the scale and one to set both Three comparison methods: one to test whether two temperatures are equal, one to test whether one temperature is greater than the other, and one to test whether one temperature is less than the other Additionally write a driver a program that tests all the methods. Be sure to invoke each of the constructors, to include at least one true and one false case for each comparison method, and to test at least the following three temperature pairs for equality. 0.0 degrees C and 32.0 degrees F, -40.0 degrees C and -40.0 degrees F, and 100.0 degrees C and 212.0 Degrees F. Make sure in the Class file to appropriately comment your code with javadoc comments for every method. (Pre and post condition comments)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java Program:


package temperatureclassdriver;

//Temperature Class
//Class definition
class Temperature {
  
    //Data memebers
    private double val;
    private char scale;
  
    //Methods

    //Default constructor
    public Temperature() {
        val = 0;
        scale = 'C';
    }

    //1-Arg
    public Temperature(double val) {
        this.val = val;
        this.scale = 'C';
    }
  
    //1-Arg
    public Temperature(char scale) {
        this.val = 0;
        this.scale = scale;
    }
  
    //2-Arg
    public Temperature(double val, char scale) {
        this.val = val;
        this.scale = scale;
    }
  
    //Accessor method
    double getTemp_Celsius() {
      
        //If its is in Celsius
        if(scale == 'C') {
            return val;
        }
      
        double mVal;
      
        mVal = ( 1.8 * val ) + 32.0;
      
        //Converting to Celsius
        return mVal;
    }
  
    //Accessor method
    double getTemp_Fahrenheit() {
        //If its is in Fahrenheit
        if(scale == 'F') {
            return val;
        }
      
        double mval;
      
        //Applying conversion
        mval = (double)((5/9.0) * (val - 32));
      
        //Converting to Celsius
        return mval;
    }

    //Setter method
    public void setVal(double val) {
        this.val = val;
    }

    //Setter method
    public void setScale(char scale) {
        this.scale = scale;
    }
  
    //Setter Method
    public void setValues(double val, char scale) {
        this.val = val;
        this.scale = scale;
    }

    //Compares two Objects
    public boolean equals(Temperature obj) {
      
        //Comparing scales
        if (this.getTemp_Fahrenheit() != obj.getTemp_Fahrenheit()) {
            return false;
        }
        return true;
    }
  
    //Compares to Objects
    public boolean isGreater(Temperature obj) {
      
        //Comparing scales
        if (this.getTemp_Celsius() > obj.getTemp_Celsius()) {
            return true;
        }
        return false;
    }
  
    //Compares to Objects
    public boolean isLesser(Temperature obj) {
      
        //Comparing scales
        if (this.getTemp_Celsius() < obj.getTemp_Celsius()) {
            return true;
        }
        return false;
    }
}

//Class definition
public class TemperatureClassDriver {

    //Main method
    public static void main(String[] args) {
      
        //Testing Temperature class
        Temperature t1 = new Temperature(32, 'C');
        Temperature t2 = new Temperature(20);
        Temperature t3 = new Temperature('F');
      
        //Comparing temperatures t1 and t2
        if(t1.isGreater(t2)) {
            System.out.println(" Temperature 1 is greater than Temperature 2 ");
        }
        else {
            System.out.println(" Temperature 1 is less than Temperature 2 ");
        }
              
        //Comparing temperatures t2 and t3
        if(t2.isLesser(t3)) {
            System.out.println(" Temperature 2 is less than Temperature 3 ");
        }
        else {
            System.out.println(" Temperature 2 is greater than Temperature 3 ");
        }
      
        //Creating 6 temperature objects
        Temperature t11 = new Temperature(0, 'C');
        Temperature t12 = new Temperature(32.0, 'F');
      
        Temperature t13 = new Temperature(-40.0, 'C');
        Temperature t14 = new Temperature(-40.0, 'F');
      
        Temperature t15 = new Temperature(100.0, 'C');
        Temperature t16 = new Temperature(212.0, 'F');
      
        //Comparing objects
        if(t11.equals(t12)) {
            System.out.println(" Both are same ");
        }
        else {
            System.out.println(" Both are not same ");
        }
      
        if(t13.equals(t14)) {
            System.out.println(" Both are same ");
        }
        else {
            System.out.println(" Both are not same ");
        }
      
        if(t15.equals(t14)) {
            System.out.println(" Both are same ");
        }
        else {
            System.out.println(" Both are not same ");
        }
    }
}


________________________________________________________________________________________

Sample Run:

Output Debugger Console X | TemperatureClassDriver (run) as Temperature 1 is greater than Temperature 2 Temperature 2 is less

Add a comment
Know the answer?
Add Answer to:
Java program Write a Temperature class that represents temperatures in degrees in both Celsius and Fahrenheit....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In java code: Write a Temperature class that has two private instance variables: • degrees: a...

    In java code: Write a Temperature class that has two private instance variables: • degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase) The class should have (1) four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument...

  • Java Write a Temperature class that has two private instance variables: • degrees: a double that...

    Java Write a Temperature class that has two private instance variables: • degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase) The class should have - four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument constructor (set...

  • A breakdown of the problem is as follows: Class name:                        Temperature Instance variables:     &

    A breakdown of the problem is as follows: Class name:                        Temperature Instance variables:           temp (double) and tempScale (char; either ‘C’ or ‘F’) Constructor methods:    Temperature() – sets temp to 0 Celsius                                                 Temperature(double initialTemp) – sets temp                                                 Temperature(char tempType) – sets tempScale to ‘C’ or ‘F’)                                                 Temperature(double initialTemp, char tempType) --                                                                 sets temp and tempScale to ‘C’ or ‘F’) Accessor methods:          getTemp – returns value of temp for the object                                                 getTempScale– returns temperature scale (‘C’ or...

  • Question: Fahrenheit To Celsius Temperature Converter GUI Assignment Write A GUI To... java Fahrenholt to Celsius...

    Question: Fahrenheit To Celsius Temperature Converter GUI Assignment Write A GUI To... java Fahrenholt to Celsius Temperature Converter GUI Assignment Write a Gul to convert Fahrenheit temperatures to Celsius temperatures and has the following appearance: Com Convert It must include the following foatures • The frame we must say 'Fahrenheit to Celsius Temperature Converter • A border layout will be used for the GUI • The JLabelite of the GUI wil suy Fahrerholt to Celsius Temperature Converter and be in...

  • For this assignment you will create a program converting Fahrenheit temperatures to Celsius temperatures. The formula...

    For this assignment you will create a program converting Fahrenheit temperatures to Celsius temperatures. The formula for converting a temperature from Celsius to Fahrenheit is: F = C x 1.8 + 32, where F is the Fahrenheit temperature and C is the Celsius temperature. Write a function named Fahrenheit that accepts a Celsius temperature as an argument and returns the temperature converted to Fahrenheit. Demonstrate the function by accepting a Celsius temperature from a user and displaying the temperature converted...

  • In Python, write a program that converts Celsius temperatures to Fahrenheit temperatures. The formula is as...

    In Python, write a program that converts Celsius temperatures to Fahrenheit temperatures. The formula is as follows: F=(9/5)C+32 The program should ask the user to enter a temperature in Celsius, then display the temperature converted to Fahrenheit.

  • c++ please 2. (50 points) Create a Temperature class that internally stores a temperature in degrees...

    c++ please 2. (50 points) Create a Temperature class that internally stores a temperature in degrees Kelvin as a double. Create mutator functions named setTempKelvin, setTempFahrenheit, and set TempCelsius that take an input temperature in the specified temperature scale (i.e. Kelvin, Fahrenheit, and Celsius, respectively), and convert the temperature to Kelvin, and store that temperature in the class member private variable (only in Kelvin, no other scales). Also, create functions that return the stored temperature in degrees Kelvin, Fahrenheit, or...

  • IN JAVA…Write a GUI application that converts Celsius temperatures to Fahrenheit temperatures. The user should be...

    IN JAVA…Write a GUI application that converts Celsius temperatures to Fahrenheit temperatures. The user should be able to enter a Celsius temperature, click a button, and then see the equiva-lent Fahrenheit temperature. Use the following formula to make the conversion: F = (9/5)C + 32 F is the Fahrenheit temperature and C is the Celsius temperature. Instead of only converting from Celsius to Fahrenheit, also convert from Fahrenheit to Celsius depending on the user's choice. Some hints: Use JTextField and...

  • Write a program that converts Celsius temperatures to Fahrenheit temperatures. The formula is as follows: F=95C+32...

    Write a program that converts Celsius temperatures to Fahrenheit temperatures. The formula is as follows: F=95C+32 The program should ask the user to enter a temperature in Celsius, and then display the temperature converted to Fahrenheit. I'm doing this on Python.

  • (in python) Write a GUI application that converts Celsius temperatures to Fahrenheit temperatures. The user s......

    (in python) Write a GUI application that converts Celsius temperatures to Fahrenheit temperatures. The user s... Write a GUI application that converts Celsius temperatures to Fahrenheit temperatures. The user should be able to enter a Celsius temperature, click a button, and then see the equivalent Fahrenheit temperature. Use the following formula to make the conversion ( F=9/5 C + 32) where F is the Fahrenheit temperature and C is the Celsius temperature

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT