write a class called Temperature that has two instance varia

write a class called Temperature that has two instance variables: a temp value ( a floating - point number -data type double ) and a character for the scale , either C or F . the class should have a constructor that sets each instance variable(assume 0 degree if no temp value and c if no scale ) and it should include the following:

two accessors method: one to return the degrees Celsius and the other to return the degrees Farenheit . use formalas c= 5 (F-32)/9 and F = (9(C/5)+32

3 mutators methods; one to set vaue, one to set scale and one to set both;

a comparison method : it should return -1 if the unvoking temperature is less than the argument temperature, 0 if they are equal and 1 if invoking temperature is greater than the argument one

a toString method; return a String representing the calling Temperature object. the numeric value of the temperature should be rounded to the nearest tenth of a degree.

Then write a driver program called TempTester that tests all the methods fromt the Temperature class.

ex: 0.0 degress C = 32.0 degrees F, 100 degrees C = 212.0 degrees F.

Solution

package chegg;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.TreeSet;

import javax.swing.text.html.HTMLDocument.Iterator;
class Temperature
{
double temp;
char c;
Temperature()
{
  temp=0;
  c=\'c\';
}
void setBoth(double t,char ch)
{
  temp=t;
  c=ch;
}
void setValue(double t)
{
  temp=t;
}
void setScale(char ch)
{
  c=ch;
}
double degreeCelsius(double f)
{
  return 5 *(f-32)/9;
  
}
double degreeFarenheit(double c)
{
  return 9*(c/5)+32;
  
}
int comparison(Temperature t)
{
  if(t.temp<temp)
  {
   return 1;
  }
  else if(t.temp>temp)
  {
   return -1;
  }
  else
   return 0;
}
public String toString(){//overriding the toString() method
    return \"value is: \"+temp + \"scale is: \"+c;
   }
}
public class Test {
public static void main(String args[]) throws IOException
{
  Temperature t = new Temperature();
  t.setBoth(10, \'c\');
  System.out.println(t);
}

}

Code:

write a class called Temperature that has two instance variables: a temp value ( a floating - point number -data type double ) and a character for the scale , e
write a class called Temperature that has two instance variables: a temp value ( a floating - point number -data type double ) and a character for the scale , e

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site