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
- F = C * 9/5 + 32
- K = C + 273.15
- From F: C = (F - 32) * 5/9
Related OOP Basics exercises
- Practice Time Class in Java
- Practice Fraction Class in Java
- Practice Point Distance Calculator in Java
Practice all OOP Basics exercises · Run this idea in the Java compiler