diff --git a/bin/App.class b/bin/App.class index baa6c67..81c9871 100644 Binary files a/bin/App.class and b/bin/App.class differ diff --git a/src/App.class b/src/App.class new file mode 100644 index 0000000..4c7be38 Binary files /dev/null and b/src/App.class differ diff --git a/src/App.java b/src/App.java index 0a839f9..d64520a 100644 --- a/src/App.java +++ b/src/App.java @@ -1,5 +1,66 @@ +import java.util.InputMismatchException; +import java.util.Scanner; + public class App { - public static void main(String[] args) throws Exception { - System.out.println("Hello, World!"); + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + while (true) { + + System.out.println("\nTemperature Conversion Program "); + System.out.println("Enter your choice:"); + System.out.println("1. Celsius to Fahrenheit"); + System.out.println("2. Fahrenheit to Celsius"); + System.out.println("0. Exit"); + System.out.print("Enter your choice (1, 2, or 0): "); + + int choice; + try { + choice = scanner.nextInt(); + } catch (InputMismatchException e) { + System.out.println("Invalid input. Please enter a number (1, 2, or 0)."); + scanner.nextLine(); + continue; + } + + + if (choice == 0) { + System.out.println("Exiting program"); + break; + } + double temperature; + while (true) { + try { + System.out.print("Enter temperature to convert: "); + temperature = scanner.nextDouble(); + break; + } catch (InputMismatchException e) { + System.out.println("Invalid input. Please enter a number."); + scanner.nextLine(); + } + } + + + char conversionType = choice == 1 ? 'C' : 'F'; + + + double convertedTemperature = convertTemperature(temperature, conversionType); + System.out.printf("The converted temperature is: %.2f\n", convertedTemperature); + } + + + scanner.close(); + } + + public static double convertTemperature(double temperature, char conversionType) { + if (conversionType == 'C') { + return (temperature * 9/5) + 32; + } else if (conversionType == 'F') { + return (temperature - 32) * 5/9; + } else { + System.out.println("Invalid conversion type. Please enter 'C' or 'F'."); + return Double.NaN; + } } }