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

(program in Java) (A More Sophisticated Object) Write a class called Temperature

ID: 3763247 • Letter: #

Question

(program in Java)

(A More Sophisticated Object) Write a class called Temperature that has two instance variables: a temp value (a oating-point number) and a character for the scale, either ‘C’ for Celsius or ‘F’ for Fahrenheit. The class should have a constructor that sets each instance variable and it should include the following methods: 1. two accessor methods: one to return the degrees Celsius and the other to return the degrees Fahrenheit — use the following formulas to write the two methods: DegreesC = 5(degreesF - 32)/9 DegreesF = (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. a comparison method: it should return -1 if the invoking temperature is less than the argument temper-ature, 0 if they are equal and 1 if the invoking temperature is greater than the argument one; and 4. a toString method: returns a String representing the calling Temperature object. The numeric value of the temperature should be rounded to the nearest tenth of a degree. For example, System.out.println(new Temperature (98.164555 , F)); // should print: 98.2 F Then write a driver program called TempTester that tests all the methods from the Temperature class. Example tests: 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.

Explanation / Answer

Write a class called Temperature that has two instance variables: a temp value (a oating-point number) and a character for the scale, either ‘C’ for Celsius or ‘F’ for Fahrenheit. The class should have a constructor that sets each instance variable and it should include the following methods:

Here is the codes:

class Temperature

{

private double value;
private char scale;

public Temperature(){
value = 0;
scale = 'C';
}

public Temperature(double value, char scale){
setTemperature(value, scale);
}

public double getTempInCelsius(){
if(scale == 'C'){
return value;
}else {
return (double)5/9 *(value - 32);
}
}

public double getTempInFahrenheit(){
if(scale == 'F'){
return value;
}else {
return (double)(9/5 *(value)) + 32;
}
}
public void setValue(double value){
this.value=value;
}
public void setScale(char scale){
this.scale=scale;
}
public void setTemperature(double value, char scale){
this.value = value;
if (scale == 'F'){
this.scale = scale;
}else {
this.scale = 'C';
}
}
public boolean isGreater(Temperature another){
return (this.getTempInCelsius()> another.getTempInCelsius() );
}
public boolean equals(Temperature another){
return (this.getTempInCelsius()== another.getTempInCelsius() );
}
public boolean isSmaller(Temperature another){
return (this.getTempInCelsius()< another.getTempInCelsius() );
}
public String toString (){
return "temperature : "+value + " " + scale;
}
}
public class TemperatureConverter{
public static void main(String[] args){
Temperature temp1=new Temperature(30, 'F');
Temperature temp2=new Temperature(15, 'C');

if (temp1.isGreater( temp2)){
System.out.println(temp1 + "is greater than " + temp2);
}
else {
System.out.println(temp2 + "is greater than " + temp1);
}
System.out.println("Temperature in Fahrenheit " + temp1 + " is "+ temp1.getTempInCelsius()+" C");
System.out.println("Temperature in Celcius " + temp2 + " is "+ temp1.getTempInFahrenheit()+" F");

Temperature temp3=new Temperature(0, 'C');
Temperature temp4=new Temperature(-40, 'C');
Temperature temp5=new Temperature(100, 'C');

System.out.println("0 degree C"+ "="+ temp3.getTempInFahrenheit()+" degree F");
System.out.println("-44 degree C"+ "="+ temp4.getTempInFahrenheit()+" degree F");
System.out.println("100 degree C"+ "="+ temp5.getTempInFahrenheit()+" degree F");
}
}