Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Temperature class that has two fields: a temperature value (a floating-p

ID: 3701575 • Letter: W

Question

Write a Temperature class that has two fields: a temperature value (a floating-point number) and a character for the scale, either “C” for Celsius or “F” for Fahrenheit. Make sure that these two fields can ONLY be accessed through the accessor methods outside of the class. Constructors: The class should have four constructors: one for each instance field (assume zero degree if no value is specified and Celsius if no scale is specified), one with two parameters for the two instance variables, and a default constructor (set to zero degrees Celsius). Methods: The class should have three types of methods (1) Two methods to return the temperature: one to return the degrees in Celsius, the other to return the degrees in Fahrenheit. Use the two following formulas to write the two methods degreesC = 5(degreesF – 32) / 9 degreesF = (9(degreesC/5) + 32 (2) Three methods to set the fields: one to set the value, one to set the scale (“F” or “C”), and one to set both (the un-specified field should not be changed). (3) A comparison method with two parameters. Both of the two parameters are objects of the Temperature class (may be in different scales!). This method should return an integer: 0: means the two temperatures are equal. E.g., 0.0 degreesC = 32.0 degreesF; 0.0 degreesC = 0.0 degreesC 1: means the first temperature is higher than the second one -1: means the first temperature is lower than the second one Driver program: You also need to write a driver program that tests all the methods: write a Driver class, which contains a main method. (a). Make sure to use EACH of the constructors and methods except the comparison method (there are 9 of them!). After each object construction or field setting, immediately print out to the screen the values of the two fields of the objects. (b). Design the driver program in the way that, when you run the program, if you don’t provide any other parameter in the command line, the program just prints out the information required in (a); but if you directly provide two strings which describe temperatures (in the format of a number followed by “C” or “F”) after the java program name, it will print out 0, 1 or -1 to show the equality of the two temperatures. For example, after you compile “Driver.java” and then type the following in the command line java Driver 0.0C 32.0F the program should print out “0” to the screen (after all the information required in (a) is printed out) since they are equal. Similarly, if you type “java Driver 10.0F 0.0C” in the command line, your program should print out “-1” to the screen. HINT: what you get from the command line are strings! You need to convert the number parts into float numbers and retrieve the characters for the scale. Check the java documentation for functions in the String class! You may also need to check functions in the Float class or Double class for the conversion. Submit your .java source code files as your assignment submission. No documentation or .class compiled files are required. I just want to see your .java files.

Explanation / Answer

Hello. Created the required Temperature class and the driver class as specified. Everything is explained well using comments. Go through it, understand the code and let me know if you have any queries. Thanks.

// Temperature.java

public class Temperature {

                private double temperature;

                private char scale;

                // constructor 1, with double argument

                public Temperature(double temp) {

                                temperature = temp;

                                scale = 'C';// default scale

                }

                // constructor 2, with char argument(scale)

                public Temperature(char scale) {

                                this.scale = scale;

                                temperature = 0;// default temp

                }

                // constructor 3, with both arguments

                public Temperature(float temperature, char scale) {

                                this.temperature = temperature;

                                this.scale = scale;

                }

                // constructor 4, default

                public Temperature() {

                                temperature = 0;

                                scale = 'C';

                }

                /**

                * method to return the temperature in celsius

                */

                public double inCelsius() {

                                if (scale == 'C') {

                                                // already in celsius, returning it

                                                return temperature;

                                } else {

                                                // converting to celsius

                                                double c = (5.0 / 9.0) * (temperature - 32.0);

                                                return c;

                                }

                }

                /**

                * method to return the temperature in fahrenheit

                */

                public double inFahrenheit() {

                                if (scale == 'F') {

                                                // already in fahrenheit, returning it

                                                return temperature;

                                } else {

                                                // converting to fahrenheit

                                                double f = (9.0 * temperature + (32.0 * 5.0)) / 5.0;

                                                return f;

                                }

                }

                /**

                * method to compare two temperature objects

                *

                * @param t1

                *            - first temperature

                * @param t2

                *            - second temperature

                * @return - 0 if equal, 1 if first > second, else -1

                */

                public static int compare(Temperature t1, Temperature t2) {

                                if (t1.inCelsius() == t2.inCelsius()) {

                                                return 0;

                                } else if (t1.inCelsius() > t2.inCelsius()) {

                                                return 1;

                                } else {

                                                return -1;

                                }

                }

                // getter methods

                public double getTemperature() {

                                return temperature;

                }

                public char getScale() {

                                return scale;

                }

}

// Driver.java

public class Driver {

                public static void main(String[] args) {

                                if (args.length == 2) {

                                                /**

                                                * extracting values from command line arguments, assuming that the

                                                * input is valid

                                                */

                                                String str = args[0];

                                                float temperature = Float.parseFloat(str.substring(0,

                                                                                str.length() - 1));//getting temperature part

                                                char scale = str.charAt(str.length() - 1); //getting scale part

                                                Temperature t1 = new Temperature(temperature, scale);

                                                str = args[1];

                                                temperature = Float.parseFloat(str.substring(0, str.length() - 1));

                                                scale = str.charAt(str.length() - 1);

                                                Temperature t2 = new Temperature(temperature, scale);

                                                /**

                                                * comparing both temperatures and displaying the result value

                                                */

                                                System.out.println(Temperature.compare(t1, t2));

                                } else {

                                                /**

                                                * Testing the Temperature class thoroughly

                                                */

                                                Temperature t1 = new Temperature();

                                                System.out.println("Using default constructor, temp= "

                                                                                + t1.getTemperature() + ", scale= " + t1.getScale());

                                                Temperature t2 = new Temperature(25.2);

                                                System.out.println("Using constructor with one floating point "

                                                                                + "argument, temp= " + t2.getTemperature() + ", scale= "

                                                                                + t2.getScale());

                                                Temperature t3 = new Temperature('F');

                                                System.out.println("Using constructor with scale 'F' as argument,"

                                                                                + " temp= " + t3.getTemperature() + ", scale= "

                                                                                + t3.getScale());

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

                                                System.out.println("Using constructor specifying both parameters"

                                                                                + " temp= " + t4.getTemperature() + ", scale= "

                                                                                + t4.getScale());

                                                System.out.println("Temperature t1 in celsius: " + t1.inCelsius());

                                                System.out.println("Temperature t1 in fahrenheit: "

                                                                                + t1.inFahrenheit());

                                                System.out.println("Temperature t4 in celsius: " + t4.inCelsius());

                                                System.out.println("Temperature t4 in fahrenheit: "

                                                                                + t4.inFahrenheit());

                                                System.out.println("Comparing t1 and t4: "

                                                                                + Temperature.compare(t1, t4));

                                }

                }

}

/*OUTPUT when executed without command line arguments*/

Using default constructor, temp= 0.0, scale= C

Using constructor with one floating point argument, temp= 25.2, scale= C

Using constructor with scale 'F' as argument, temp= 0.0, scale= F

Using constructor specifying both parameters temp= 32.0, scale= F

Temperature t1 in celsius: 0.0

Temperature t1 in fahrenheit: 32.0

Temperature t4 in celsius: 0.0

Temperature t4 in fahrenheit: 32.0

Comparing t1 and t4: 0

/*OUTPUT when executed with command line arguments 32.0F 0.0C*/

0

/*OUTPUT when executed with command line arguments 33.0F 0.0C*/

1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote