Question 1 TemperatureCjava and TemperatureTesterjava 20 mar
Question 1. (TemperatureC.java and TemperatureTester.java, 20 marks) Write a Java class to
represent a temperature. The class has a single data field:
temperatureC
The class has a single constructor that accepts an initial temperature provided as a double argument. If this
argument is < -273.15, set it to -273.15.
The class has the following methods:
setC – this method takes a single double argument and changes the value of the data field accordingly.
Like the constructor his method should prevent the temperature from being less than -273.15.
getC – this method returns the current value of the data field.
getF – this method computes and returns the value of temperatureC in Fahrenheit.
o C × 1.8 + 32 = F
getK – this method computes and returns the value of temperatureC in Kelvin.
o C + 273.15 = K
***Write JavaDoc comments for this class.***
Write a tester class that performs the following.
Prompt the user and collect a double value. Use this value to create a Temperature object.
Show the initial temperature in C, F and K.
Prompt the user for another value and use this value to change the temperature of your existing object.
Again, show the new values of C, F and K.
Please enter the initial temperature: 20
The current temperature in Celsius is: 20.0
The current temperature in Fahrenheit is: 68.0
The current temperature in Kelvin is: 293.15
Please enter a new temperature: 4000
The current temperature in Celsius is: 4000.0
The current temperature in Fahrenheit is: 7232.0
The current temperature in Kelvin is: 4273.15
(Extra – 5 marks) In your tester create two objects instead of one (this means prompting for two initial values). Do
the exact same work as listed above but show that the two object behave independently of each other.
Please enter the initial temperatures: 20 6000
1) The current temperature in Celsius is: 20.0
1) The current temperature in Fahrenheit is: 68.0
1) The current temperature in Kelvin is: 293.15
--------------------------------------------
2) The current temperature in Celsius is: 6000.0
2) The current temperature in Fahrenheit is: 10832.0
2) The current temperature in Kelvin is: 6273.15
Please enter a new set of temperatures: 4000 0
1) The current temperature in Celsius is: 4000.0
1) The current temperature in Fahrenheit is: 7232.0
1) The current temperature in Kelvin is: 4273.15
--------------------------------------------
2) The current temperature in Celsius is: 0.0
2) The current temperature in Fahrenheit is: 32.0
2) The current temperature in Kelvin is: 273.15
Solution
TemperatureTester.java
import java.util.Scanner;
public class TemperatureTester {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print(\"Please enter the initial temperature: \");
double tempC = scan.nextDouble();
double tempC2 = scan.nextDouble();
TemperatureC temp = new TemperatureC(tempC);
TemperatureC temp2 = new TemperatureC(tempC2);
System.out.println(\"The current temperature in Celsius is: \"+temp.getC());
System.out.println(\"The current temperature in Fahrenheit is: \"+temp.getF());
System.out.println(\"The current temperature in Kelvin is: \"+temp.getK());
System.out.println();
System.out.println(\"The current temperature in Celsius is: \"+temp2.getC());
System.out.println(\"The current temperature in Fahrenheit is: \"+temp2.getF());
System.out.println(\"The current temperature in Kelvin is: \"+temp2.getK());
System.out.print(\"Please enter a new temperature: \");
double newTempC = scan.nextDouble();
double newTempC2 = scan.nextDouble();
temp.setC(newTempC);
temp2.setC(newTempC2);
System.out.println(\"The current temperature in Celsius is: \"+temp.getC());
System.out.println(\"The current temperature in Fahrenheit is: \"+temp.getF());
System.out.println(\"The current temperature in Kelvin is: \"+temp.getK());
System.out.println();
System.out.println(\"The current temperature in Celsius is: \"+temp2.getC());
System.out.println(\"The current temperature in Fahrenheit is: \"+temp2.getF());
System.out.println(\"The current temperature in Kelvin is: \"+temp2.getK());
}
}
TemperatureC.java
public class TemperatureC {
private double temperatureC;
public TemperatureC(double temperatureC){
this.temperatureC = temperatureC;
}
public void setC(double temperatureC){
if(temperatureC > 273.15){
this.temperatureC = temperatureC;
}
else{
this.temperatureC=0;
}
}
public double getC(){
return temperatureC;
}
public double getF(){
return temperatureC*1.8 + 32;
}
public double getK(){
return temperatureC+273.15;
}
}
Output:
Please enter the initial temperature: 20 6000
The current temperature in Celsius is: 20.0
The current temperature in Fahrenheit is: 68.0
The current temperature in Kelvin is: 293.15
The current temperature in Celsius is: 6000.0
The current temperature in Fahrenheit is: 10832.0
The current temperature in Kelvin is: 6273.15
Please enter a new temperature: 4000 0
The current temperature in Celsius is: 4000.0
The current temperature in Fahrenheit is: 7232.0
The current temperature in Kelvin is: 4273.15
The current temperature in Celsius is: 0.0
The current temperature in Fahrenheit is: 32.0
The current temperature in Kelvin is: 273.15


