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

Instructions/Design Design and implement a class, called Temperature , to repres

ID: 3598767 • Letter: I

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

____________________________________________________________________________________________________________________________________________________

_____________________________________________________________________________________________________________________________________________________

what is the code of Temperature.java ?

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.

Explanation / Answer

import java.util.*;

import java.lang.*;

import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */

public class Temperature

{

               double degrees;

               char type;

               Temperature(double degrees,char type)

               {

                              this.degrees=degrees;

                              this.type=type;

               }

               Temperature(double degrees)

               {

                              this.degrees=degrees;

                              type='C';

               }

               Temperature(char type)

               {

                              this.degrees=0.0;

                              this.type=type;

               }

               Temperature()

               {

                              this.degrees=0.0;

                              this.type='C';

               }

               void writeOutput()

               {

                              System.out.println("Temperature = "+degrees+" "+type);

               }

               double getC()

               {

                   if(type=='C'||type=='c')return Math.round(degrees*10)/10.0;

                   else {

                       double temp=(degrees-32)*(0.5556);

                       return Math.round(temp*10)/10.0;

                   }

               }

               double getF()

               {

                   if(type=='F'||type=='f')return Math.round(degrees*10)/10.0;

                   else

                   {

                    double temp=   ((9/5) * degrees + 32);

                    return Math.round(temp*10)/10.0;

                   }

               }

               void writeC()

               {

                   System.out.println("Temperature in Celsius = "+getC());

               }

               void writeF()

               {

                    System.out.println("Temperature in Fahrenheit = "+getF());

               }

               void set(double degrees)

               {

                   this.degrees=degrees;

               }

               void set(char type)

               {

                   this.type=type;

               }

               void set(double degrees, char type)

               {

                   this.degrees=degrees;

                   this.type=type;

               }

               boolean equals(Temperature T)

               {

                   double t1=this.getC();

                   double t2=T.getC();

                   if(t1==t2)

                   return true;

                   else return false;

               }

               public String toString()

               {

                   return "temperature "+degrees+type;  

               }

               void readInput()

               {

                   Scanner sc=new Scanner(System.in);

                   System.out.println("Enter degrees");

                   degrees=sc.nextDouble();

                   System.out.println("Enter type");

                   type=sc.next().charAt(0);

                  

                   if((type!='c')&&(type!='C')&&(type!='F')&&(type!='f'))

                   System.out.println("INVALID INPUT");

               }

              

}