From c6a4632db27c42817e432f8ebb2e2d910ebe197f Mon Sep 17 00:00:00 2001 From: csprtheghst Date: Sat, 10 Feb 2024 14:18:02 -0800 Subject: [PATCH] Completed Java Lab 003 --- .idea/misc.xml | 2 +- Java-Lab-003.iml | 1 + README.md | 13 +++++++++++++ src/Printf.java | 9 +++++++++ src/SecondsToTimeConverter.java | 14 ++++++++++++++ src/TempConvert.java | 11 +++++++++++ 6 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/Printf.java create mode 100644 src/SecondsToTimeConverter.java create mode 100644 src/TempConvert.java diff --git a/.idea/misc.xml b/.idea/misc.xml index 639900d..69ace3f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/Java-Lab-003.iml b/Java-Lab-003.iml index b46c0dd..e1006ff 100644 --- a/Java-Lab-003.iml +++ b/Java-Lab-003.iml @@ -5,6 +5,7 @@ + \ No newline at end of file diff --git a/README.md b/README.md index cdcf86f..17877f3 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,19 @@ For each of the issues above, screenshot and add the error images to this repo n ### **Note:** Your answers to this exercise should be added using Markdown into the project's `README.md` file so that they get committed with the project's code.. +--- + +Errors in Printf format: + +Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.Integer +at java.base/java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4515) +at java.base/java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:3079) +at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:3027) +at java.base/java.util.Formatter.format(Formatter.java:2791) +at java.base/java.io.PrintStream.implFormat(PrintStream.java:1367) +at java.base/java.io.PrintStream.format(PrintStream.java:1346) +at java.base/java.io.PrintStream.printf(PrintStream.java:1245) +at Printf.main(Printf.java:5) --- diff --git a/src/Printf.java b/src/Printf.java new file mode 100644 index 0000000..55a3184 --- /dev/null +++ b/src/Printf.java @@ -0,0 +1,9 @@ +public class Printf { + public static void main(String[] args) { + int intValue = 10; + double doubleValue = 5.75; + System.out.printf("Displaying an int using %%f: %f\n", intValue); + System.out.printf("Displaying a double using %%d: %d\n", doubleValue); + System.out.printf("Providing only one value for two format specifiers: %d %f\n", intValue); + } +} \ No newline at end of file diff --git a/src/SecondsToTimeConverter.java b/src/SecondsToTimeConverter.java new file mode 100644 index 0000000..deba0bc --- /dev/null +++ b/src/SecondsToTimeConverter.java @@ -0,0 +1,14 @@ +import java.util.Scanner; +public class SecondsToTimeConverter { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.print("Enter a total number of seconds: "); + int totalSeconds = scanner.nextInt(); + int hours = totalSeconds / 3600; + int remainingSeconds = totalSeconds % 3600; + int minutes = remainingSeconds / 60; + int seconds = remainingSeconds % 60; + System.out.printf("%d seconds = %d hours, %d minutes, and %d seconds\n", totalSeconds, hours, minutes, seconds); + scanner.close(); + } +} diff --git a/src/TempConvert.java b/src/TempConvert.java new file mode 100644 index 0000000..08e2775 --- /dev/null +++ b/src/TempConvert.java @@ -0,0 +1,11 @@ +import java.util.Scanner; +public class TempConvert { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.print("Enter a temperature in Celsius: "); + double celsius = scanner.nextDouble(); + double fahrenheit = celsius * 9 / 5 + 32; + System.out.printf("%.1f C = %.1f F\n", celsius, fahrenheit); + scanner.close(); + } +}