Question

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 ‘F’)
  • Mutator methods:           setTemp – sets the value of temp
                                                    setScale – sets the value of tempScale
                                                    setTempSystem – sets both the value of temp and tempScale
  • Comparison methods:   equals – determines if argument is equal to current temperature
                                                    greaters—determines if argument is greater than current temperature
                                                    lessers—determines if argument is less than current temperature

    Allow for conversion using temperature formulas prior to doing comparison.
    DegreesC = 5 / 9 * (degreesF – 32)            DegreesF = (9/5 * (degreesC) ) + 32
  • Additional method:         tostring – returns a string that is representative of the object

Write a driver program(s) that tests all methods and constructors and at least test

  • 0.0 C      =             32.0 F
  • -40.0 C =             -40.0 F
  • 100.0 C =             212.0 F

Submit two files (Temperature.java and TemperatureDemo.java. Both files must compile without errors.
                                               
                                               

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Temperature.java

============================================================================================

package shape;

public class Temperature {
   double temp;
   char tempScale;
   //Constructor methods
   Temperature()
   {
       temp=0;
   }
   Temperature(double initialTemp)
   {
       temp=initialTemp;
   }
   Temperature(char tempType)
   {
       tempScale=tempType;
   }
   Temperature(double initialTemp, char tempType)
   {
       temp=initialTemp;
       tempScale=tempType;
   }
  
   //Accessor methods
   public double getTemp() {
       return temp;
   }
   public char getTempScale() {
       return tempScale;
   }
  
   //Mutator methods
   public void setTemp(double temp) {
       this.temp = temp;
   }
  
   public void setTempScale(char tempScale) {
       this.tempScale = tempScale;
   }
  
   public void setTempSystem(double temp,char tempScale) {
       this.temp = temp;
       this.tempScale = tempScale;
   }
  
  
   //Comparison methods
   public boolean equals(Temperature ob)
   {
       double tempC1,tempC2;
      
       //convert temp to celsius
       if(ob.getTempScale()=='F')
       {
           tempC1= 5 / 9 * (ob.getTemp()-32);
       }
       else
       {
           tempC1=ob.getTemp();
       }
      
       if(this.getTempScale()=='F')
       {
           tempC2= 5 / 9 * (this.getTemp()-32);
       }
       else
       {
           tempC2=this.getTemp();
       }
      
       if(tempC1==tempC2)
           return true;
       else
           return false;
   }
  
   public boolean greaters(Temperature ob)
   {
double tempC1,tempC2;
      
       //convert temp to celsius
       if(ob.getTempScale()=='F')
       {
           tempC1= 5 / 9 * (ob.getTemp()-32);
       }
       else
       {
           tempC1=ob.getTemp();
       }
      
       if(this.getTempScale()=='F')
       {
           tempC2= 5 / 9 * (this.getTemp()-32);
       }
       else
       {
           tempC2=this.getTemp();
       }
      
       if(tempC1>tempC2)
           return true;
       else
           return false;
   }
  
   public boolean lessers(Temperature ob)
   {
double tempC1,tempC2;
      
       //convert temp to celsius
       if(ob.getTempScale()=='F')
       {
           tempC1= 5 / 9 * (ob.getTemp()-32);
       }
       else
       {
           tempC1=ob.getTemp();
       }
      
       if(this.getTempScale()=='F')
       {
           tempC2= 5 / 9 * (this.getTemp()-32);
       }
       else
       {
           tempC2=this.getTemp();
       }
      
       if(tempC1<tempC2)
           return true;
       else
           return false;
   }
  
  
  
   //tostring()
   @Override
   public String toString() {
       return "Temperature [temp=" + temp + ", tempScale=" + tempScale + "]";
   }
  

}

============================================================================================

TemperatureDemo.java

package shape;

public class TemperatureDemo {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
      
       Temperature t1=new Temperature(); //Temperature()
       t1.setTempScale('C'); // setScale
      
      
       Temperature t2=new Temperature(32,'F'); // Temperature(double initialTemp, char tempType)
      
       //tosting()
       System.out.println(t1);
       System.out.println(t2);
       //equals()
       boolean b=t1.equals(t2);
       if(b==true)
           System.out.println("Both temp are equal");
       else
           System.out.println("Both temp are not equal");
      
       Temperature t3=new Temperature(); //Temperature()
       t3.setTemp(-40); // setTemp
       t3.setTempScale('C'); //setScale
      
       Temperature t4=new Temperature(); //Temperature()
       t4.setTempSystem(-40,'F'); // setTempSystem – sets both the value of temp and tempScale
      
       b=t3.greaters(t4);
       if(b==true)
           System.out.println(t4+" is greater than "+t3);
       else
           System.out.println(t4+" is less than "+t3);
      
       Temperature t5=new Temperature(100); // Temperature(double initialTemp) – sets temp
       t5.setTempScale('C');
      
       Temperature t6=new Temperature('F'); // Temperature(double initialTemp) – sets temp
       t6.setTemp(212);
      
       b=t5.lessers(t6);
       if(b==true)
           System.out.println(t6+" is less than "+t5);
       else
           System.out.println(t6+" is greater than "+t5);

   }

}

============================================================================================

Output

- 0 X Java - Shape/src/shape/TemperatureDemojava - Eclipse Eile Edit Source Relator Navigate Search Project Bur Window Help Q

Add a comment
Know the answer?
Add Answer to:
A breakdown of the problem is as follows: Class name:                        Temperature Instance variables:     &
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
  • CIT-111 Homework #4, Part A                                      

    CIT-111 Homework #4, Part A                                                                    Chapter 4, problem #7 (pages 254-255) 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...

  • 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...

  • In Java please, Create a class called airConditioner which has 3 private attributes; integer temperature, boolean...

    In Java please, Create a class called airConditioner which has 3 private attributes; integer temperature, boolean on and String location and has 5 behaviors: turning the air conditioner on and off (called turnON and turnOFF with no return and no arguments) checking to see if the air conditioner is on (called isON wihch returns boolean) sets the temperature( setTemp which receives an int argument) and gets current temp (getTemp) which accepts no value and returns the int temp

  • In java Se8 i am trying to write a program convert temperature between celsius, fahrenheit and...

    In java Se8 i am trying to write a program convert temperature between celsius, fahrenheit and kelvin, but i am stuck at how to return the proper result without chage the frame, and I cnould not figure out how to use those three settemp method. import java. uti1. Arrays public class Temperature different scale names/ public static Stringll scales -Celsius", "Fahrenheit", "Kelvin private double temperature private char scale; public Temperature (double temp temp 273. 15; this. scale-C if (temp <-273....

  • Java program Write a Temperature class that represents temperatures in degrees in both Celsius and Fahrenheit....

    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...

  • In this assignment you will be implementing a weather forecaster. It involves writing 3 different classes...

    In this assignment you will be implementing a weather forecaster. It involves writing 3 different classes plus a driver program. It is recommended that you write one class at a time and then write a driver (tester) program to ensure that it works before putting everything together. Alternately, you can use the Interactions tab of JGRASP to test some of the early classes. This is discussed in the next few pages. The Season Class The first, shortest, and easiest class...

  • Write a Temperature class in java and only assing the values if they are valid. Temperature...

    Write a Temperature class in java and only assing the values if they are valid. Temperature Degrees:double                    between -50 and 150 F Scale :char                            can be C or F +Temperature ()                   default to 10 celcius +Temperature(temp:double, scale:char) assign if valid +get_Temp():double              returns current temperature value +get_Scale(): char                   returns current scale +set(temp:double,scale:char)void        set degrees and scale if valid +set_Temp(temp:double)void               assings it if valid +set_Scale(scale:char)voild                    coverts existing tmep to scale if valid +covert_FtoC(temp: double)double     coverts F to returned C

  • Step 1 Develop the following class: Class Name: Bag Access Modifier: public Instance variables Name: name...

    Step 1 Develop the following class: Class Name: Bag Access Modifier: public Instance variables Name: name Access modifier: private Data Type: String Name: currentWeight Access modifier: private Data Type: double Name: maximumWeight Access modifier: private Data Type: double Constructors Name: Bag Access modifier: public Parameters: none (default constructor) Task: sets the value of the instance variable name to the empty string sets the value of the instance variable currentWeight to 0.0 sets the value of the instance variable maximumWeight to...

  • Write a class called Student. The specification for a Student is: Three Instance fields name -...

    Write a class called Student. The specification for a Student is: Three Instance fields name - a String of the student's full name totalQuizScore - double numQuizesTaken - int Constructor Default construtor that sets the instance fields to a default value Parameterized constructor that sets the name instance field to a parameter value and set the other instance fields to a default value. Methods setName - sets or changes the student name by taking in a parameter getName - returns...

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
ADVERTISEMENT