In: Computer Science
JAVA
Write a Temperature class that has two instance variables: a temperature value (a floating-point number) and a character for the scale, either C for Celsius or F for Fahrenheit. The class should have four constructor methods: one for each instance variable (assume zero degrees if no value is specified and Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument constructor (set to zero degrees Celsius). Include the following: (1) two accessor methods to return the temperature—one to return the degrees Celsius, the other to return the degrees Fahrenheit—use the following formulas to write the two methods, and round to the nearest tenth of a degree:
DegreesC=5(degreesF−32)/9DegreesF=(9(degreesC)/5+32);
DegreesC=5(degreesF−32)/9DegreesF=(9(degreesC)/5+32);
(2) three mutator methods: one to set the value, one to set the scale (F or C), and one to set both; (3) three comparison methods: an equals method to test whether two temperatures are equal, one method to test whether one temperature is greater than another, and one method to test whether one temperature is less than another (note that a Celsius temperature can be equal to a Fahrenheit temperature as indicated by the above formulas); and (4) a suitable toString method. Then write a driver program (or programs) that tests all the methods. Be sure to use each of the constructors, to include at least one true and one false case for each of the comparison methods, and to test at least the following temperature equalities: 0.0 degrees C = 32.0 degrees F, –40.0 degrees C = –40.0 degrees F, and 100.0 degrees C = 212.0 degrees F.
Temperature.java
public class Temperature {
private double value;
private char scale;
public Temperature()
{
setScale('C');
setValue(0.0);
}
public Temperature(double value) {
setValue(value);
setScale('C');
}
public Temperature(char scale) {
setValue(0.0);
setScale(scale);
}
public Temperature(double value, char scale) {
setValue(value);
setScale(scale);
}
public double getValue() {
return value;
}
public char getScale() {
return scale;
}
public double getCelsius()
{
return(5 * (value - 32) / 9);
}
public double getFahrenheit()
{
return(((9 * value) / 5) + 32);
}
public void setValue(double value) {
this.value = value;
}
public void setScale(char scale) {
if(scale != 'C' && scale != 'F')
this.scale = 'C';
else
this.scale = Character.toUpperCase(scale);
}
public void setValueAndScale(double value, char scale)
{
setValue(value);
setScale(scale);
}
public boolean checkEqual(Temperature other)
{
if(getScale() == other.getScale())
{
return(getValue() == other.getValue());
}
else
{
if(getScale() == 'C' && other.getScale() == 'F')
return(getValue() == other.getCelsius());
else
return(getValue() == other.getFahrenheit());
}
}
public boolean checkGreater(Temperature other)
{
if(getScale() == other.getScale())
{
return(getValue() > other.getValue());
}
else
{
if(getScale() == 'C' && other.getScale() == 'F')
return(getValue() > other.getCelsius());
else
return(getValue() > other.getFahrenheit());
}
}
public boolean checkLesser(Temperature other)
{
if(getScale() == other.getScale())
{
return(getValue() < other.getValue());
}
else
{
if(getScale() == 'C' && other.getScale() == 'F')
return(getValue() < other.getCelsius());
else
return(getValue() < other.getFahrenheit());
}
}
@Override
public String toString()
{
return(String.format("%.1f", getValue()) + "" + getScale());
}
}
Test.java
public class Test {
public static void main(String[] args) {
Temperature t1 = new Temperature();
Temperature t2 = new Temperature('F');
Temperature t3 = new Temperature(-40);
Temperature t4 = new Temperature(100, 'C');
System.out.println("Displaying all four temperature
instances..");
System.out.println("T1 = " + t1);
System.out.println("T2 = " + t2);
System.out.println("T3 = " + t3);
System.out.println("T4 = " + t4);
System.out.println("\nEquivalent Fahrenheit value of T1: " +
String.format("%.1fF", t1.getFahrenheit()));
System.out.println("Equivalent Celsius value of T2: " +
String.format("%.1fC", t2.getCelsius()));
System.out.println("Equivalent Fahrenheit value of T3: " +
String.format("%.1fF", t3.getFahrenheit()));
System.out.println("Equivalent Fahrenheit value of T4: " +
String.format("%.1fF", t4.getFahrenheit()));
System.out.println("\nComparing temperatures..");
System.out.println("0C equals 32F: " + t1.checkEqual(new
Temperature(32, 'F')));
System.out.println("0C equals 23F: " + t1.checkEqual(new
Temperature(23, 'F')));
System.out.println("0F is greater than 0C: " +
t1.checkGreater(t2));
System.out.println("-40C is greater than 100C: " +
t3.checkGreater(t4));
System.out.println("-40C is lesser than 212F: " +
t3.checkLesser(new Temperature(212, 'F')));
System.out.println("100C is lesser than 21C: " + t4.checkLesser(new
Temperature(21, 'C')));
System.out.println("\nSome equality comparisons..");
Temperature t11 = new Temperature();
Temperature t22 = new Temperature(32, 'F');
Temperature t33 = new Temperature(-40, 'C');
Temperature t44 = new Temperature(-40, 'C');
Temperature t55 = new Temperature(100, 'C');
Temperature t66 = new Temperature(212, 'F');
System.out.println("0.0 degrees C = 32.0 degrees F: " +
t11.checkEqual(t22));
System.out.println("-40.0 degrees C = -40.0 degrees F: " +
t33.checkEqual(t44));
System.out.println("100.0 degrees C = 212.0 degrees F: " +
t55.checkEqual(t66));
}
}
******************************************************** SCREENSHOT ********************************************************