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 'C' 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 the 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.
• 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 another, and one to test whether one temperature is less than another.
Write a driver 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.
This project’s requirements include two accessor methods to read the temperature, one in degrees F and the other in degrees C. From this description it is not clear if the “two accessor methods to read the temperature…” should display or return the temperature in the specified units. Also, note the confusing terminology: methods that display values are actually “write” methods. The solution in this manual includes both types of accessor, two write methods to display (“read”) the temperature and units, and two get methods that return just the temperature in either degrees C or degrees F. An added feature of the solution is that it displays or returns temperatures rounded to one decimal place. The expression
Math.round(degrees*10)/10.0 is used, where the divisor is 10.0 (rather than 10) to force the division results to be floating point instead of an integer and not lose the decimal place. Also, as described in the prologue, units is not guaranteed to be a legitimate value (c, C, f, or F). The read methods give an error message if it is not a legitimate value, but the set methods allow any character and the get methods default to a return value of the variable degrees (no conversion is performed) if units is anything other than one of the legitimate values.
Getting the equals method to work properly highlights the problem of comparing two floating point values. One consequence of using a fixed number of bits to encode floating point values is that they cannot always be encoded precisely. Two mathematical expressions that have the same result when done by hand may actually have slightly different values (in the last decimal place or so) when stored in memory. For example the calculation
```
double a = 51.8 /1 0;
```
is likely to give a slightly different value than
```
double a = 0.518 * 10;
```
because each number is stored as an approximate value. Because of this comparisons of floating point values in conditional expressions do not always return the expected results. A way to get around it is to decide how many decimal place accuracy we want to compare, multiply the floating point numbers by the appropriate power of 10, then round the results to get integers. This is the approach taken in the comparison methods, equals, isGreaterThan, and isLessThan: First the methods make sure both temperatures are in the same units (degrees C, but degrees F would be equally valid) using the getC() method. Since getC() returns a value with one decimal place, the temperatures are then multiplied by 10 and rounded using Math.round(), which returns an integer value (you can think of it as comparing an integer number of tenths of degrees)
See the code in Temperature.java and TemperatureTest.java.
You might also like to view...
Answer the following statements true (T) or false (F)State whether each of the following is true or false. If false, explain why. Assume the state- ment using std::cout; is used.
1) Comments cause the computer to print the text after the // on the screen when the program is executed. 2) The escape sequence \n, when output with cout and the stream insertion operator, causes the cursor to position to the beginning of the next line on the screen. 3) All variables must be declared before they are used. 4) All variables must be given a type when they are declared. 5) C++ considers the variables number and NuMbEr to be identical.
Deleting a section break does not delete the section formatting
Indicate whether the statement is true or false
Select the correct syntax to declare an array named intReservations with 10 Integer elements.
A. Dim intReservations(9) as Integer B. Dim intReservations(10) as Integer C. Dim intReservations[9] as Integer D. Dim intReservations[10] as Integer
To customize theme colors, click the _____ tab.
A. Home B. Format C. Design D. Insert