LearnFrenzy
Sign in Take a free test

Records, Enums and Sealed Classes · Predict the Output · Question 10

Records with extra methods and a factory Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

What is the output of the following Java program?

Main.java
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links