Question 10
Back to Exercise
What is the output of the following Java program?
record Temperature(double celsius) {
static Temperature ofFahrenheit(double f) { return new Temperature((f - 32) * 5 / 9); }
double fahrenheit() { return celsius * 9 / 5 + 32; }
boolean isFever() { return celsius > 37.5; }
}
public class Main {
public static void main(String[] args) {
Temperature t = Temperature.ofFahrenheit(101.3);
System.out.printf("%.1f %.1f %b %b%n", t.celsius(), new Temperature(37).fahrenheit(), t.isFever(), new Temperature(37).isFever());
}
}
- A 38.5 98.6 true false Correct
- B 38.5 98.6 false false
- C 101.3 98.6 true false
- D 38.0 98.6 true false
Correct Answer: Option A
Records can have static factories and instance methods that use their components.
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!