Write a Temperature class that represents temperatures in degrees in both Celsiu
ID: 3624696 • Letter: W
Question
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 following formulas:
Degrees_C = 5 (Degrees_F - 32) / 9
Degrees_F = (9 (Degrees_C) / 5) + 32
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.
Explanation / Answer
// Write a Temperature class that represents temperatures in degrees in both Celsius and Fahrenheit.
public class Temperature
{
// Use a floating-point number for the temperature and a character for the scale: either 'C' for Celsius or 'F' for Fahrenheit. T
private double temperature;
private char scale;
// 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.
public Temperature()
{
this(0.0, 'C');
}
public Temperature(double temp)
{
this(temp, 'C');
}
public Temperature(char scale)
{
this(0.0, scale);
}
public Temperature(double temp, char s)
{
temperature = temp;
scale = s;
}
// Two accessor methods: one to return the temperature in degrees Celsius,the other to return it in degrees Fahrenheit. Use the following formulas:
// Degrees_C = 5 (Degrees_F - 32) / 9
// Degrees_F = (9 (Degrees_C) / 5) + 32
// and round to the nearest tenth of a degree.
public double getDegreesF()
{
if(scale == 'F')
{
return round(temperature);
}
else
{
return round(9*temperature/5+32);
}
}
public double getDegreesC()
{
if(scale == 'C')
{
return round(temperature);
}
else
{
return round(5*(temperature-32)/9);
}
}
private double round(double input)
{
return (double)(((int)(input*10))/10.0);
}
// Three set methods: one to set the number of degrees, one to set the scale, and one to set both.
public void setDegrees(double temp)
{
temperature = temp;
}
public void setScale(char s)
{
scale = s;
}
public void setTemperature(double temp, char scale)
{
setDegrees(temp);
setScale(scale);
}
// 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.
public boolean isEqualTo(Temperature rhs)
{
return rhs.getDegreesC() == getDegreesC();
}
public boolean isLessThan(Temperature rhs)
{
return rhs.getDegreesC() > getDegreesC();
}
public boolean isGreaterThan(Temperature rhs)
{
return rhs.getDegreesC() < getDegreesC();
}
}
public class Test
{
public static void main(String[] args)
{
Temperature iceC = new Temperature();
Temperature iceF = new Temperature('F');
iceF.setDegrees(32.0);
Temperature equalC = new Temperature(-40.0, 'C');
Temperature equalF = new Temperature(-40.0);
equalF.setScale('F');
Temperature boilC = new Temperature(100.0, 'C');
Temperature boilF = new Temperature(212.0, 'F');
System.out.println(boilC.isEqualTo(boilF));
System.out.println(equalC.isEqualTo(equalF));
System.out.println(iceC.isEqualTo(iceF));
System.out.println(boilC.isLessThan(iceF));
System.out.println(iceF.isGreaterThan(equalC));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.