Java Tutorial
Java Project: Temperature Converter
Convert Celsius, Fahrenheit, and Kelvin with a small set of methods and formatted output.
What you will build
Three formulas, three methods, printf for one decimal place.
The formulas
Example
public class Main {
static double cToF(double c) { return c * 9 / 5 + 32; }
static double cToK(double c) { return c + 273.15; }
static double fToC(double f) { return (f - 32) * 5 / 9; }
public static void main(String[] args) {
double c = 21.0;
System.out.printf("%.1f C = %.1f F%n", c, cToF(c));
System.out.printf("%.1f C = %.1f K%n", c, cToK(c));
System.out.printf("70 F = %.1f C%n", fToC(70));
}
}Practice
- Compile the complete program at
/java/try. - Change one input value and compile again.
- Replace a hardcoded number with
Scannerwhen you want typed input.