• isWaterFreezing. This function should return the bool value true if the temperature stored in the temperature field is at or below the freezing point of water. Otherwise, the function should return false.
• isWaterBoiling. This function should return the bool value true if the temperature stored in the temperature field is at or above the boiling point of water. Otherwise, the function should return false.
Write a program that demonstrates the class. The program should ask the user to enter a temperature and then display a list of the substances that will freeze at that temperature and those that will boil at that temperature. For example, if the temperature is −20 the class should report that water will freeze and for 100 water will boil.
import java.util.Scanner;
class TemperatureChecker {
private double temperature;
public TemperatureChecker(double t) {
temperature = t;
}
public double getTemperature() {
return temperature;
}
/**
* Method should check if the temperature is freezing
*
* @return true if Water is freezing
*/
public boolean isWaterFreezing() {
if (temperature <= 0.0) {
return true;
} else {
return false;
}
}
/**
* Method should check if the temperature is boiling
*
* @return true if Water is boiling
*/
public boolean isWaterBoiling() {
if (temperature >= 100.0) {
return true;
} else {
return false;
}
}
}
public class App {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a temperature: ");
double temperature = keyboard.nextDouble();
// close keyboard
keyboard.close();
TemperatureChecker temperatureChecker = new TemperatureChecker(temperature);
if (temperatureChecker.isWaterFreezing()) {
System.out.println("Water will freeze.");
}
if (temperatureChecker.isWaterBoiling()) {
System.out.println("Water will boil.");
}
keyboard.close();
}
}
Comments
Leave a comment