Question
Instructions/Design Design and implement a class, called Temperature , to represent a temperature (example: 98.6F degrees Fahrenheit.) Use the following design: 1) Put your class
Instructions/Design
Design and implement a class, called Temperature, to represent a temperature (example: 98.6F degrees Fahrenheit.) Use the following design:
1) Put your class in a file named: Temperature.java
2) A Temperature object has two instance variables/properties/attributes:
- degrees is the temperature value (a real number with a fractional part) - use a double - and type, either 'C' for Celsius or 'F' for Fahrenheit - use a char
3) Member functions:
Four constructors:
- one to specify both parameters (degrees, type) - one to specify just the degrees (defaults to type Celsius) - one to specify just the type (defaults to 0.0 for the degrees value) - and the default constructor that specifies neither (defaults to 0 degrees Celsius)
4) Three "write" methods (these DO NOT change values of original instance data):
- one to display both parameter values (use this for debugging) - writeOutput() - one to display temperature in degrees C (even if value is Fahrenheit)- writeC() - and one to display temperature in degrees F (even if value is in Celsius)- writeF()
5) Two accessor methods to get (return) temperature (these DO NOT change values of instance data):
- one in degrees C returns value in Celsius (even if current value is in Fahrenheit) - getC() - and the other in degrees F returns Fahrenheit value (no matter what type current temperature has) - getF()
Note: get methods return value of the variable degrees if the type is any character other than c, C, f, or F. No input validation is required. |
.
Hint: Displayed (write) and returned (get) temperature values are rounded to one decimal place using the statement:Math.round(temperature*10)/10.0 where the 10.0 is used to force the division to return a floating point value rather than an integer. |
6) Three mutator methods to set (update) the parameters:
- one to set the degrees value - one to set the type value - and one to set both together.
All are named set but the method is overloaded for the three variations of parameters
7) One comparison method named equals(): example: returns true if temperature1 is equivalent to temperature 2
HINT: This means 32F should be equal to 0C. Convert all temperatures to either Celsius or Fahrenheit (using the get methods) so you are comparing similar values. Method returns a boolean |
8) One toString() method Example output: temperature 98.6F but you may determine what output is displayed
9) Implement a readInput() method to prompt user for degrees and type and then reads the values. NOTE: The Scanner class does not have a specific method for reading chars. Method MUST DO INPUT VALIDATION FOR units. Valid inputs are only: 'C' or 'F' or 'c' or 'f' for units. If user inputs lower case 'c' or 'f' these are acceptable and do not need to be changed to upper case
Hint: Read the input as a string and pick out first element from the string (which is the character you want). |
9) A partially written test program to test the class Temperature is provided. We are using TDD so the test program was written BEFORE the production (actual) code. This test is in a separate class called TemperatureTest and has been provided for you
____________________________________________________________________________________________________________________________________________________
import java.util.Scanner; public class TemperatureTest { public static void main(String[] args) { //dummy variable to stop scrolling so user can see test results String junk; Scanner scan = new Scanner(System.in); header(); System.out.println(); System.out.println("Test case 1: default constructor and"); System.out.println("writeOutput() method."); System.out.println(); Temperature t1 = new Temperature(); System.out.println("Results of default constructor:"); System.out.println("Verify 0 degrees C."); System.out.println(); t1.writeOutput(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 2: readInput() method."); t1.readInput(); System.out.println(); System.out.println("Verify temperature and units:"); System.out.println("Should be whatever you just entered."); System.out.println(); t1.writeOutput(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 3: constructor with just" + " temperature."); Temperature t2 = new Temperature(20.5); System.out.println(); System.out.println("Verify 20.5 degrees C."); System.out.println(); t2.writeOutput(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 4: constructor with just units," + " f."); System.out.println(); System.out.println("Verify 0 degrees f."); System.out.println(); Temperature t3 = new Temperature('f'); t3.writeOutput(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 5: constructor with just units," + " c."); System.out.println(); System.out.println("Verify 0 degrees c."); System.out.println(); Temperature t4 = new Temperature('c'); t4.writeOutput(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 6: constructor with" + " temperature and units."); System.out.println(); System.out.println("Verify -51.2 degrees F."); System.out.println(); Temperature t5 = new Temperature(-51.2, 'F'); t5.writeOutput(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 7: read degrees C, " + "original in F."); System.out.println(); System.out.println("Verify -46.2 degrees Celsius."); System.out.println(); t5.writeC(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 8: read degrees C, " + "original in C."); System.out.println(); System.out.println("Verify 0 degrees Celsius."); System.out.println(); t4.writeC(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 9: read degrees F, " + "original in C."); System.out.println(); System.out.println("Verify 32 degrees Fahrenheit"); System.out.println(); t4.writeF(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 10: read degrees F, " + " original in F."); System.out.println(); System.out.println("Verify -51.2 degrees Fahrenheit"); System.out.println(); t5.writeF(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 11: getF method with " + "original temperature in F."); System.out.println(); System.out.println("Verify -51.2"); System.out.println(); System.out.println(t5.getF()); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 12: getF method with original" + " temperature in C."); System.out.println("Verify 32"); System.out.println(); System.out.println(t4.getF()); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 13: getC method with " + "original temperature in F."); System.out.println("Verify -46.2"); System.out.println(); System.out.println(t5.getC()); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println(); System.out.println("Test case 14: getC method with " + "original temperature in C."); System.out.println("Verify 0"); System.out.println(); System.out.println(t4.getC()); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println(); System.out.println("Test case 15: set method for " + "both parameters."); System.out.println("t5 before:"); t5.writeOutput(); t5.set(72.8, 'C'); System.out.println("After set: verify 72.8 degrees C."); System.out.println(); t5.writeOutput(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 16: set method for just " + "temperature."); System.out.println("t5 before:"); t5.writeOutput(); t5.set(100); System.out.println("After set: verify 100 degrees C."); System.out.println(); t5.writeOutput(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 17: set method for just units."); System.out.println("t5 before:"); t5.writeOutput(); t5.set('F'); System.out.println("After set: verify 100 degrees F."); System.out.println(); t5.writeOutput(); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 18: equals test 1."); System.out.println(); t1.set(100, 'C'); t2.set(212, 'F'); System.out.println("T1 = 100 degrees C."); System.out.println("T2 = 212 degrees F."); System.out.println(); System.out.println("Here are the two temperatures " + "converted to degrees C:"); t1.writeC(); t2.writeC(); System.out.println(); System.out.println("Verify results: should be true."); System.out.println(); System.out.println(t1.equals(t2)); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 19: equals test 2."); System.out.println(); t1.set(100.1); System.out.println("T1 changed to 100.1 degrees C."); System.out.println("T2 = 212 degrees F."); System.out.println(); System.out.println("Here are the two temperatures in " + " degrees C:"); t1.writeC(); t2.writeC(); System.out.println(); System.out.println("Verify results: should be false."); System.out.println(); System.out.println(t1.equals(t2)); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 20: equals test 3."); System.out.println(); t1.set(100); t2.set(212.1); System.out.println("T1 changed back to 100 degrees C."); System.out.println("T2 = 212.1 degrees F."); System.out.println(); System.out.println("Here are the two temperatures in " + "degrees C:"); t1.writeC(); t2.writeC(); System.out.println(); System.out.println("Verify results: should be false."); System.out.println(); System.out.println(t1.equals(t2)); System.out.println(); System.out.println("Hit 'y' to continue."); junk = scan.nextLine(); System.out.println(); System.out.println("Test case 21 (at last): toString method"); System.out.println("T2 = 212.1 degrees F."); System.out.println(t2); System.out.println(); } //header information public static void header() { System.out.println("TEMPERATURE TEST PROGRAM"); System.out.println("Author: Constance Conner - Section 001"); System.out.println("Author: Luke Skywalker - Section 001"); System.out.println("Date: August 08, 2005"); System.out.println("Course: CIS 111B -Temperature Application"); } }
_____________________________________________________________________________________________________________________________________________________
what is the code of Temperature.java ?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started