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.
0 rating(0 Reviews)