Question
public class Thermometer { private float currentTemperature = 283.15f; //A default is 283.15 Kelvin public float getTemperature() { return currentTemperature; } } public abstract class
public class Thermometer {
private float currentTemperature = 283.15f; //A default is 283.15 Kelvin
public float getTemperature() {
return currentTemperature;
}
}
public abstract class Display {
protected Thermometer t;
protected Converter c;
Display(){
c = new Converter();
t = new Thermometer();
}
/**
* Prints the current temperature -- in any unit.
*/
public abstract void displayReading();
}
public class DisplayCelsius extends Display {
/**
* Prints the current temperature - in Celsius.
*/
public void displayReading() {
System.out.println("Displaying: " + c.fromKalvinToCelsius(t.getTemperature()) + "C");
}
}
public class DisplayFahrenheit extends Display {
/**
* Prints the current temperature - in Fahrenheit.
*/
public void displayReading() {
System.out.println("Displaying: " + c.fromKalvinToFahrenheit(t.getTemperature()) + "F");
}
}
public class Refrigerator {
public static void main(String[] args) {
// The following switching needs to take place using strategy.
Display d = new DisplayFahrenheit();
d.displayReading();
d = new DisplayCelsius();
d.displayReading();
}
}
/**
* Temperature converter for your convenience.
*/
public class Converter {
/**
* @param The temperature in Kalvin
* @return The temperature in Fahrenheit
*/
public float fromKalvinToFahrenheit(float a){
return (float) ((a-273.15)*9.0/5.0+32);
}
/**
* @param The temperature in Kalvin
* @return The temperature in Celsius
*/
public float fromKalvinToCelsius(float a){
return (float) (a - 273.15);
}
}
the Fahrenheit and Celsius units are T(Fahrenheit)=(T(Kalvin)273.15)9.0/5.0+32 and T(Celsius)=T(Kalvin)273.15 You have devised an implementation that is based on inheritance, in which you have an abstract Display, specialized according the unit of interest as follows: convenienceStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started