Temperature Converter in Java: Explanation & Practice

Create a temperature conversion class

Problem summary

Create a Temperature class that converts between Celsius, Fahrenheit, and Kelvin.

Starter code

// Create Temperature class with:
// - double celsius
// - double toFahrenheit() - C * 9/5 + 32
// - double toKelvin() - C + 273.15
// - static Temperature fromFahrenheit(double f)

public class Main {
    public static void main(String[] args) {
        // Test case 1: 25 Celsius
        // Print: C: <c>, F: <f>, K: <k>
    }
}

Expected output and test cases

  • Convert 25C
    C: 25.0, F: 77.0, K: 298.15
  • Freezing point
    C: 0.0, F: 32.0, K: 273.15
  • Boiling point
    C: 100.0, F: 212.0, K: 373.15

Hints

  1. F = C * 9/5 + 32
  2. K = C + 273.15
  3. From F: C = (F - 32) * 5/9

Related OOP Basics exercises

Practice all OOP Basics exercises · Run this idea in the Java compiler