Question

Create a program that the simulates a thermometer and displays its temperature using different units. 1-...

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

Homework Answers

Answer #1

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
a. The Talk-A-Lot Cell Phone Company provides phone services for its customers. Create an abstract class...
a. The Talk-A-Lot Cell Phone Company provides phone services for its customers. Create an abstract class named PhoneCall that includes a String field for a phone number and a double field for the price of the call. Also include a constructor that requires a phone number parameter and that sets the price to 0.0. Include a set method for the price. Also include three abstract get methods - one that returns the phone number, another that returns the price of...
Download the attached .java file. Run it, become familiar with its processes. Your task is to...
Download the attached .java file. Run it, become familiar with its processes. Your task is to turn TemperatureConversion into GUI based program. it should, at the least, perform similar functions as their text output versions. The key factor to remember is that the workings should remain the same (some tweaks may be necessary) between text and GUI programs, while the means pf visual presentation and user interaction changes. You must properly document, comment, indent, space, and structure both programs. import...
Class VacationPackage java.lang.Object triptypes.VacationPackage Constructor Summary Constructors Constructor and Description VacationPackage(java.lang.String name, int numDays) Initializes a...
Class VacationPackage java.lang.Object triptypes.VacationPackage Constructor Summary Constructors Constructor and Description VacationPackage(java.lang.String name, int numDays) Initializes a VacationPackage with provided values. Method Summary All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method and Description boolean equals(java.lang.Object other) Provides a logical equality comparison for VacationPackages and any other object type. double getAmountDue() This method provides the remaining amount due to the travel agent for this trip less any deposit made upfront. abstract double getDepositAmount() This method provides the required...
Do the TODOs in SongFileAccessor.java. It inherits from FileAccessor class. TODO 1: Implement the processLine method....
Do the TODOs in SongFileAccessor.java. It inherits from FileAccessor class. TODO 1: Implement the processLine method. When the text file is processed, each line of text will be passed to processLine . Each line contains 4 fields: title, album, artist, and play time. The album field is optional. Each field is separated by a comma. TODO 2: Implement the songToCSVString method. This method takes a Song object as a parameter and returns a String which is the csv representation of...
import java.util.ArrayList; /* Rules:         1. Allow Tester to iterate through all nodes using the...
import java.util.ArrayList; /* Rules:         1. Allow Tester to iterate through all nodes using the in-order traversal as the default.             This means, in Tester the following code should work for an instance of this class             called bst that is storing Student objects for the data:                 BinarySearchTree_Lab08<String> bst = new BinarySearchTree_Lab08<String>();                 bst.add("Man");       bst.add("Soda");   bst.add("Flag");                 bst.add("Home");   bst.add("Today");   bst.add("Jack");                ...