From cd4c4ea65e62d68e41b98e1dba7e75c3a7fe12aa Mon Sep 17 00:00:00 2001 From: Kyle Wehrung Date: Fri, 21 Jul 2023 09:04:25 -0600 Subject: [PATCH 1/2] Making it convert from inchs to centimeters as well --- ch03/Convert.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/ch03/Convert.java b/ch03/Convert.java index d31fe77..d8a0a8c 100644 --- a/ch03/Convert.java +++ b/ch03/Convert.java @@ -12,6 +12,36 @@ public static void main(String[] args) { final int IN_PER_FOOT = 12; Scanner in = new Scanner(System.in); + System.out.print("Enter 1 to convert centimeters to feet and inches\n" + + "Enter 2 to convert feet and inches to centimeters\n" + + "Your choice: "); + + int choice = in.nextInt(); + + if (choice == 1) { + System.out.print("Exactly how many cm? "); + cm = in.nextDouble(); + + if (cm < 0) { + System.out.print("Please enter a positive value for centimeters."); + } else { + inches = (int) (cm / CM_PER_INCH); + feet = inches / IN_PER_FOOT; + remainder = inches % IN_PER_FOOT; + inches = inches - (feet * IN_PER_FOOT); + System.out.printf("%.2f cm = %fd ft, %d in\n", cm, feet, inches); + } + } else if (choice == 2); { + System.out.print("Enter feet: "); + feet = in.nextInt(); + System.out.print("Enter inches: "); + inches = in.nextInt(); + + if (feet < 0 || inches < 0 || inches >= IN_PER_FOOT) { + System.out.println("Please enter valid and positive values for feet and inches."); + } + } + // prompt the user and get the value System.out.print("Exactly how many cm? "); cm = in.nextDouble(); From 623e6cd4cdca60a84467e2c195b18a4976a5a5b7 Mon Sep 17 00:00:00 2001 From: Kyle Wehrung Date: Fri, 21 Jul 2023 09:15:14 -0600 Subject: [PATCH 2/2] Can convert feet to centimeters and vice-versa! --- ch03/Convert.java | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/ch03/Convert.java b/ch03/Convert.java index d8a0a8c..7f75210 100644 --- a/ch03/Convert.java +++ b/ch03/Convert.java @@ -29,9 +29,9 @@ public static void main(String[] args) { feet = inches / IN_PER_FOOT; remainder = inches % IN_PER_FOOT; inches = inches - (feet * IN_PER_FOOT); - System.out.printf("%.2f cm = %fd ft, %d in\n", cm, feet, inches); + System.out.printf("%.2f cm = %d ft, %d in\n", cm, feet, inches); } - } else if (choice == 2); { + } else if (choice == 2) { System.out.print("Enter feet: "); feet = in.nextInt(); System.out.print("Enter inches: "); @@ -39,19 +39,14 @@ public static void main(String[] args) { if (feet < 0 || inches < 0 || inches >= IN_PER_FOOT) { System.out.println("Please enter valid and positive values for feet and inches."); + } else { + cm = (feet * IN_PER_FOOT + inches) * CM_PER_INCH; + System.out.printf("%d ft, %d in = %.2f cm\n", feet, inches, cm); } + } else { + System.out.println("Invalid choice, please enter 1 or 2."); } - // prompt the user and get the value - System.out.print("Exactly how many cm? "); - cm = in.nextDouble(); - - // convert and output the result - inches = (int) (cm / CM_PER_INCH); - feet = inches / IN_PER_FOOT; - remainder = inches % IN_PER_FOOT; - System.out.printf("%.2f cm = %d ft, %d in\n", - cm, feet, remainder); } }