Create a program that the simulates a thermometer and displays its temperature using different units.
1- Temperature Interface (Temperature.java)
This interface should declare the following two abstract
methods:
• getTemperature
o Returns a String o Accepts no arguments
• setTemperature
o Returns void o Accepts one double argument
2- Abstract Thermometer Class (Thermometer.java)
This abstract class must implement the Temperature interface and
contain:
• One private field (a double) named degrees
• One constructor that accepts a double argument to set the degrees field
• A getter and setter for the degrees field
• An abstract method named convert
o Accepts no arguments o Returns a String.
3- FahrenheitThermometer Class
(FahrenheitThermometer.java)
This class must be a subclass of Thermometer and
include:
• No fields
• One constructor that accepts a double argument to set the degrees field in the superclass.
• An implementation of the getTemperature method (required by the abstract superclass’s interface)
o Returns a String that is the degrees F o For example, 45.7 F (if the superclass’s degrees field was 45.7)
• An implementation of the setTemperature method (required by the abstract superclass’s interface)
o Simply sets the superclass’s degrees field
• An implementation of the convert method (required by the abstract superclass)
o Returns the temperature in Celsius
▪ Degrees Celsius = (Degrees Fahrenheit - 32) * (5/9) o Returns a String that is the degrees C o For example, 7.6 C (if the superclass’s degrees field was 45.7)
4- CelsiusThermometer Class
(CelsiusThermometer.java)
This class must be a subclass of Thermometer and
include:
• No fields
• One constructor that accepts a double argument to set the degrees field in the superclass.
• An implementation of the getTemperature method (required by the interface)
o Returns a String that is the degrees C o For example, 62.8 C (if the superclass’s degrees field was 62.8)
• An implementation of the setTemperature method (required by the interface)
o Simply sets the superclass’s degrees field
• An implementation of the convert method (required by the abstract superclass)
o Returns the temperature in Fahrenheit
▪ Degrees Fahrenheit = (Degrees Celsius * (9/5)) + 32 o Returns a String that is the degrees F o For example, 145.04 F (if the superclass’s degrees field was 62.8)
5- Thermometer Application (ThermoApp.java)
In the main method:
• Prompt the user to choose either Fahrenheit or Celsius. o You may implement this as the user entering “1 or 2”, “F or C”, etc.
• Prompt the user to enter the degrees.
• Instantiate either a FahrenheitThermometer or CelsiusThermometer object (based on the user’s choice) and assign it to the Thermometer variable that has been declared for you.
o For example: therm = new FahrenheitTemperature(degrees);
• Uncomment the two lines that pass the Thermometer variable to the displayTemperature and displayConversion methods.
Sample Input/Output
Choose Fahrenheit (F) or Celsius (C): F
Enter the degrees: 89.5
The temperature is 89.5 F
The temperature converted is 31.9 C
public interface Temperature {
String getTemperature();
void setTemperature(double temperature);
}
public abstract class Thermometer implements Temperature{
private double degrees;
public Thermometer(double degrees){
this.degrees=degrees;
}
public double getDegrees() {
return degrees;
}
public void setDegrees(double degrees) {
this.degrees = degrees;
}
public abstract String convert();
}
public class FahrenheitThermometer extends Thermometer{
public FahrenheitThermometer(double degrees)
{
super(degrees);
}
@Override
public String getTemperature() {
// TODO Auto-generated method
stub
return this.getDegrees()+"F";
}
@Override
public void setTemperature(double temperature) {
// TODO Auto-generated method
stub
this.setDegrees(temperature);
}
@Override
public String convert() {
// TODO Auto-generated method
stub
return
Math.round(((this.getDegrees()-32)*((double)5/9))*10)/10.0+"C";
}
}
public class CelsiusThermometer extends Thermometer{
public CelsiusThermometer(double degrees) {
super(degrees);
}
@Override
public String getTemperature() {
// TODO Auto-generated method
stub
return this.getDegrees()+"C";
}
@Override
public void setTemperature(double temperature) {
// TODO Auto-generated method
stub
this.setDegrees(temperature);
}
@Override
public String convert() {
// TODO Auto-generated method
stub
return
Math.round(((this.getDegrees()*((double)9/5))+32)*10)/10.0+"F";
}
}
import java.util.Scanner;
public class ThermoApp {
public static void main(String[] args) {
// TODO Auto-generated method
stub
Scanner sc=new
Scanner(System.in);
System.out.println("Choose
Fahrenheit (F) or Celsius (C):");
String inp=sc.nextLine();
Thermometer therm=null;
System.out.println("Enter the
degrees:");
double
degrees=sc.nextDouble();
if(inp.equalsIgnoreCase("F")){
therm=new
FahrenheitThermometer(degrees);
}
else{
if(inp.equalsIgnoreCase("C")){
therm=new CelsiusThermometer(degrees);
}
}
System.out.println("The temperature
is "+therm.getTemperature());
System.out.println("The temperature
converted is "+therm.convert());
}
}
Expected output:
Get Answers For Free
Most questions answered within 1 hours.