From 461de395e1cf086fb384fd07ac3c55b78bfab170 Mon Sep 17 00:00:00 2001 From: Jeremy Miller Date: Tue, 26 Mar 2019 15:33:02 -0700 Subject: [PATCH 1/9] compile and run --- ch01/Hello.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch01/Hello.java b/ch01/Hello.java index 593557b..2451f4d 100644 --- a/ch01/Hello.java +++ b/ch01/Hello.java @@ -2,7 +2,7 @@ public class Hello { public static void main(String[] args) { // generate some simple output - System.out.println("Hello, World!"); + System.out.println("ello, World!"); } } From cc2b55a87304ca48cb961481964bbe50d5792792 Mon Sep 17 00:00:00 2001 From: Jeremy Miller Date: Tue, 26 Mar 2019 15:38:47 -0700 Subject: [PATCH 2/9] finish celcius to farenheit --- ch03/celciusToFarenheit.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 ch03/celciusToFarenheit.java diff --git a/ch03/celciusToFarenheit.java b/ch03/celciusToFarenheit.java new file mode 100644 index 0000000..1ac1a07 --- /dev/null +++ b/ch03/celciusToFarenheit.java @@ -0,0 +1,25 @@ +import java.util.Scanner; + +/** + * Converts celcius temperature to farenheit temperature + */ +public class celciusToFarenheit { + + public static void main(String[] args) { + double celcius; + final double slopeNumerator = 9.0; + final double slopeDenominator = 5.0; + final double intercept = 32.0; + double farenheit; + + Scanner in = new Scanner(System.in); + + // prompt the user for a value + System.out.println("What is the temperature in Celcius? "); + celcius = in.nextDouble(); + + // perform conversion + farenheit = celcius * slopeNumerator / slopeDenominator + intercept; + System.out.printf("%.2f degrees celcius = %.2f degrees farenheit.\n", celcius, farenheit); + } +} \ No newline at end of file From 8caa1ecdb858ea4628a968516523f2634ed30bc5 Mon Sep 17 00:00:00 2001 From: Jeremy Miller Date: Tue, 26 Mar 2019 15:46:37 -0700 Subject: [PATCH 3/9] finish scanner bug --- ch03/ScannerBug.java | 1 + 1 file changed, 1 insertion(+) diff --git a/ch03/ScannerBug.java b/ch03/ScannerBug.java index 353affb..de1e012 100644 --- a/ch03/ScannerBug.java +++ b/ch03/ScannerBug.java @@ -18,6 +18,7 @@ public static void main(String[] args) { System.out.print("What is your age? "); age = in.nextInt(); + in.nextLine(); // extra newline needed for reading str after int, have to read in the trailing newline character otherwise remaining data will not be read System.out.print("What is your name? "); name = in.nextLine(); System.out.printf("Hello %s, age %d\n", name, age); From 3dce4f23139c114b8c61863b8586d2f65362f95e Mon Sep 17 00:00:00 2001 From: Jeremy Miller Date: Tue, 26 Mar 2019 15:52:45 -0700 Subject: [PATCH 4/9] start seconds converter --- ch03/secondsConverter.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 ch03/secondsConverter.java diff --git a/ch03/secondsConverter.java b/ch03/secondsConverter.java new file mode 100644 index 0000000..cbe622b --- /dev/null +++ b/ch03/secondsConverter.java @@ -0,0 +1,12 @@ +import java.util.scanner; + +/** + * Converts and input number of seconds to Hours, Minutes, Seconds output + */ + +public class secondsConverter { + + public static void main(String[] args) { + + } +} \ No newline at end of file From 189887660b3c98fb580621ea19c58b235f84ff54 Mon Sep 17 00:00:00 2001 From: Jeremy Miller Date: Tue, 26 Mar 2019 16:03:59 -0700 Subject: [PATCH 5/9] seconds converter --- ch03/secondsConverter.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/ch03/secondsConverter.java b/ch03/secondsConverter.java index cbe622b..7ad87fc 100644 --- a/ch03/secondsConverter.java +++ b/ch03/secondsConverter.java @@ -1,4 +1,4 @@ -import java.util.scanner; +import java.util.Scanner; /** * Converts and input number of seconds to Hours, Minutes, Seconds output @@ -7,6 +7,21 @@ public class secondsConverter { public static void main(String[] args) { + int seconds; + int minutes; + int hours; + int remainingSeconds; + int remainingMinutes; + Scanner in = new Scanner(System.in); + + System.out.println("How many seconds? "); + seconds = in.nextInt(); + minutes = seconds / 60; + remainingSeconds = seconds % 60; + hours = minutes / 60; + remainingMinutes = minutes % 60; + + System.out.printf("%d Hours, %d Minutes, %d Seconds\n", hours, remainingMinutes, remainingSeconds); } } \ No newline at end of file From 799018caaeadeaf816e165837dfd607593eaabcd Mon Sep 17 00:00:00 2001 From: Jeremy Miller Date: Tue, 26 Mar 2019 16:13:49 -0700 Subject: [PATCH 6/9] finish number guesser --- ch03/numberGuess.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 ch03/numberGuess.java diff --git a/ch03/numberGuess.java b/ch03/numberGuess.java new file mode 100644 index 0000000..e341c0f --- /dev/null +++ b/ch03/numberGuess.java @@ -0,0 +1,26 @@ +import java.util.Random; +import java.util.Scanner; + +/** + * A simple number guessing game + */ + +public class numberGuess { + public static void main(String[] args) { + // pick a random number + Random rand = new Random(); + final int number = rand.nextInt(100) +1; + // get user input + System.out.println("I'm thinking of a number between 1 and 100. Can you guess?"); + Scanner in = new Scanner(System.in); + int guess; + guess = in.nextInt(); + // return guess to user + System.out.printf("You guessed %d!\n", guess); + // share my number + System.out.printf("I was thinking of %d!\n", number); + // share difference + final int difference = guess - number; + System.out.printf("You were off by %d...\n", difference); + } +} \ No newline at end of file From 6383fd9f0f78ef1845745eab84da21a447066c98 Mon Sep 17 00:00:00 2001 From: Jeremy Miller Date: Tue, 26 Mar 2019 16:58:56 -0700 Subject: [PATCH 7/9] finish print american date --- ch04/printAmerican.java | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 ch04/printAmerican.java diff --git a/ch04/printAmerican.java b/ch04/printAmerican.java new file mode 100644 index 0000000..6e37a21 --- /dev/null +++ b/ch04/printAmerican.java @@ -0,0 +1,44 @@ +import java.util.Scanner; + + +/** + * Gets data input from user and returns date in the American format. + */ + +public class printAmerican { + + public static void printAmerican(String month, String day, int date, int year) { + System.out.printf("Today is %s, %s %d, %d\n", day, month, date, year); + + } + + public static void printEuropean(String month, String day, int date, int year) { + System.out.printf("Today is %s, %d %s, %d\n", day, date, month, year); + + } + + + public static void main(String[] args) { + // declare variables + String month; + String day; + int date; + int year; + + // instantiate scanner + Scanner in = new Scanner(System.in); + // get data from user + System.out.println("\nWhat is the month? \n"); + month = in.nextLine(); + System.out.println("\nWhat is the day of the week? \n"); + day = in.nextLine(); + System.out.println("\nWhat is the day of the month (1-31)? \n"); + date = in.nextInt(); + System.out.println("\nWhat is the year? \n"); + year = in.nextInt(); + + // call print method + printAmerican(month, day, date, year); + printEuropean(month, day, date, year); + } +} \ No newline at end of file From 55df140198747b87dce93f70d94dcc03f39e8b14 Mon Sep 17 00:00:00 2001 From: Jeremy Miller Date: Tue, 26 Mar 2019 17:15:50 -0700 Subject: [PATCH 8/9] add error handling --- ch04/printAmerican.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/ch04/printAmerican.java b/ch04/printAmerican.java index 6e37a21..3299bd7 100644 --- a/ch04/printAmerican.java +++ b/ch04/printAmerican.java @@ -29,12 +29,31 @@ public static void main(String[] args) { Scanner in = new Scanner(System.in); // get data from user System.out.println("\nWhat is the month? \n"); + if (! in.hasNextLine()) { + System.err.println("Error, try again...\n"); + return; + } month = in.nextLine(); + System.out.println("\nWhat is the day of the week? \n"); + if (! in.hasNextLine()) { + System.err.println("Error!.\n"); + return; + } day = in.nextLine(); + System.out.println("\nWhat is the day of the month (1-31)? \n"); + if (! in.hasNextInt()) { + System.err.println("Error, looks like this is not a number.\n"); + return; + } date = in.nextInt(); + System.out.println("\nWhat is the year? \n"); + if (! in.hasNextInt()) { + System.err.println("Error, looks like this is not a number.\n"); + return; + } year = in.nextInt(); // call print method From f407e24644197ac7708d68e0c2514fa626fbfeca Mon Sep 17 00:00:00 2001 From: Jeremy Miller Date: Thu, 25 Apr 2019 09:41:40 -0700 Subject: [PATCH 9/9] update comments --- ch03/numberGuess.java | 1 - 1 file changed, 1 deletion(-) diff --git a/ch03/numberGuess.java b/ch03/numberGuess.java index e341c0f..9304e42 100644 --- a/ch03/numberGuess.java +++ b/ch03/numberGuess.java @@ -1,5 +1,4 @@ import java.util.Random; -import java.util.Scanner; /** * A simple number guessing game