Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Write a Temperature class that represents temperatures in degrees in .docx
1. Write a Temperature class that represents temperatures in
degrees in both Celsius and Fahrenheit. Use a floating-point
number for the temperature and character for the scale: either
'C' for Celsius or 'F' for Fahrenheit. The class should have
Four constructor: 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
formulas as listed below from Practice Program 5 of Chapter 3
and round to the nearest tenth of a degree. C = 5 (F - 32)/9 F =
9 * C/5 + 32 Three set methods: one to set the number of
degree, one to set 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 (a demonstration program using main method)
that tests all the methods. Begin the demo program by
displaying: Program title Java class code (CSCl-125 or 502),
Author name (programmer),ID and sequences of operational
stops Be sure to involve each of the constructors, to include at
least one true and one false 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 -10.0
degrees F, and 100.0 degrees C said 212.0 degrees F. Set up a
loop using Yes/No button for repeating the testing for various
temperature pairs All L/O statements must be in GUI style
Solution
2. /* Temperature class*/
class Temperature
{
//instant variables//////////////////////////////////////////////
private float value;
private char scale;
/////////////////////the four constructors///////////////////////
public Temperature(float value)
{
this.value=value;
this.scale='C';
}
public Temperature(char scale)
{
this.value=0;
this.scale=scale;
}
public Temperature(float value,char scale)
{
this.value=value;
this.scale=scale;
}
public Temperature()
{
7. return true;
}
else
{
return false;
}
}
else
{
if (value < obj.getFahrenhite())
{
return true;
}
else
{
return false;
}
}
}
/////////////////////////////useOfToString/////////////////////////////
public String toString()
{
if (scale == 'C')
{
return "The Temperature in celsius is:ttt"+value+" The
8. Temperature in Fahrenhite is:tt"+getFahrenhite();
}
else
{
return "The Temperature in celsius
is:ttt"+getCelsius()+" The Temperature in Fahrenhite
is:tt"+value;
}
}
}
///////////////////////// TEST CLASS///////////////////////////
public class TestTemperature
{
public static void main(String[] args)
{
//0.0 Celsius
Temperature obj1 = new Temperature();
//32.0 Fahrenhite
Temperature obj2 = new Temperature(32, 'F');
//-40.0 Celsius
Temperature obj3 = new Temperature(-40);
//-40.0 Fahrenhite
Temperature obj4 = new Temperature(-40,'F');
//100.0 Celsius
Temperature obj5 = new Temperature(100,'C');
9. //212.0 Fahrenhite
Temperature obj6 = new Temperature(212, 'F');
//Runing the program
//toString of object 1 to test it's Values
System.out.println("Object 1 ");
System.out.println(obj1);
//toString of Object 2 to test it's Values
System.out.println(" Object 2 ");
System.out.println(obj2);
//toString of Object 3 to test it's Values
System.out.println(" Object 3 ");
System.out.println(obj3);
//toString of Object 4 to test it's Values
System.out.println(" Object 4 ");
System.out.println(obj4);
//toString of Object 5 to test it's Values
System.out.println(" Object 5 ");
System.out.println(obj5);
//toString of Object 6 to test it's Values
System.out.println(" Object 6 ");
System.out.println(obj6) ;
System.out.println("--------------- ---------------");
System.out.println("Checking if object 3 is less than object
1");
System.out.println(obj3.isLess(obj1)+" ");
10. System.out.println("Checking if object 4 is greater than object
5");
System.out.println(obj4.isGreater(obj5)+" ");
System.out.println("Getting Celsius of object 4");
System.out.println(obj4.getCelsius()+" ");
System.out.println("Getting Fahrenhite of object 3");
System.out.println(obj3.getCelsius()+" ");
System.out.println("Setting the value 15 to object 1");
obj1.setValue(15);
System.out.println("Printing out the results:");
System.out.println(obj1+" ");
System.out.println("Setting the scale C to object 2");
obj2.setScale('C');
System.out.println("Printing out the results:");
System.out.println(obj2+" ");
System.out.println("Setting the scale F and the value 34 to
object 5");
obj5.setBoth(34,'F');
System.out.println("Printing out the results:");
System.out.println(obj5+" ");
}
}