import java.util.Scanner;
public class TemperatureConverter {
private double temperature;
// Constructor
public TemperatureConverter(double temperature) {
this.temperature = temperature;
}
// Convert Celsius to Kelvin
public double celsiusToKelvin() {
return temperature + 273.15;
}
// Convert Celsius to Fahrenheit
public double celsiusToFahrenheit() {
return (temperature * 9 / 5) + 32;
}
// Convert Kelvin to Celsius
public double kelvinToCelsius() {
return temperature - 273.15;
}
// Convert Kelvin to Fahrenheit
public double kelvinToFahrenheit() {
return (temperature - 273.15) * 9 / 5 + 32;
}
// Convert Fahrenheit to Celsius
public double fahrenheitToCelsius() {
return (temperature - 32) * 5 / 9;
}
// Convert Fahrenheit to Kelvin
public double fahrenheitToKelvin() {
return (temperature - 32) * 5 / 9 + 273.15;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Nama : Dhafin Shabir Alfatih");
System.out.println("NIM : 3420230036");
System.out.println("---------------------------");
System.out.println("PROGRAM KONVERSI TEMPERATUR");
System.out.println("---------------------------");
System.out.println("Masukkan nilai suhu: ");
double value = scanner.nextDouble();
TemperatureConverter converter = new TemperatureConverter(value);
System.out.println("Pilih jenis konversi:");
System.out.println("1. Celcius ke Kelvin");
System.out.println("2. Celcius ke Fahrenheit");
System.out.println("3. Kelvin ke Celcius");
System.out.println("4. Kelvin ke Fahrenheit");
System.out.println("5. Fahrenheit ke Celcius");
System.out.println("6. Fahrenheit ke Kelvin");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.printf("Celcius ke Kelvin: %.2f%n", converter.celsiusToKelvin());
break;
case 2:
System.out.printf("Celcius ke Fahrenheit: %.2f%n", converter.celsiusToFahrenheit());
break;
case 3:
System.out.printf("Kelvin ke Celcius: %.2f%n", converter.kelvinToCelsius());
break;
case 4:
System.out.printf("Kelvin ke Fahrenheit: %.2f%n", converter.kelvinToFahrenheit());
break;
case 5:
System.out.printf("Fahrenheit ke Celcius: %.2f%n", converter.fahrenheitToCelsius());
break;
case 6:
System.out.printf("Fahrenheit ke Kelvin: %.2f%n", converter.fahrenheitToKelvin());
break;
default:
System.out.println("Pilihan tidak valid!");
break;
}
scanner.close();
}
}