Question 18
Back to Exercise
What is the output of the following Java program?
class Thermometer {
private double celsius;
private void setCelsius(double c) { celsius = Math.max(-273.15, c); }
void readCelsius(double c) { setCelsius(c); }
void readFahrenheit(double f) { setCelsius((f - 32) * 5 / 9); }
String show() { return String.format("%.1f C", celsius); }
}
public class Main {
public static void main(String[] args) {
Thermometer t = new Thermometer();
t.readFahrenheit(98.6);
String a = t.show();
t.readCelsius(-500);
System.out.println(a + " " + t.show());
}
}
- A 37.0 C -273.2 C Correct
- B 37.0 C -500.0 C
- C 98.6 C -273.2 C
- D 37.0 C -273.1 C
Correct Answer: Option A
Both public methods funnel through one private setter that clamps the value; -273.15 formatted to one decimal is -273.2.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!