Question

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

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

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// Temperature.java

public class Temperature {

                // attributes

                private double temp;

                private char tempScale;

                // constructor to initialize temperature to 0 and scale to 'C'

                public Temperature() {

                                temp = 0;

                                tempScale = 'C';

                }

                // constructor to initialize a temmperature in 'C'

                public Temperature(double initialTemp) {

                                temp = initialTemp;

                                tempScale = 'C';

                }

                // constructor to initialize a '0' temperature in tempType.

                // throws exception if tempType is not 'C' or 'F'

                public Temperature(char tempType) {

                                temp = 0;

                                if (!isValidScale(tempType)) {

                                               throw new RuntimeException("Invalid temperature type.");

                                }

                                this.tempScale = tempType;

                }

                // constructor to initialize a temperature in tempType.

                // throws exception if tempType is not 'C' or 'F'

                public Temperature(double initialTemp, char tempType) {

                                temp = initialTemp;

                                if (!isValidScale(tempType)) {

                                               throw new RuntimeException("Invalid temperature type.");

                                }

                                this.tempScale = tempType;

                }

                // returns the temperature

                public double getTemp() {

                                return temp;

                }

                // returns the scale

                public char getTempScale() {

                                return tempScale;

                }

                // sets the temperature

                public void setTemp(double temp) {

                                this.temp = temp;

                }

                // method to set the scale to a new scale. the temperature in old scale will

                // be converted to the new scale if necessary.throws exception if tempType

                // is not 'C' or 'F'

                public void setScale(char newScale) {

                                if (!isValidScale(newScale)) {

                                               throw new RuntimeException("Invalid temperature type.");

                                }

                                // converting temperature in C to F if previous scale is C and new scale

                                // is F

                                if (tempScale == 'C' && newScale == 'F') {

                                               temp = temp * (9.0 / 5.0) + 32;

                                }

                                // converting temperature in F to C if previous scale is F and new scale

                                // is C

                                else if (tempScale == 'F' && newScale == 'C') {

                                               temp = (temp - 32) / (9.0 / 5.0);

                                }

                                // setting new scale

                                tempScale = newScale;

                }

                // method to set new temperature and scale.

                public void setTempSystem(double temp, char tempScale) {

                                if (!isValidScale(tempScale)) {

                                               throw new RuntimeException("Invalid temperature type.");

                                }

                                this.temp = temp;

                                this.tempScale = tempScale;

                }

                // helper method to check if the scale is valid or not

                private boolean isValidScale(char c) {

                                if (c == 'C' || c == 'F') {

                                               return true;

                                }

                                return false;

                }

                @Override

                public boolean equals(Object obj) {

                                if (obj instanceof Temperature) {

                                               // safe type casting

                                               Temperature other = (Temperature) obj;

                                               // creating a new Temperature object using other object's values

                                               Temperature temperature = new Temperature(other.temp,

                                                                               other.tempScale);

                                               // converting other temperature to this scale so that this scale and

                                               // the other scales will be same and we can compare the temperatures

                                               temperature.setScale(this.tempScale);

                                               // checking if both temperatures are same

                                               return this.temp == temperature.temp;

                                }

                                return false;

                }

                // returns true if this temperature > other

                public boolean greaters(Temperature other) {

                                // creating a new Temperature object using other object's values

                                Temperature temperature = new Temperature(other.temp, other.tempScale);

                                // converting other temperature to this scale so that this scale and

                                // the other scales will be same and we can compare the temperatures

                                temperature.setScale(this.tempScale);

                                return this.temp > temperature.temp;

                }

                // returns true if this temperature < other

                public boolean lessers(Temperature other) {

                                Temperature temperature = new Temperature(other.temp, other.tempScale);

                                temperature.setScale(this.tempScale);

                                return this.temp < temperature.temp;

                }

                @Override

                public String toString() {

                                return temp + " " + tempScale;

                }

}

// TemperatureDemo.java

public class TemperatureDemo {

                public static void main(String[] args) {

                                // creating objects and testing all methods of Temperature class

                                Temperature temp1 = new Temperature();

                                System.out.println("temp1: " + temp1);

                                Temperature temp2 = new Temperature(32, 'F');

                                System.out.println("temp2: " + temp2);

                                System.out.println("temp1 = temp2: " + temp1.equals(temp2));

                                System.out.println("temp1 < temp2: " + temp1.lessers(temp2));

                                System.out.println("temp1 > temp2: " + temp1.greaters(temp2));

                                Temperature temp3 = new Temperature();

                                System.out.println("temp3: " + temp3);

                                System.out.println("temp3 scale: " + temp3.getTempScale());

                                temp3.setTempSystem(-40, 'C');

                                System.out.println("Converting temp3 to F");

                                temp3.setScale('F');

                                System.out.println("temp3: " + temp3);

                                System.out.println("0.0 C = 32.0 F: "

                                                               + new Temperature(0, 'C').equals(new Temperature(32.0, 'F')));

                                System.out

                                                               .println("-40.0 C = -40.0 F: "

                                                                                               + new Temperature(-40, 'C').equals(new Temperature(

                                                                                                                              -40.0, 'F')));

                                System.out.println("100.0 C = 212.0 F: "

                                                               + new Temperature(100, 'C').equals(new Temperature(212, 'F')));

                }

}

/*OUTPUT*/

temp1: 0.0 C

temp2: 32.0 F

temp1 = temp2: true

temp1 < temp2: false

temp1 > temp2: false

temp3: 0.0 C

temp3 scale: C

Converting temp3 to F

temp3: -40.0 F

0.0 C = 32.0 F: true

-40.0 C = -40.0 F: true

100.0 C = 212.0 F: true

Add a comment
Know the answer?
Add Answer to:
CIT-111 Homework #4, Part A                                      
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
  • 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...

  • CIT-111 Homework #5, Part A                                    Chapter 5, part 1

    CIT-111 Homework #5, Part A                                    Chapter 5, part 1 and alternate part 2 (page 341) A breakdown of the problem is as follows: Class name:                        Money Instance variables:           private, integer:                dollars, cents Constructor methods:    Money () – sets dollars and cents both to zero                                                 Money (int bucks) – sets dollars to bucks, cents to zero                                                 Money (int buck, int pennies) – sets dollars to bucks, cents to pennies Accessor methods:          getDollar () – returns value of dollars for...

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

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

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

  • 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

  • Using the following create the nessecary static methods to pass random arrays with the TempartureWithArrays and...

    Using the following create the nessecary static methods to pass random arrays with the TempartureWithArrays and Temperature classes -------------------------------------------------------------------------------------- public class TemperatureWithArrays {    public static final int ARRAY_SIZE = 5;    public static void main(String[] args)    {        int x;        Temperature temp1 = new Temperature(120.0, 'C');        Temperature temp2 = new Temperature(100, 'C');        Temperature temp3 = new Temperature(50.0, 'C');        Temperature temp4 = new Temperature(232.0, 'K');        Temperature tempAve =...

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