From f9c08a3ed5ac15066c2fb46575d3c45123950242 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 17 Apr 2018 13:51:29 -0500 Subject: [PATCH 01/97] added comment --- ch02/Variables.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ch02/Variables.java b/ch02/Variables.java index a295abf..a8eab2b 100644 --- a/ch02/Variables.java +++ b/ch02/Variables.java @@ -1,10 +1,12 @@ /** * Examples from Chapter 2. */ -public class Variables { - - public static void main(String[] args) { +public class Variables +{ + public static void main(String[] args) + { + //Josiah String message; int x; @@ -59,7 +61,7 @@ public static void main(String[] args) { System.out.println(0.1 * 10); System.out.println(0.1 + 0.1 + 0.1 + 0.1 + 0.1 - + 0.1 + 0.1 + 0.1 + 0.1 + 0.1); + + 0.1 + 0.1 + 0.1 + 0.1 + 0.1); double balance = 123.45; // potential rounding error int balance2 = 12345; // total number of cents From 130b9e1fd824cd82ca8f5732c4e36debb5a12f13 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 17 Apr 2018 15:00:09 -0500 Subject: [PATCH 02/97] Variables --- ch02/Variables.java | 1 + 1 file changed, 1 insertion(+) diff --git a/ch02/Variables.java b/ch02/Variables.java index a8eab2b..c48ea0e 100644 --- a/ch02/Variables.java +++ b/ch02/Variables.java @@ -80,6 +80,7 @@ public static void main(String[] args) hour = minute + 1; // correct // minute + 1 = hour; // compiler error + } } From 80e870b13feb5f1811f7270ba46a3e7c9c38ddeb Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 17 Apr 2018 15:00:40 -0500 Subject: [PATCH 03/97] First Variable --- ch02/FirstVariable.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 ch02/FirstVariable.java diff --git a/ch02/FirstVariable.java b/ch02/FirstVariable.java new file mode 100644 index 0000000..d518bfa --- /dev/null +++ b/ch02/FirstVariable.java @@ -0,0 +1,10 @@ +public class FirstVariable +{ + public static void main(String[] args) + { + String message = "Initial value"; + System.out.println(message); + message="Modified value"; + System.out.println(message); + } +} From b81dd173a1b6e9258ff0907f5ac44fada501c9d1 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 17 Apr 2018 15:01:27 -0500 Subject: [PATCH 04/97] First Variable --- ch02/FirstVariable.java | 1 + 1 file changed, 1 insertion(+) diff --git a/ch02/FirstVariable.java b/ch02/FirstVariable.java index d518bfa..2c5d785 100644 --- a/ch02/FirstVariable.java +++ b/ch02/FirstVariable.java @@ -4,6 +4,7 @@ public static void main(String[] args) { String message = "Initial value"; System.out.println(message); + message="Modified value"; System.out.println(message); } From 4655716b78c94683dddbfafcd476fd65680b7935 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 17 Apr 2018 15:02:14 -0500 Subject: [PATCH 05/97] Data types --- ch02/DataTypes.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 ch02/DataTypes.java diff --git a/ch02/DataTypes.java b/ch02/DataTypes.java new file mode 100644 index 0000000..96dfafe --- /dev/null +++ b/ch02/DataTypes.java @@ -0,0 +1,17 @@ +public class DataTypes +{ + public static void main(String[] args) + { + char letter = 'm'; + String title = "Java in easy steps"; + int number = 365; + float decimal = 98.6f; + boolean result = true; + + System.out.println("Initial is "+letter); + System.out.println("Book is "+title); + System.out.println("Days are "+number); + System.out.println("Temperature is "+decimal); + System.out.println("ANswer is "+result); + } +} From fa5de6c7e692061a81b6d5b2e7295ba002115b60 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 17 Apr 2018 15:02:30 -0500 Subject: [PATCH 06/97] Constants --- ch02/Constants.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 ch02/Constants.java diff --git a/ch02/Constants.java b/ch02/Constants.java new file mode 100644 index 0000000..45d1259 --- /dev/null +++ b/ch02/Constants.java @@ -0,0 +1,22 @@ +public class Constants +{ + public static void main(String[] args) + { + //Constant score values + final int TOUCHDOWN = 6; + final int CONVERSION = 1; + final int FIELDGOAL = 3; + + int td, pat, fg, total; + + //Calculate points scored + td=4*TOUCHDOWN; + pat=3*CONVERSION; + fg=2*FIELDGOAL; + total=(td+pat+fg); + + //Output calculated total + System.out.println("Score: " + total); + + } +} From 326f1f77fdcd7691e0cf247325f0d7cdd838f26c Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 17 Apr 2018 15:02:51 -0500 Subject: [PATCH 07/97] Arithmetic --- ch02/Arithmetic.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 ch02/Arithmetic.java diff --git a/ch02/Arithmetic.java b/ch02/Arithmetic.java new file mode 100644 index 0000000..294aebe --- /dev/null +++ b/ch02/Arithmetic.java @@ -0,0 +1,19 @@ +public class Arithmetic +{ + public static void main(String[] args) + { + int num = 100; + int factor = 20; + int sum = 0; + + sum = num + factor; //100 + 20 + System.out.println("Addition sum: " + sum); + sum=num - factor; //100 - 20 + System.out.println("Subtraction sum: " + sum); + + sum = num * factor; //100 * 20 + System.out.println("Multiplication sum: " + sum); + sum = num / factor; //100 / 20 + System.out.println("Division sum: " + sum); + } +} From 003615c981ecdeb97c236610ad6272b762ac57aa Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 17 Apr 2018 15:03:26 -0500 Subject: [PATCH 08/97] Assignment --- ch02/Assignment.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 ch02/Assignment.java diff --git a/ch02/Assignment.java b/ch02/Assignment.java new file mode 100644 index 0000000..5cdd12c --- /dev/null +++ b/ch02/Assignment.java @@ -0,0 +1,22 @@ +public class Assignment +{ + public static void main(String[] args) + { + String txt = "Fantastic "; + String lang = "Java"; + txt += lang; //Assign concatenated String + System.out.println("Add & Assign Strings: " + txt); + + int sum = 10; + int num = 20; + sum += num; //Assign result(10+20 = 30) + System.out.println("Add & Assign Integers " + sum); + + int factor = 5; + sum *= factor; //Assign result(30 *= 5) + System.out.println("Multiplication sum: " + sum); + + sum /= factor; //Assign result (150 / 5 = 30) + System.out.println("Division sum: " + sum); + } +} From c7d3885cb4a72c01b0d5876e3feb8a29206981dd Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 17 Apr 2018 15:03:51 -0500 Subject: [PATCH 09/97] Date --- ch02/Date.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 ch02/Date.java diff --git a/ch02/Date.java b/ch02/Date.java new file mode 100644 index 0000000..6b23235 --- /dev/null +++ b/ch02/Date.java @@ -0,0 +1,15 @@ +public class Date +{ + public static void main(String[] args) + { + String day = "Tuesday"; + int date = 17; + String month = "April"; + int year = 2018; + + System.out.println("American format:"); + System.out.println(day + ", " + month + " " + date + ", " + year); + System.out.println("European format:"); + System.out.println(day + " " + date + " " + month + " " + year); + } +} From 97a08f26207bcf96f9aec5b78fb34286880791ea Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 17 Apr 2018 15:04:14 -0500 Subject: [PATCH 10/97] Time --- ch02/Time.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 ch02/Time.java diff --git a/ch02/Time.java b/ch02/Time.java new file mode 100644 index 0000000..0a7bec1 --- /dev/null +++ b/ch02/Time.java @@ -0,0 +1,25 @@ +public class Time +{ + public static void main(String[] args) + { + int hour = 14; + int minute = 50; + int second = 45; + + int secSinceMidnight; + int secRemaining; + + secSinceMidnight = (60*60*14) + (50*60) + 45; + System.out.println("Seconds since midnight: " + secSinceMidnight); + + secRemaining = (24*60*60) - secSinceMidnight; + System.out.println("Seconds until the day ends: " + secRemaining); + + hour = 14; + minute = 55; + second = 30; + + int timeTaken = ((55-5) * 60) + (15+30); + System.out.println("Seconds it took me to complete this exercise: " + timeTaken); + } +} From 6cc64fbfb31a11bca631879a0a884462b1e7aa03 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 17 Apr 2018 15:12:19 -0500 Subject: [PATCH 11/97] Had a typo - changed 5 to 50 at the bottom --- ch02/Time.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch02/Time.java b/ch02/Time.java index 0a7bec1..c3fcc90 100644 --- a/ch02/Time.java +++ b/ch02/Time.java @@ -19,7 +19,7 @@ public static void main(String[] args) minute = 55; second = 30; - int timeTaken = ((55-5) * 60) + (15+30); + int timeTaken = ((55-50) * 60) + (15+30); System.out.println("Seconds it took me to complete this exercise: " + timeTaken); } } From 7b036ee96964f5471f48eb3ff8018dfaa534d210 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 17 Apr 2018 15:35:47 -0500 Subject: [PATCH 12/97] First Variable --- ch02/IntExtremes.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 ch02/IntExtremes.java diff --git a/ch02/IntExtremes.java b/ch02/IntExtremes.java new file mode 100644 index 0000000..ca25417 --- /dev/null +++ b/ch02/IntExtremes.java @@ -0,0 +1,20 @@ +public class IntExtremes +{ + public static void main(String[] args) + { + int positiveInt = 2147483647; + System.out.println(positiveInt); + + positiveInt++; + + System.out.println(positiveInt); + + int negativeInt = -2147483648; + System.out.println(negativeInt); + + negativeInt--; + + System.out.println(negativeInt); + + } +} From 695ead2c32b3a46d067fc495d254f94bddc156a1 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 17 Apr 2018 15:36:37 -0500 Subject: [PATCH 13/97] First Variable --- ch02/IntExtremes.java | 1 - 1 file changed, 1 deletion(-) diff --git a/ch02/IntExtremes.java b/ch02/IntExtremes.java index ca25417..80463a5 100644 --- a/ch02/IntExtremes.java +++ b/ch02/IntExtremes.java @@ -15,6 +15,5 @@ public static void main(String[] args) negativeInt--; System.out.println(negativeInt); - } } From f705466183bc5b4c6c47283e4e0e90c2da8bdeb6 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 17 Apr 2018 15:39:48 -0500 Subject: [PATCH 14/97] First Variable --- ch02/IntByZero.java | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 ch02/IntByZero.java diff --git a/ch02/IntByZero.java b/ch02/IntByZero.java new file mode 100644 index 0000000..89b4806 --- /dev/null +++ b/ch02/IntByZero.java @@ -0,0 +1,11 @@ +public class IntByZero +{ + public static void main(String[] args) + { + int first = 42; + int second = 0; + int result = 42/0; + + System.out.println(result); + } +} From b058b74b434fd9c6a418c60ac3c42b11340751d0 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 17 Apr 2018 15:41:35 -0500 Subject: [PATCH 15/97] First Variable --- ch02/IntByZero.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch02/IntByZero.java b/ch02/IntByZero.java index 89b4806..4c85b36 100644 --- a/ch02/IntByZero.java +++ b/ch02/IntByZero.java @@ -4,7 +4,7 @@ public static void main(String[] args) { int first = 42; int second = 0; - int result = 42/0; + int result = first/second; System.out.println(result); } From 4b92fc1578a773cc431832695b92c786151a83ca Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 17 Apr 2018 15:42:44 -0500 Subject: [PATCH 16/97] doublebyzero --- ch02/DoubleByZero.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 ch02/DoubleByZero.java diff --git a/ch02/DoubleByZero.java b/ch02/DoubleByZero.java new file mode 100644 index 0000000..e2da165 --- /dev/null +++ b/ch02/DoubleByZero.java @@ -0,0 +1,12 @@ +public class DoubleByZero +{ + public static void main(String[] args) + { + double first = 42.0; + double second = 0.0; + double result = first/second; + + System.out.println(result); + + } +} From 0bd9650ecdf4fc467ee4e3e9fb9a9c0b4830f16f Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 17 Apr 2018 15:46:50 -0500 Subject: [PATCH 17/97] I Love Java --- ch02/LoveJava.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 ch02/LoveJava.java diff --git a/ch02/LoveJava.java b/ch02/LoveJava.java new file mode 100644 index 0000000..cbb6753 --- /dev/null +++ b/ch02/LoveJava.java @@ -0,0 +1,12 @@ +public class LoveJava +{ + public static void main(String[] args) + { + System.out.println("I love Java!"); + String one = "I "; + String two = "love "; + String three = "Java!"; + + System.out.println(one + two + three ); + } +} From fe2351f9f34714d75a3947c60f696e2ed2393275 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 17 Apr 2018 15:55:04 -0500 Subject: [PATCH 18/97] Withdrawal --- ch02/Withdrawal.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 ch02/Withdrawal.java diff --git a/ch02/Withdrawal.java b/ch02/Withdrawal.java new file mode 100644 index 0000000..30916d6 --- /dev/null +++ b/ch02/Withdrawal.java @@ -0,0 +1,26 @@ +public class Withdrawal +{ + public static void main(String[] args) + { + int withdrawal = 137; + int twenty; + int ten; + int five; + int one; + + twenty = withdrawal / 20; + withdrawal -= twenty * 20; + + ten = withdrawal / 10; + withdrawal -= ten * 10; + + five = withdrawal / 5; + withdrawal -= five * 5; + + one = withdrawal / 1; + withdrawal -= one * 1; + + System.out.printf(("$20 (%d), $10 (%d), $5 (%d), $1 (%d)"), twenty, ten, five, one); + } + +} From 44371b799dae19ae999384b6ab2139644b6ff6b7 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 17 Apr 2018 15:59:25 -0500 Subject: [PATCH 19/97] doublebyzero --- ch02/Withdrawal.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/ch02/Withdrawal.java b/ch02/Withdrawal.java index 30916d6..1d616a7 100644 --- a/ch02/Withdrawal.java +++ b/ch02/Withdrawal.java @@ -17,10 +17,7 @@ public static void main(String[] args) five = withdrawal / 5; withdrawal -= five * 5; - one = withdrawal / 1; - withdrawal -= one * 1; - - System.out.printf(("$20 (%d), $10 (%d), $5 (%d), $1 (%d)"), twenty, ten, five, one); + System.out.printf(("$20 (%d), $10 (%d), $5 (%d), $1 (%d)"), twenty, ten, five, withdrawal); } } From 13cdb48f451ccb2c921b3f4d58327dad8ce80b9d Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Wed, 18 Apr 2018 12:24:23 -0500 Subject: [PATCH 20/97] celcius to farhenheit --- ch03/CelsToFar.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 ch03/CelsToFar.java diff --git a/ch03/CelsToFar.java b/ch03/CelsToFar.java new file mode 100644 index 0000000..b16c125 --- /dev/null +++ b/ch03/CelsToFar.java @@ -0,0 +1,19 @@ +import java.util.Scanner; +public class CelsToFar + +{ + public static void main(String[] args) + { + double cels; + double far; + + Scanner input = new Scanner(System.in); + + System.out.print("Enter a value in degrees Celcius: "); + cels = input.nextDouble(); + + far = cels * (9/5) + 32; + + System.out.printf("%.1f C = %.1f F", cels, far); + } +} From fba507b269257f50e618677168c43b9696ed817e Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Wed, 18 Apr 2018 13:04:02 -0500 Subject: [PATCH 21/97] celcius to farhenheit --- ch03/Escape.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 ch03/Escape.java diff --git a/ch03/Escape.java b/ch03/Escape.java new file mode 100644 index 0000000..e0fb5cd --- /dev/null +++ b/ch03/Escape.java @@ -0,0 +1,15 @@ +public class Escape +{ + public static void main(String[] args) + { + String header = "\n\t NEW YORK 3-DAY FORECAST:\n"; + header+="\n\tDay\t\tHigh\tLow\tConditions\n"; + header+="\t---\t\t---\t---\t----------\n"; + + String forecast = "\tSunday\t\t68F\t48F\tSunny\n"; + forecast+= "\tMonday\t\t69F\t57F\tSunny\n"; + forecast+="\tTuesday\t\t71F\t50F\tCloudy"; + + System.out.println((header+forecast)); + } +} From 054f2d44a9c13b7eca518194c6bc53eaa6fdf320 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Wed, 18 Apr 2018 15:57:14 -0500 Subject: [PATCH 22/97] SimpleMethods --- ch04/SimpleMethods.java | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 ch04/SimpleMethods.java diff --git a/ch04/SimpleMethods.java b/ch04/SimpleMethods.java new file mode 100644 index 0000000..054c611 --- /dev/null +++ b/ch04/SimpleMethods.java @@ -0,0 +1,31 @@ +public class SimpleMethods +{ + public static void main(String[] args) + { + printCount(5); + + printSum(4,6); + printSum(7,2); + + printBoolean(true); + printBoolean(false); + + } + + public static void printCount(int count) + { + System.out.println("The count is: " + count); + } + + public static void printSum(int x, int y) + { + System.out.println(x + " + " + y + " = " + (x+y)); + } + + public static void printBoolean(boolean isStudent) + { + System.out.println("I am a student: " + isStudent); + } + +} + From 93a3785e1af8179e0e55ef9f5e87b354f64e095b Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Wed, 18 Apr 2018 16:06:51 -0500 Subject: [PATCH 23/97] DemoMath --- ch04/DemoMath.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 ch04/DemoMath.java diff --git a/ch04/DemoMath.java b/ch04/DemoMath.java new file mode 100644 index 0000000..59b7db5 --- /dev/null +++ b/ch04/DemoMath.java @@ -0,0 +1,21 @@ +public class DemoMath +{ + public static void main(String[] args) + { + int a = -4; + int b = 8; + double c = 3.5; + + System.out.println("Value of int a: " + a); + System.out.println("Absolute value of int a: " + Math.abs(a)); + + System.out.println("Value of int a: " + a); + System.out.println("Value of int b: " + b); + System.out.println("Max value of int a and int b: " + Math.max(a,b)); + + System.out.println("Value of double c: " + c); + System.out.println("Double c to the power of c: " + Math.pow(c,c)); + + System.out.println("Value of pi: " + Math.PI); + } +} From 6e65eafc45ce59fbdd518bf34255fe647d79b7e3 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Wed, 18 Apr 2018 16:12:22 -0500 Subject: [PATCH 24/97] MathUtil --- ch04/MathUtil.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 ch04/MathUtil.java diff --git a/ch04/MathUtil.java b/ch04/MathUtil.java new file mode 100644 index 0000000..b5676fd --- /dev/null +++ b/ch04/MathUtil.java @@ -0,0 +1,21 @@ +public class MathUtil +{ + public static void main(String[] args) + { + printDifference(1000,4000000); + + } + + public static void printDifference(int x, int y) + { + int difference = x - y; + System.out.println("The difference of " + x + " and " + y + " is " + difference); + printAbsValue(difference); + } + + public static void printAbsValue(int x) + { + System.out.println("Value is: " + x + " and abs value is: " + Math.abs(x)); + } + +} From 18fcfbbd5b12de9fc430b71ea7251d8e66c681f3 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Wed, 18 Apr 2018 16:19:55 -0500 Subject: [PATCH 25/97] BigMethodSignature --- ch04/BigMethodSignature.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 ch04/BigMethodSignature.java diff --git a/ch04/BigMethodSignature.java b/ch04/BigMethodSignature.java new file mode 100644 index 0000000..82a630b --- /dev/null +++ b/ch04/BigMethodSignature.java @@ -0,0 +1,16 @@ +public class BigMethodSignature +{ + public static void main(String[] args) + { + printSum(1,2,3,4,5,6,7,8,9,10); + printSum(67,65,45,4,34,3,2,34,67,8); + } + + public static void printSum(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j) + { + int sum = a + b + c + d + e + f + g + h + i + j; + + System.out.print("The sum of " + a + ", " + b + ", " + c + ", " + d + ", " + e + ", " + f + ", "); + System.out.println(g + ", " + h + ", " + i + ", and " + j + ", " + " is " + sum); + } +} From 56293b2e4ed2852bf3c1a83c154cf8a6e1a341eb Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Wed, 18 Apr 2018 17:14:34 -0500 Subject: [PATCH 26/97] TimeConversion --- ch03/TimeConversion.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 ch03/TimeConversion.java diff --git a/ch03/TimeConversion.java b/ch03/TimeConversion.java new file mode 100644 index 0000000..764be40 --- /dev/null +++ b/ch03/TimeConversion.java @@ -0,0 +1,29 @@ +import java.util.Scanner; +public class TimeConversion +{ + public static void main(String[] args) + { + int secInput; + int hr; + int min; + int secConverted; + final int SEC_PER_MIN = 60; + final int SEC_PER_HR = SEC_PER_MIN * 60; + + Scanner input = new Scanner(System.in); + + System.out.print("Enter a number of seconds:"); + secInput = input.nextInt(); + + secConverted = secInput; + + hr = secInput/SEC_PER_HR; + secConverted = secInput%SEC_PER_HR; + + min = secConverted/SEC_PER_MIN; + secConverted = secConverted%SEC_PER_MIN; + + System.out.printf("%d seconds = %d hours, %d minutes, and %d seconds", secInput, hr, min, secConverted); + + } +} From 1320fac9e73277856edfe51985894c90ea848096 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Wed, 18 Apr 2018 17:22:02 -0500 Subject: [PATCH 27/97] Employee --- ch04/Employee.java | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 ch04/Employee.java diff --git a/ch04/Employee.java b/ch04/Employee.java new file mode 100644 index 0000000..712e7fa --- /dev/null +++ b/ch04/Employee.java @@ -0,0 +1,68 @@ +import java.util.Scanner; +import java.util.Random; +public class Employee +{ + public static void main(String[] args) + { + int birthYear = 1999; + boolean isUnionMember = true; + String fName = "Josiah"; + String mName = "David"; + String lName = "Douglas"; + int employeeNumber; + Scanner scanner = new Scanner(System.in); + + printHeader(); + System.out.println("Please enter your 5 digit employee number:"); + employeeNumber = scanner.nextInt(); + + printFullName(fName,mName,lName); + + printUnionStatus(isUnionMember); + + printAge(birthYear); + + printEvenOrOdd(employeeNumber); + + printGenerateSecretPassword(employeeNumber); + } + + public static void printHeader() + { + System.out.println("Welcome to the WallabyTech Employee Application"); + System.out.println("==============================================="); + } + + public static void printFullName(String first, String middle, String last) + { + System.out.println(last + ", " + first + " " + middle); + } + + public static void printUnionStatus(boolean unionMember) + { + System.out.println("Your union status is: " + unionMember ); + } + + public static void printAge(int age) + { + age = 2018 - age; + System.out.println("Your age is: " + age); + } + + public static void printEvenOrOdd(int evenOdd) + { + evenOdd %=2; + System.out.println("Employee number is : " + evenOdd); + } + + public static void printGenerateSecretPassword(int eNum) + { + Random random = new Random(); + + int ranNum = random.nextInt(10) + 1; + int password = (eNum + ranNum) * 5; + + System.out.println("Employee's random secret pw is: " + password); + } + +} From 28b7aa58e71472a2f69b107f5dc342fed54d6b17 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Wed, 18 Apr 2018 18:11:54 -0500 Subject: [PATCH 28/97] PrintDate --- ch04/PrintAmerican.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 ch04/PrintAmerican.java diff --git a/ch04/PrintAmerican.java b/ch04/PrintAmerican.java new file mode 100644 index 0000000..d82f7dd --- /dev/null +++ b/ch04/PrintAmerican.java @@ -0,0 +1,24 @@ +public class PrintAmerican +{ + public static void main(String[] args) + { + String day = "Wednesday"; + int date = 18; + String month = "April"; + int year = 2018; + + printAmerican(day, date, month, year); + System.out.println(); + printEuropean(day, date, month, year); + } + + public static void printAmerican(String day, int date, String month, int year) + { + System.out.println("American date: " + day + ", " + month + " " + date + ", " + year); + } + + public static void printEuropean(String day, int date, String month, int year) + { + System.out.println("European date: " + day + " " + date + " " + month + " " + year); + } +} From 5b77bcefcd834242d2d21ed34cb507d35c38efd9 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Thu, 19 Apr 2018 11:08:51 -0500 Subject: [PATCH 29/97] Comparison --- ch05/Comparison.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 ch05/Comparison.java diff --git a/ch05/Comparison.java b/ch05/Comparison.java new file mode 100644 index 0000000..e2891a0 --- /dev/null +++ b/ch05/Comparison.java @@ -0,0 +1,24 @@ +public class Comparison +{ + public static void main(String[] args) + { + String txt = "Fantastic "; + String lang = "Java"; + boolean state = (txt==lang); + + System.out.println("String Equality Test: " + state); + + state = (txt!=lang); + System.out.println("String Inequality Test: " + state); + + int dozen = 12; + int score = 20; + state = (dozen>score); + + System.out.println("Greater Than Test: " + state); + + state = (dozen < score); + + System.out.println("Less Than Test: " + state); + } +} From 5b714a96a5ff8381a58518ee3531bb6714db127c Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Thu, 19 Apr 2018 11:09:09 -0500 Subject: [PATCH 30/97] Logic --- ch05/Logic.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 ch05/Logic.java diff --git a/ch05/Logic.java b/ch05/Logic.java new file mode 100644 index 0000000..423bfa6 --- /dev/null +++ b/ch05/Logic.java @@ -0,0 +1,16 @@ +public class Logic +{ + public static void main(String[] args) + { + boolean yes = true; + boolean no = false; + + System.out.println("Both YesYes True: " + (yes && yes)); + System.out.println("Both YesNo True: " + (yes && no)); + System.out.println("Either YesYes True: " + (yes || yes)); + System.out.println("Either YesNo True: " + (yes || no)); + System.out.println("Eitehr NoNo True: " + (no || no)); + System.out.println("Original Yes Value: " + yes); + System.out.println("Original No Value" + no); + } +} From e3ccf9598215804f75f0e4adb6adfe42d86bc715 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Thu, 19 Apr 2018 11:15:26 -0500 Subject: [PATCH 31/97] Condition --- ch05/Condition.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 ch05/Condition.java diff --git a/ch05/Condition.java b/ch05/Condition.java new file mode 100644 index 0000000..b9cbeaf --- /dev/null +++ b/ch05/Condition.java @@ -0,0 +1,14 @@ +public class Condition +{ + public static void main(String[] args) + { + int num1 = 1357; + int num2 = 2468; + + String result = (num1 % 2 != 0)? "Odd" : "Even"; + System.out.println(num1 + " is " + result); + + result = (num2 % 2 != 0)? "Odd" : "Even"; + System.out.println(num2 + " is " + result); + } +} From b970844b909904164f446cdb21b9e3d3ac2397f1 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Thu, 19 Apr 2018 11:26:27 -0500 Subject: [PATCH 32/97] Else --- ch05/Else.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 ch05/Else.java diff --git a/ch05/Else.java b/ch05/Else.java new file mode 100644 index 0000000..648a130 --- /dev/null +++ b/ch05/Else.java @@ -0,0 +1,18 @@ +public class Else +{ + public static void main(String[] args) + { + int hrs = 15; + + if(hrs<13) + { + System.out.println("Good morning: " + hrs); + } + + else + { + System.out.println("Good evening: " + hrs); + } + + } +} From 17691ec1e6e0bc39d56367e14b0b5bc12eb7a7b2 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Thu, 19 Apr 2018 11:26:54 -0500 Subject: [PATCH 33/97] Precedence --- ch05/Precedence.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 ch05/Precedence.java diff --git a/ch05/Precedence.java b/ch05/Precedence.java new file mode 100644 index 0000000..a7d3068 --- /dev/null +++ b/ch05/Precedence.java @@ -0,0 +1,17 @@ +public class Precedence +{ + public static void main(String[] args) + { + int sum = 32 - 8 + 16 * 2; + + System.out.println("Default order: " + sum); + + sum = (32 - 8 + 16) * 2; + + System.out.println("Specified order: " + sum); + + sum = (32 - (8 + 16)) * 2; + + System.out.println("Nested specified order: " + sum); + } +} From eff3a6a59defe8a37016fa7a1e5726010f66e22d Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Thu, 19 Apr 2018 11:27:10 -0500 Subject: [PATCH 34/97] If --- ch05/If.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 ch05/If.java diff --git a/ch05/If.java b/ch05/If.java new file mode 100644 index 0000000..1d8309e --- /dev/null +++ b/ch05/If.java @@ -0,0 +1,25 @@ +public class If +{ + public static void main(String[] args) + { + if(5<1) + { + System.out.println("Five is greater than one"); + } + + if(2<4) + { + System.out.println("Two is less than four"); + System.out.println("Test succeeded"); + } + + int num = 10; + + if(((num>5)&&(num<10))||(num==12)) + { + System.out.println("Number is 6-9 inclusive, or 12"); + } + + + } +} From d912b117944b9a8ff8e6805d519d6c63277edb88 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Thu, 19 Apr 2018 11:32:53 -0500 Subject: [PATCH 35/97] Switch --- ch05/Switch.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 ch05/Switch.java diff --git a/ch05/Switch.java b/ch05/Switch.java new file mode 100644 index 0000000..0a55553 --- /dev/null +++ b/ch05/Switch.java @@ -0,0 +1,17 @@ +public class Switch +{ + public static void main(String[] args) + { + int month = 2; + int year = 2018; + int num = 31; + + switch(month) + { + case 4: case 6: case 9: case 11: num = 30; break; + case 2: num = (year%4 == 0) ? 29 : 28; break; + } + + System.out.println(month + "/" + year + ": " + num + " days"); + } +} From 582ba8b50f7facb392d49f1de4875e9e5e9d779c Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Thu, 19 Apr 2018 12:41:08 -0500 Subject: [PATCH 36/97] SwitchExample --- ch05/SwitchExample.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 ch05/SwitchExample.java diff --git a/ch05/SwitchExample.java b/ch05/SwitchExample.java new file mode 100644 index 0000000..d8fa3fd --- /dev/null +++ b/ch05/SwitchExample.java @@ -0,0 +1,17 @@ +public class SwitchExample +{ + public static void main(String[] args) + { + lastNameWinner("lazenby"); + } + + private static void lastNameWinner(String lName) + { + switch(lName) + { + case "smith": case "Jones": System.out.println("Congratulations grand winner"); break; + case "lazenby": System.out.println("Hey, he owes me dinner"); break; + default: System.out.println("Sorry, not a winner"); + } + } +} From 8b2a0af8da6f7c55a55fc78d8c10c9a1d2121c16 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Thu, 19 Apr 2018 12:41:28 -0500 Subject: [PATCH 37/97] LogicMethods --- ch05/LogicMethods.java | 79 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 ch05/LogicMethods.java diff --git a/ch05/LogicMethods.java b/ch05/LogicMethods.java new file mode 100644 index 0000000..0ff7c09 --- /dev/null +++ b/ch05/LogicMethods.java @@ -0,0 +1,79 @@ +public class LogicMethods +{ + public static void main(String[] args) + { + int x = 9; + int y = 9; + + printIsLarge(x); + printIsLargeOrSmall(x); + printLargest(x,y); + printLargestOdd(x,y); + } + + private static void printIsLarge(int number) + { + if(number>99) + { + System.out.println("The number is large"); + } + } + + private static void printIsLargeOrSmall(int number) + { + if(number>99) + { + System.out.println("The number is large"); + } + + else if(number<10) + { + System.out.println("The number is small"); + } + } + + private static void printLargest(int number1, int number2) + { + if(number1>number2) + { + System.out.println("The largest number is: " + number1); + } + + else if(number2>number1) + { + System.out.println("The largest number is: " + number2); + } + + else if(number1 == number2) + { + System.out.println("The numbers are equal"); + } + } + + private static void printLargestOdd(int number1, int number2) + { + if(number1 % 2 == 1 || number2 % 2 == 1) + { + if(number1>number2) + { + System.out.println("The largest number odd number is: " + number1); + } + + else if(number2>number1) + { + System.out.println("The largest odd number is: " + number2); + } + } + + else if(number1 % 2 == 0 && number2 % 2 == 0) + { + System.out.println("Neither number is odd"); + } + + if(number1 % 2 == 1 && number2 % 2 == 1 && number1 == number2) + { + int sum = number1 + number2; + System.out.println("Two odds make an even: " + sum); + } + } +} From 59a184534c910976903874eeb4ff3a002d6c6d06 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Thu, 19 Apr 2018 12:47:29 -0500 Subject: [PATCH 38/97] SwitchExample --- ch05/SwitchExample.java | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/ch05/SwitchExample.java b/ch05/SwitchExample.java index d8fa3fd..e54e7f8 100644 --- a/ch05/SwitchExample.java +++ b/ch05/SwitchExample.java @@ -3,6 +3,7 @@ public class SwitchExample public static void main(String[] args) { lastNameWinner("lazenby"); + dayOfWeek(-2); } private static void lastNameWinner(String lName) @@ -11,7 +12,30 @@ private static void lastNameWinner(String lName) { case "smith": case "Jones": System.out.println("Congratulations grand winner"); break; case "lazenby": System.out.println("Hey, he owes me dinner"); break; - default: System.out.println("Sorry, not a winner"); + default: System.out.println("Sorry, not a winner"); break; + } + } + + private static void dayOfWeek(int day) + { + switch (day) + { + case 1: + System.out.println("Sunday"); break; + case 2: + System.out.println("Monday"); break; + case 3: + System.out.println("Tuesday"); break; + case 4: + System.out.println("Wednesday"); break; + case 5: + System.out.println("Thursday"); break; + case 6: + System.out.println("Friday"); break; + case 7: + System.out.println("Saturday"); break; + default: + System.out.println("Invalid value: " + day); } } } From 3f2fc6483e695a7b6e43ee53745af6c42df3a67b Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Thu, 19 Apr 2018 13:36:29 -0500 Subject: [PATCH 39/97] CrazyEd --- ch05/CrazyEd.java | 81 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 ch05/CrazyEd.java diff --git a/ch05/CrazyEd.java b/ch05/CrazyEd.java new file mode 100644 index 0000000..0f29800 --- /dev/null +++ b/ch05/CrazyEd.java @@ -0,0 +1,81 @@ +import java.util.Scanner; + +public class CrazyEd +{ + public static void main(String[] args) + { + final int oneInchCost = 2; + final int twoInchCost = 4; + final int threeInchCost = 6; + final int oneInchShip = 2; + final int twoInchShip = 2; + final int threeInchShip = 4; + final int handlingCharge = 5; + int initialCost = 0; + int totalShip = 0; + int totalCost = 0; + + Scanner input = new Scanner(System.in); + + System.out.print("Welcome to Crazy Ed's Sting Cheese. Would you like your string cheese to have a diameter of "); + System.out.println(" 1 inch, 2 inches, or 3 inches (enter 1, 2, or 3)?"); + + int cheeseSize = input.nextInt(); + + if(cheeseSize > 3 || cheeseSize < 1 ) + { + System.out.println("This order is too crazy"); + } + + else + { + System.out.print("OK, so you want the diameter of your string cheese to be " + cheeseSize + "."); + System.out.println(" How many yards would you like to order?"); + + int cheeseLength = input.nextInt(); + + System.out.println("So, you want " + cheeseLength + " yards of " + cheeseSize + " inch diameter string cheese."); + + if(cheeseSize == 1) + { + + initialCost = (oneInchCost * cheeseLength); + totalShip = (oneInchShip * cheeseLength); + } + + else if(cheeseSize == 2) + { + + initialCost = (twoInchCost * cheeseLength); + totalShip = (twoInchShip * cheeseLength); + } + + else if(cheeseSize == 3) + { + + initialCost = (threeInchCost * cheeseLength); + totalShip = (threeInchShip * cheeseLength); + } + + if(cheeseSize == 1 && cheeseLength > 50) + { + totalShip = 0; + } + + if(cheeseSize == 2 && cheeseLength > 75) + { + totalShip = 0; + } + + if(cheeseSize == 3 && cheeseLength > 25) + { + totalShip = 0; + } + + totalCost = initialCost + totalShip + handlingCharge; + + System.out.println("Your total, including shipping and handling, is: $" + totalCost); + + } + } +} From 3de5b58aff5c8ddfcceff37981d4f918c7557eb9 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Thu, 19 Apr 2018 13:40:15 -0500 Subject: [PATCH 40/97] Made all charges visible --- ch05/CrazyEd.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ch05/CrazyEd.java b/ch05/CrazyEd.java index 0f29800..2ccf4bd 100644 --- a/ch05/CrazyEd.java +++ b/ch05/CrazyEd.java @@ -74,7 +74,10 @@ else if(cheeseSize == 3) totalCost = initialCost + totalShip + handlingCharge; - System.out.println("Your total, including shipping and handling, is: $" + totalCost); + System.out.println("Cost of cheese: $" + initialCost); + System.out.println("Cost of shipping: $" + totalShip); + System.out.println("Handling charge: $" + handlingCharge); + System.out.println("Total cost: $" + totalCost); } } From 3a545c985288806460c5f38e9229eaad3fe65816 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Thu, 19 Apr 2018 13:51:36 -0500 Subject: [PATCH 41/97] Added recursion --- ch05/CrazyEd.java | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/ch05/CrazyEd.java b/ch05/CrazyEd.java index 2ccf4bd..7a6ecd9 100644 --- a/ch05/CrazyEd.java +++ b/ch05/CrazyEd.java @@ -4,31 +4,36 @@ public class CrazyEd { public static void main(String[] args) { - final int oneInchCost = 2; - final int twoInchCost = 4; - final int threeInchCost = 6; - final int oneInchShip = 2; - final int twoInchShip = 2; - final int threeInchShip = 4; - final int handlingCharge = 5; - int initialCost = 0; - int totalShip = 0; - int totalCost = 0; + calculateCost(); - Scanner input = new Scanner(System.in); + } + private static void calculateCost() - System.out.print("Welcome to Crazy Ed's Sting Cheese. Would you like your string cheese to have a diameter of "); - System.out.println(" 1 inch, 2 inches, or 3 inches (enter 1, 2, or 3)?"); + { + final int oneInchCost = 2; + final int twoInchCost = 4; + final int threeInchCost = 6; + final int oneInchShip = 2; + final int twoInchShip = 2; + final int threeInchShip = 4; + final int handlingCharge = 5; + int initialCost = 0; + int totalShip = 0; + int totalCost = 0; - int cheeseSize = input.nextInt(); + Scanner input = new Scanner(System.in); - if(cheeseSize > 3 || cheeseSize < 1 ) - { - System.out.println("This order is too crazy"); - } + System.out.print("Welcome to Crazy Ed's Sting Cheese. Would you like your string cheese to have a diameter of "); + System.out.println(" 1 inch, 2 inches, or 3 inches (enter 1, 2, or 3)?"); + + int cheeseSize = input.nextInt(); + + if (cheeseSize > 3 || cheeseSize < 1) + { + System.out.println("This order is too crazy. Try again."); + calculateCost(); + } - else - { System.out.print("OK, so you want the diameter of your string cheese to be " + cheeseSize + "."); System.out.println(" How many yards would you like to order?"); @@ -78,7 +83,5 @@ else if(cheeseSize == 3) System.out.println("Cost of shipping: $" + totalShip); System.out.println("Handling charge: $" + handlingCharge); System.out.println("Total cost: $" + totalCost); - } - } } From a75b48c383f1ef0e4fb2780132178f6bc9b6c21e Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Thu, 19 Apr 2018 14:06:21 -0500 Subject: [PATCH 42/97] Tried to fix the scanner bug and apparently failed --- ch05/CrazyEd.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/ch05/CrazyEd.java b/ch05/CrazyEd.java index 7a6ecd9..add1a17 100644 --- a/ch05/CrazyEd.java +++ b/ch05/CrazyEd.java @@ -4,8 +4,10 @@ public class CrazyEd { public static void main(String[] args) { - calculateCost(); + System.out.print("Welcome to Crazy Ed's Sting Cheese. Would you like your string cheese to have a diameter of "); + System.out.println(" 1 inch, 2 inches, or 3 inches (enter 1, 2, or 3)?"); + calculateCost(); } private static void calculateCost() @@ -23,14 +25,12 @@ private static void calculateCost() Scanner input = new Scanner(System.in); - System.out.print("Welcome to Crazy Ed's Sting Cheese. Would you like your string cheese to have a diameter of "); - System.out.println(" 1 inch, 2 inches, or 3 inches (enter 1, 2, or 3)?"); - int cheeseSize = input.nextInt(); + input.nextLine(); if (cheeseSize > 3 || cheeseSize < 1) { - System.out.println("This order is too crazy. Try again."); + System.out.println("This order is too crazy. Try a value between 1 and 3."); calculateCost(); } @@ -38,6 +38,7 @@ private static void calculateCost() System.out.println(" How many yards would you like to order?"); int cheeseLength = input.nextInt(); + input.nextLine(); System.out.println("So, you want " + cheeseLength + " yards of " + cheeseSize + " inch diameter string cheese."); From 11dbcd7423c60212862f483bf503ee4b3f10fd76 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Thu, 19 Apr 2018 14:39:51 -0500 Subject: [PATCH 43/97] fixed the recursion problem --- ch05/CrazyEd.java | 81 ++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/ch05/CrazyEd.java b/ch05/CrazyEd.java index add1a17..0f0e6e2 100644 --- a/ch05/CrazyEd.java +++ b/ch05/CrazyEd.java @@ -4,9 +4,6 @@ public class CrazyEd { public static void main(String[] args) { - System.out.print("Welcome to Crazy Ed's Sting Cheese. Would you like your string cheese to have a diameter of "); - System.out.println(" 1 inch, 2 inches, or 3 inches (enter 1, 2, or 3)?"); - calculateCost(); } private static void calculateCost() @@ -25,6 +22,9 @@ private static void calculateCost() Scanner input = new Scanner(System.in); + System.out.print("Welcome to Crazy Ed's Sting Cheese. Would you like your string cheese to have a diameter of "); + System.out.println(" 1 inch, 2 inches, or 3 inches (enter 1, 2, or 3)?"); + int cheeseSize = input.nextInt(); input.nextLine(); @@ -34,55 +34,56 @@ private static void calculateCost() calculateCost(); } - System.out.print("OK, so you want the diameter of your string cheese to be " + cheeseSize + "."); - System.out.println(" How many yards would you like to order?"); + else - int cheeseLength = input.nextInt(); - input.nextLine(); + { + System.out.print("OK, so you want the diameter of your string cheese to be " + cheeseSize + "."); + System.out.println(" How many yards would you like to order?"); - System.out.println("So, you want " + cheeseLength + " yards of " + cheeseSize + " inch diameter string cheese."); + int cheeseLength = input.nextInt(); + input.nextLine(); - if(cheeseSize == 1) - { + System.out.println("So, you want " + cheeseLength + " yards of " + cheeseSize + " inch diameter string cheese."); - initialCost = (oneInchCost * cheeseLength); - totalShip = (oneInchShip * cheeseLength); - } + if (cheeseSize == 1) + { - else if(cheeseSize == 2) - { + initialCost = (oneInchCost * cheeseLength); + totalShip = (oneInchShip * cheeseLength); + } else if (cheeseSize == 2) + { - initialCost = (twoInchCost * cheeseLength); - totalShip = (twoInchShip * cheeseLength); - } + initialCost = (twoInchCost * cheeseLength); + totalShip = (twoInchShip * cheeseLength); + } else if (cheeseSize == 3) + { - else if(cheeseSize == 3) - { + initialCost = (threeInchCost * cheeseLength); + totalShip = (threeInchShip * cheeseLength); + } - initialCost = (threeInchCost * cheeseLength); - totalShip = (threeInchShip * cheeseLength); - } + if (cheeseSize == 1 && cheeseLength > 50) + { + totalShip = 0; + } - if(cheeseSize == 1 && cheeseLength > 50) - { - totalShip = 0; - } + if (cheeseSize == 2 && cheeseLength > 75) + { + totalShip = 0; + } - if(cheeseSize == 2 && cheeseLength > 75) - { - totalShip = 0; - } + if (cheeseSize == 3 && cheeseLength > 25) + { + totalShip = 0; + } - if(cheeseSize == 3 && cheeseLength > 25) - { - totalShip = 0; - } + totalCost = initialCost + totalShip + handlingCharge; - totalCost = initialCost + totalShip + handlingCharge; + System.out.println("Cost of cheese: $" + initialCost); + System.out.println("Cost of shipping: $" + totalShip); + System.out.println("Handling charge: $" + handlingCharge); + System.out.println("Total cost: $" + totalCost); - System.out.println("Cost of cheese: $" + initialCost); - System.out.println("Cost of shipping: $" + totalShip); - System.out.println("Handling charge: $" + handlingCharge); - System.out.println("Total cost: $" + totalCost); + } } } From 9c3cdfb3099fdd2ede74fd2a50b4fe5c0d552af5 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Thu, 19 Apr 2018 14:54:59 -0500 Subject: [PATCH 44/97] TicketNumber --- ch05/TicketNumber.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 ch05/TicketNumber.java diff --git a/ch05/TicketNumber.java b/ch05/TicketNumber.java new file mode 100644 index 0000000..3dfd9f8 --- /dev/null +++ b/ch05/TicketNumber.java @@ -0,0 +1,27 @@ +import java.util.Scanner; +public class TicketNumber +{ + public static void main(String[] args) + { + Scanner input = new Scanner(System.in); + + System.out.print("Enter your ticket number: "); + int ticketNumber = input.nextInt(); + + int lastDigit = ticketNumber % 10; + //System.out.println(lastDigit); + + int ticketPrefix = ticketNumber / 10; + //System.out.println(ticketPrefix); + + int ticketPrefixRemainder = ticketPrefix % 7; + //System.out.println(ticketPrefixRemainder); + + boolean result = lastDigit == ticketPrefixRemainder; + + if(result) + System.out.println("Good number"); + else + System.out.println("Bad number"); + } +} From a22f449deccd8a158dddd69b717035f245a9078f Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Thu, 19 Apr 2018 15:47:26 -0500 Subject: [PATCH 45/97] TicketNumber --- ch05/FreeCoffee.java | 58 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 ch05/FreeCoffee.java diff --git a/ch05/FreeCoffee.java b/ch05/FreeCoffee.java new file mode 100644 index 0000000..ed23084 --- /dev/null +++ b/ch05/FreeCoffee.java @@ -0,0 +1,58 @@ + +public class FreeCoffee +{ + public static void main(String[] args) + { + System.out.print("Bicycle rider commuting 20 miles: "); + calculateDiscount(true, false, 20); + System.out.println(); + + System.out.print("Bicycle rider commuting 30 miles: "); + calculateDiscount(true, false, 30); + System.out.println(); + + System.out.print("Bicycle rider commuting 52 miles: "); + calculateDiscount(true, false, 52); + System.out.println(); + + System.out.print("Bus rider commuting 35 miles: "); + calculateDiscount(false, true, 35); + System.out.println(); + + System.out.print("Bus rider commuting 50 miles: "); + calculateDiscount(false, true, 50); + } + + private static void calculateDiscount(boolean isBicycle, boolean isBus, int distance) + { + float discount = 0; + String coffee = ""; + + if(distance < 21) + { + coffee = " and you get free coffee!"; + } + + if(isBicycle && distance < 30) + { + discount += .1; + } + + if(isBus && distance >= 35) + { + discount += .2; + } + + if(isBicycle && distance < 50) + { + discount += .2; + } + + if(isBus && distance < 50) + { + discount += .3; + } + + System.out.println("Your discount is " + discount + "0%" + coffee); + } +} From 45c2b24add31c3a76fe18a1ac4e34d60755da917 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 23 Apr 2018 08:56:39 -0500 Subject: [PATCH 46/97] IsDivisible --- ch06/IsDivisible.java | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 ch06/IsDivisible.java diff --git a/ch06/IsDivisible.java b/ch06/IsDivisible.java new file mode 100644 index 0000000..3f83f8a --- /dev/null +++ b/ch06/IsDivisible.java @@ -0,0 +1,38 @@ +public class IsDivisible +{ + public static void main(String[] args) + { + int x = 34; + int y = 5; + + int a = 11; + int b = 1; + int c = 12; + + System.out.println(isDivisible(x,y)); + System.out.println(isTriangle(a, b, c)); + } + + private static boolean isDivisible(int n, int m) + { + boolean result = false; + + if(n % m == 0) + { + result = true; + } + return result; + } + + private static boolean isTriangle(int a, int b, int c) + { + boolean result = true; + + if(a > b + c || b > a + c || c > a + b) + { + result = false; + } + + return result; + } +} From 0d0bb2d34d5af173f45ff969e0e25e04df1928e9 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 23 Apr 2018 08:56:57 -0500 Subject: [PATCH 47/97] MathUtil --- ch06/MathUtil.java | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 ch06/MathUtil.java diff --git a/ch06/MathUtil.java b/ch06/MathUtil.java new file mode 100644 index 0000000..707227d --- /dev/null +++ b/ch06/MathUtil.java @@ -0,0 +1,34 @@ +import java.util.Scanner; +public class MathUtil +{ + public static void main(String[] args) + { + Scanner scanner = new Scanner(System.in); + + System.out.println("Enter two integers:"); + int x = scanner.nextInt(); + int y = scanner.nextInt(); + int result = absoluteSum(x,y); + + System.out.println("The sum of the absolute values of those integers is: " + result); + + System.out.println("Now enter three integers: "); + x = scanner.nextInt(); + y = scanner.nextInt(); + int z = scanner.nextInt(); + + result = absoluteSum(x,y,z); + + System.out.println("The sum of the absolute vales of those three integers is: " + result); + } + + private static int absoluteSum(int x, int y) + { + return Math.abs(x) + Math.abs(y); + } + + private static int absoluteSum(int x, int y, int z) + { + return Math.abs(x) + Math.abs(y) + Math.abs(z); + } +} From d08ae16d4449146371b386888ad3e40f2f064e8c Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 23 Apr 2018 08:57:13 -0500 Subject: [PATCH 48/97] MultAdd --- ch06/Multadd.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 ch06/Multadd.java diff --git a/ch06/Multadd.java b/ch06/Multadd.java new file mode 100644 index 0000000..bd517cc --- /dev/null +++ b/ch06/Multadd.java @@ -0,0 +1,17 @@ +public class Multadd +{ + public static void main(String[] args) + { + double a = 1.0; + double b = 2.0; + double c = 3.0; + + System.out.println(multadd(a,b,c)); + + } + + private static double multadd(double a, double b, double c) + { + return a * b + c; + } +} From 90ad9d105e347d286fb83c7139477d53a996d6de Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 23 Apr 2018 12:22:49 -0500 Subject: [PATCH 49/97] While --- ch07/While.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 ch07/While.java diff --git a/ch07/While.java b/ch07/While.java new file mode 100644 index 0000000..a772e7f --- /dev/null +++ b/ch07/While.java @@ -0,0 +1,13 @@ +public class While +{ + public static void main(String[] args) + { + int num = 100; + + while(num > 0) + { + System.out.println(num); + num -= 10; + } + } +} From 94fa997f3eb863049ffe0cc7e4ebc10f53bf5b65 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 23 Apr 2018 12:25:25 -0500 Subject: [PATCH 50/97] DoWhile --- ch07/DoWhile.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 ch07/DoWhile.java diff --git a/ch07/DoWhile.java b/ch07/DoWhile.java new file mode 100644 index 0000000..f06b502 --- /dev/null +++ b/ch07/DoWhile.java @@ -0,0 +1,13 @@ +public class DoWhile +{ + public static void main(String[] args) + { + int num = 100; + + do + { + System.out.println("DoWhile Countup: " + num); + num += 10; + }while(num < 10); + } +} From d912c69ab76903608d1f28f609a29a848e930b08 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 23 Apr 2018 12:30:55 -0500 Subject: [PATCH 51/97] DoWhile --- ch07/While.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch07/While.java b/ch07/While.java index a772e7f..7a2ac6f 100644 --- a/ch07/While.java +++ b/ch07/While.java @@ -6,7 +6,7 @@ public static void main(String[] args) while(num > 0) { - System.out.println(num); + System.out.println("While countdown: " + num); num -= 10; } } From 0d321ffcdcc6008b5b3b9e635ac0e38868ea3463 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 23 Apr 2018 12:31:06 -0500 Subject: [PATCH 52/97] DoWhile --- ch07/ForLoop.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 ch07/ForLoop.java diff --git a/ch07/ForLoop.java b/ch07/ForLoop.java new file mode 100644 index 0000000..15b7da8 --- /dev/null +++ b/ch07/ForLoop.java @@ -0,0 +1,17 @@ +public class ForLoop +{ + public static void main(String[] args) + { + int num = 0; + + for(int i = 0;i < 4;i++) + { + System.out.println("Outer loop i = " + i); + for(int j = 1; j < 4;j++) + { + System.out.print("\tInner Loop j = " + j); + System.out.println("\t\tTotal num = " + (++num)); + } + } + } +} From c18fa4e9a4cf95c1d30e89c195e69f91be601ae7 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 23 Apr 2018 12:31:36 -0500 Subject: [PATCH 53/97] ForLoop --- ch07/ForLoop.java | 1 - 1 file changed, 1 deletion(-) diff --git a/ch07/ForLoop.java b/ch07/ForLoop.java index 15b7da8..ae0082e 100644 --- a/ch07/ForLoop.java +++ b/ch07/ForLoop.java @@ -3,7 +3,6 @@ public class ForLoop public static void main(String[] args) { int num = 0; - for(int i = 0;i < 4;i++) { System.out.println("Outer loop i = " + i); From d29eb9c2b113d3fb90a80b1c3285259b3c6a1624 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 23 Apr 2018 12:43:02 -0500 Subject: [PATCH 54/97] 7-A --- ch07/Loops7A.java | 73 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 ch07/Loops7A.java diff --git a/ch07/Loops7A.java b/ch07/Loops7A.java new file mode 100644 index 0000000..16ccdb9 --- /dev/null +++ b/ch07/Loops7A.java @@ -0,0 +1,73 @@ +public class Loops7A +{ + public static void main(String[] args) + { + System.out.println("For Loop Up:"); + forLoopUp(); + System.out.println("For Loop Down:"); + forLoopDown(); + System.out.println("While Loop Up:"); + whileLoopUp(); + System.out.println("While Loop Down:"); + whileLoopDown(); + System.out.println("Do While UP:"); + doWhileLoopUp(); + System.out.println("Do While Down:"); + doWhileLoopDown(); + } + + private static void forLoopUp() + { + for(int i = 1;i < 11; i++) + System.out.println(i); + } + + private static void forLoopDown() + { + for(int i = 10; i > 0; i--) + { + System.out.println(i); + } + } + + private static void whileLoopUp() + { + int i = 1; + + while(i < 11) + { + System.out.println(i); + i++; + } + } + + private static void whileLoopDown() + { + int i = 10; + while(i > 0) + { + System.out.println(i); + i--; + } + } + + private static void doWhileLoopUp() + { + int i = 1; + do + { + System.out.println(i); + i++; + }while(i < 11); + } + + private static void doWhileLoopDown() + { + int i = 10; + do + { + System.out.println(i); + i--; + }while(i > 0); + } +} From 3424e48bbd2e6a92c5cfe24126f2c0adcd88deda Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 23 Apr 2018 12:46:45 -0500 Subject: [PATCH 55/97] 7B --- ch07/Loops7B.java | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 ch07/Loops7B.java diff --git a/ch07/Loops7B.java b/ch07/Loops7B.java new file mode 100644 index 0000000..071dac2 --- /dev/null +++ b/ch07/Loops7B.java @@ -0,0 +1,39 @@ +public class Loops7B +{ + public static void main(String[] args) + { + System.out.println("For loop:"); + forLoopUp(); + System.out.println("While loop:"); + whileLoopUp(); + System.out.println("Do while loop:"); + doWhileLoopUp(); + } + + private static void forLoopUp() + { + for(int i = 0;i < 101; i += 10) + System.out.println(i); + } + + private static void whileLoopUp() + { + int i = 0; + + while(i < 101) + { + System.out.println(i); + i +=10; + } + } + + private static void doWhileLoopUp() + { + int i = 0; + do + { + System.out.println(i); + i += 10; + }while(i < 101); + } +} From c6472972a00443b2b53cf191852b00fd949235bd Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 23 Apr 2018 12:50:35 -0500 Subject: [PATCH 56/97] Loops7C --- ch07/Loop7C.java | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 ch07/Loop7C.java diff --git a/ch07/Loop7C.java b/ch07/Loop7C.java new file mode 100644 index 0000000..f4b3b68 --- /dev/null +++ b/ch07/Loop7C.java @@ -0,0 +1,39 @@ +public class Loop7C +{ + public static void main(String[] args) + { + System.out.println("For loop:"); + forLoop(); + System.out.println("While loop:"); + whileLoop(); + System.out.println("Do while loop:"); + doWhileLoop(); + } + + private static void forLoop() + { + for(int i = 100;i > -101; i -= 8) + System.out.println(i); + } + + private static void whileLoop() + { + int i = 100; + + while(i > -101) + { + System.out.println(i); + i -= 8; + } + } + + private static void doWhileLoop() + { + int i = 100; + do + { + System.out.println(i); + i -= 8; + }while(i > -101); + } +} From f6167111adec2ea86306c211dade7ac229e4bc6d Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 23 Apr 2018 12:53:24 -0500 Subject: [PATCH 57/97] Loops7D --- ch07/Loops7D.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 ch07/Loops7D.java diff --git a/ch07/Loops7D.java b/ch07/Loops7D.java new file mode 100644 index 0000000..54819ff --- /dev/null +++ b/ch07/Loops7D.java @@ -0,0 +1,15 @@ +public class Loops7D +{ + public static void main(String[] args) + { + loop(178); + } + + private static void loop(int x) + { + for(int i = 1; i <= x; i++) + { + System.out.println(i); + } + } +} From 9243a21fc7fea0497c1dd175a9f0b07925cb5de9 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 23 Apr 2018 12:56:47 -0500 Subject: [PATCH 58/97] Loops7E --- ch07/Loops7E.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 ch07/Loops7E.java diff --git a/ch07/Loops7E.java b/ch07/Loops7E.java new file mode 100644 index 0000000..fc01b78 --- /dev/null +++ b/ch07/Loops7E.java @@ -0,0 +1,14 @@ +import java.util.Scanner; +public class Loops7E +{ + public static void main(String[] args) + { + Scanner in = new Scanner(System.in); + int input; + System.out.println("Enter a number: "); + do + { + input = in.nextInt(); + }while(input != 0); + } +} From 7f933f6f7073e972a61d70fbc1af1d5a71cc321e Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 23 Apr 2018 13:02:19 -0500 Subject: [PATCH 59/97] Loops 7F --- ch07/Loops7F.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 ch07/Loops7F.java diff --git a/ch07/Loops7F.java b/ch07/Loops7F.java new file mode 100644 index 0000000..e27d54e --- /dev/null +++ b/ch07/Loops7F.java @@ -0,0 +1,21 @@ +import java.util.Scanner; + +public class Loops7F +{ + public static void main(String[] args) + { + Scanner in = new Scanner(System.in); + int total = 0; + int input; + while(total < 1000) + { + System.out.print("Enter a number:"); + input = in.nextInt(); + total += input; + if(total < 1000) + System.out.println("The total so far is: " + total); + } + + System.out.println("The total is: " + total); + } +} From 6d6a0a6185946d15acc0861bbb8e6dfa0f7bed5e Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 23 Apr 2018 14:45:30 -0500 Subject: [PATCH 60/97] Loops 7G --- ch07/Loops7G.java | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 ch07/Loops7G.java diff --git a/ch07/Loops7G.java b/ch07/Loops7G.java new file mode 100644 index 0000000..5f6cb6b --- /dev/null +++ b/ch07/Loops7G.java @@ -0,0 +1,43 @@ +import java.util.Scanner; + +public class Loops7G +{ + public static void main(String[] args) + { + Scanner input = new Scanner(System.in); + + System.out.println("Enter a number:"); + int maxInput = input.nextInt(); + System.out.print(0 + " | "); + + for(int i = 1; i <= maxInput; i++) + { + System.out.printf("%5d", i); + } + System.out.println(); + System.out.print("- \t"); + + for(int i = 1; i <= maxInput; i++) + { + System.out.print("-----"); + } + + System.out.println(); + for(int i = 1; i <= maxInput; i++) + { + if(i<10) + System.out.print(i + " | "); + + else + System.out.print(i + " | "); + for(int j = 1; j <= maxInput ; j++) + { + System.out.printf("%5d", i * j); + } + System.out.println(); + } + + } + + +} From 0a5a0cc7f4e0723b2ae7c7fe6e09365833e0ad34 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 23 Apr 2018 14:49:19 -0500 Subject: [PATCH 61/97] 5 digit capability --- ch07/Loops7G.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ch07/Loops7G.java b/ch07/Loops7G.java index 5f6cb6b..f1f5de8 100644 --- a/ch07/Loops7G.java +++ b/ch07/Loops7G.java @@ -8,7 +8,7 @@ public static void main(String[] args) System.out.println("Enter a number:"); int maxInput = input.nextInt(); - System.out.print(0 + " | "); + System.out.print(0 + " | "); for(int i = 1; i <= maxInput; i++) { @@ -27,9 +27,10 @@ public static void main(String[] args) { if(i<10) System.out.print(i + " | "); - - else + else if(i>10&&i<100) System.out.print(i + " | "); + else + System.out.print(i + "| "); for(int j = 1; j <= maxInput ; j++) { System.out.printf("%5d", i * j); From 8a61816781784bc2a1aaa756894e31598e3d1afb Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 23 Apr 2018 14:50:27 -0500 Subject: [PATCH 62/97] Fixed formatting for 10 --- ch07/Loops7G.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch07/Loops7G.java b/ch07/Loops7G.java index f1f5de8..7816df5 100644 --- a/ch07/Loops7G.java +++ b/ch07/Loops7G.java @@ -27,7 +27,7 @@ public static void main(String[] args) { if(i<10) System.out.print(i + " | "); - else if(i>10&&i<100) + else if(i>=10&&i<100) System.out.print(i + " | "); else System.out.print(i + "| "); From 303869d5e01acf7ab0378b718da669008f788c6d Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 24 Apr 2018 09:48:01 -0500 Subject: [PATCH 63/97] JavaES Array --- ch08/Array.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 ch08/Array.java diff --git a/ch08/Array.java b/ch08/Array.java new file mode 100644 index 0000000..6da2d6f --- /dev/null +++ b/ch08/Array.java @@ -0,0 +1,17 @@ +public class Array +{ + public static void main(String[] args) + { + String[] str = {"Much", "More", "Java"}; + int[] num = new int[3]; + num[0] = 100; + num[1] = 200; + + str[1] = "Better"; + + System.out.println("String array length is: " + str.length); + System.out.println("Integer array length is: " + num.length); + System.out.println(num[0] + "," + num[1] + "," + num[2]); + System.out.println(str[0] + str[1] + str[2]); + } +} From 3092477c25f942bd66a85909a8f8a9057b9021e9 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 24 Apr 2018 10:15:48 -0500 Subject: [PATCH 64/97] How Many Pets --- ch08/HowManyPets.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 ch08/HowManyPets.java diff --git a/ch08/HowManyPets.java b/ch08/HowManyPets.java new file mode 100644 index 0000000..fe51452 --- /dev/null +++ b/ch08/HowManyPets.java @@ -0,0 +1,29 @@ +import java.util.Scanner; + +public class HowManyPets +{ + public static void main(String[] args) + { + Scanner scanner = new Scanner(System.in); + System.out.println("How many pets do you have?"); + + int numPets = scanner.nextInt(); + scanner.nextLine(); + String[] pets = new String[numPets]; + String petName; + + for(int i=0;i Date: Tue, 24 Apr 2018 10:16:11 -0500 Subject: [PATCH 65/97] Array Demo --- ch08/ArrayDemo.java | 82 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 ch08/ArrayDemo.java diff --git a/ch08/ArrayDemo.java b/ch08/ArrayDemo.java new file mode 100644 index 0000000..2555245 --- /dev/null +++ b/ch08/ArrayDemo.java @@ -0,0 +1,82 @@ +public class ArrayDemo +{ + public static void main(String[] args) + { + int[] array = {1,5,9}; + double[] doubleArray = {10,15.0,20}; + String[] stringArray = new String[10]; + stringArray[0] = "Hi"; + stringArray[3] = "Hello"; + stringArray[9] = "Bye"; + printArray(array); + printTotal(array); + System.out.println("Array max is: " + arrayMax(array)); + System.out.println("Max index is: " + arrayMaxIndex(array)); + System.out.println("The average is: " + arrayAverage(doubleArray)); + printArray(stringArray); + } + + private static void printArray(int[] array) + { + for(int i=0;imax) + max=array[i]; + } + return max; + } + + private static int arrayMaxIndex(int[]array) + { + int index=0; + int max=0; + for(int i=0;imax) + index=i; + } + return index; + } + + private static double arrayAverage(double[] array) + { + int total=0; + double arrayLength = array.length; + for(int i=0;i Date: Tue, 24 Apr 2018 10:43:34 -0500 Subject: [PATCH 66/97] Intergalactic Vending Machine --- ch08/IntergalacticVendingMachine.java | 54 +++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 ch08/IntergalacticVendingMachine.java diff --git a/ch08/IntergalacticVendingMachine.java b/ch08/IntergalacticVendingMachine.java new file mode 100644 index 0000000..795e117 --- /dev/null +++ b/ch08/IntergalacticVendingMachine.java @@ -0,0 +1,54 @@ +import java.util.Scanner; + +public class IntergalacticVendingMachine +{ + public static void main(String[] args) + { + String[] food = {"Freeze Dried Sushi", "Spock's Brain Blast", "Alien Asparagus"}; + Scanner in = new Scanner(System.in); + int[] sales = new int[3]; + + int userChoice = -1; + System.out.println("Menu options: "); + printMenu(food); + System.out.println(); + + do + { + System.out.println("Which would you like?"); + + userChoice = in.nextInt(); + + printSales(sales, food, userChoice); + + }while(userChoice != 99); + + System.out.println("Goodbye!"); + } + + private static void printMenu(String[] food) + { + for(int i = 0; i< food.length;i++) + { + System.out.println("Option " + i + ": " + food[i]); + } + } + + private static void printSales(int[] sales, String[] food, int foodBought) + { + int totalBought = 0; + + + if (foodBought == 0) + sales[0]++; + if (foodBought == 1) + sales[1]++; + if (foodBought == 2) + sales[2]++; + System.out.println("Sold so far: \n"); + for(int i = 0;i Date: Tue, 24 Apr 2018 13:16:11 -0500 Subject: [PATCH 67/97] Intergalactic Vending Machine Gold version --- ch08/IntergalacticVendingMachine.java | 71 +++++++++++++++++++++------ 1 file changed, 55 insertions(+), 16 deletions(-) diff --git a/ch08/IntergalacticVendingMachine.java b/ch08/IntergalacticVendingMachine.java index 795e117..771db93 100644 --- a/ch08/IntergalacticVendingMachine.java +++ b/ch08/IntergalacticVendingMachine.java @@ -13,16 +13,38 @@ public static void main(String[] args) printMenu(food); System.out.println(); - do + while(userChoice != 99) { - System.out.println("Which would you like?"); + System.out.println("Which would you like?"); - userChoice = in.nextInt(); + userChoice = in.nextInt(); + for(int i = 0;i 0) + { + inputb /= 10; + n++; + } + if(input == 0) + { + n = 1; + } + int[] selection = new int[n]; + for(int i = 0;i Date: Wed, 25 Apr 2018 09:01:07 -0500 Subject: [PATCH 68/97] Intergalactic Vending Machine Gold version fixed --- ch08/IntergalacticVendingMachine.java | 80 +++++++++++++-------------- 1 file changed, 37 insertions(+), 43 deletions(-) diff --git a/ch08/IntergalacticVendingMachine.java b/ch08/IntergalacticVendingMachine.java index 771db93..cdbe366 100644 --- a/ch08/IntergalacticVendingMachine.java +++ b/ch08/IntergalacticVendingMachine.java @@ -8,37 +8,48 @@ public static void main(String[] args) Scanner in = new Scanner(System.in); int[] sales = new int[3]; - int userChoice = -1; + String userChoice = ""; System.out.println("Menu options: "); printMenu(food); System.out.println(); + boolean is99 = false; - while(userChoice != 99) + while(!is99) { System.out.println("Which would you like?"); - userChoice = in.nextInt(); - for(int i = 0;i 0) - { - inputb /= 10; - n++; - } - if(input == 0) - { - n = 1; - } - int[] selection = new int[n]; - for(int i = 0;i Date: Wed, 25 Apr 2018 09:07:38 -0500 Subject: [PATCH 69/97] Intergalactic Vending Machine Gold version fixed --- ch08/IntergalacticVendingMachine.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ch08/IntergalacticVendingMachine.java b/ch08/IntergalacticVendingMachine.java index cdbe366..f937ab0 100644 --- a/ch08/IntergalacticVendingMachine.java +++ b/ch08/IntergalacticVendingMachine.java @@ -39,9 +39,7 @@ public static void main(String[] args) System.out.println("Thank you for ordering " + food[2]); sales[2]++; } - System.out.println(); - printSales(sales, food); - System.out.println(); + if (i < array(userChoice).length - 1) { if (array(userChoice)[i] == '9' && array(userChoice)[i + 1] == '9') @@ -50,6 +48,7 @@ public static void main(String[] args) } } } + printSales(sales, food); } System.out.println("Total sales: "); for(int i = 0;i Date: Wed, 25 Apr 2018 13:42:59 -0500 Subject: [PATCH 70/97] StringUtilBronze --- ch09/StringUtil.java | 77 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 ch09/StringUtil.java diff --git a/ch09/StringUtil.java b/ch09/StringUtil.java new file mode 100644 index 0000000..2bed889 --- /dev/null +++ b/ch09/StringUtil.java @@ -0,0 +1,77 @@ +public class StringUtil +{ + public static void main(String[] args) + { + String x = "Hello!"; + System.out.println("getFirstCharacter: " + getFirstCharacter(x)); + System.out.println("getLastCharacter: " + getLastCharacter(x)); + System.out.println("getFirstTwoCharacter: " + getFirstTwoCharacter(x)); + System.out.println("getLastTwoCharacter: " + getLastTwoCharacter(x)); + System.out.println("getAllButFirstThreeCharacters: " + getAllButFirstThreeCharacters(x)); + printCharacters(x); + printPhoneNumber("501-555-0100"); + System.out.println("Index of first e: " + findFirstE(x)); + System.out.println("Is it Finn? " + isFinn("Finn")); + System.out.println("Is it Finn? " + isFinn("Jake")); + } + + private static String getFirstCharacter(String x) + { + x = x.substring(0,1); + return x; + } + + private static String getLastCharacter(String x) + { + x = x.substring(x.length()-1); + return x; + } + + private static String getFirstTwoCharacter(String x) + { + x = x.substring(0,2); + return x; + } + + private static String getLastTwoCharacter(String x) + { + x = x.substring(x.length()-2,x.length()); + return x; + } + + private static String getAllButFirstThreeCharacters(String x) + { + x = x.substring(3); + return x; + } + + private static void printCharacters(String x) + { + System.out.println("Characters in string by position: "); + for(int i=0;i Date: Wed, 25 Apr 2018 14:39:44 -0500 Subject: [PATCH 71/97] StringUtilSilver --- ch09/StringUtilSilver.java | 122 +++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 ch09/StringUtilSilver.java diff --git a/ch09/StringUtilSilver.java b/ch09/StringUtilSilver.java new file mode 100644 index 0000000..a8befcf --- /dev/null +++ b/ch09/StringUtilSilver.java @@ -0,0 +1,122 @@ +public class StringUtilSilver +{ + public static void main(String[] args) + { + printPhoneNumber("1231231234"); + + } + + private static void printPhoneNumber(String x) + { + int[] phoneNumber = new int[12]; + if(x.length()==12) + { + for(int i=0;i=0&&i<=3||i>=4&&i<=7||i>=8&&i<=12) + { + if ((x.substring(i, i + 1)).equals("1")) + { + phoneNumber[i] = 1; + } + + if ((x.substring(i, i + 1)).equals("a") || (x.substring(i, i + 1)).equals("b") || (x.substring(i, i + 1)).equals("c") || (x.substring(i, i + 1)).equals("2")) + { + phoneNumber[i] = 2; + } + else if ((x.substring(i, i + 1)).equals("d") || (x.substring(i, i + 1)).equals("e") || (x.substring(i, i + 1)).equals("f") || (x.substring(i, i + 1)).equals("3")) + { + phoneNumber[i] = 3; + } + else if ((x.substring(i, i + 1)).equals("h") || (x.substring(i, i + 1)).equals("i") || (x.substring(i, i + 1)).equals("j") || (x.substring(i, i + 1)).equals("4")) + { + phoneNumber[i] = 4; + } + else if ((x.substring(i, i + 1)).equals("k") || (x.substring(i, i + 1)).equals("l") || (x.substring(i, i + 1)).equals("m") || (x.substring(i, i + 1)).equals("5")) + { + phoneNumber[i] = 5; + } + else if ((x.substring(i, i + 1)).equals("n") || (x.substring(i, i + 1)).equals("o") || (x.substring(i, i + 1)).equals("p") || (x.substring(i, i + 1)).equals("6")) + { + phoneNumber[i] = 6; + } + else if ((x.substring(i, i + 1)).equals("q") || (x.substring(i, i + 1)).equals("r") || (x.substring(i, i + 1)).equals("s") || (x.substring(i, i + 1)).equals("7")) + { + phoneNumber[i] = 7; + } + else if ((x.substring(i, i + 1)).equals("t") || (x.substring(i, i + 1)).equals("u") || (x.substring(i, i + 1)).equals("v") || (x.substring(i, i + 1)).equals("8")) + { + phoneNumber[i] = 8; + } + else if ((x.substring(i, i + 1)).equals("x") || (x.substring(i, i + 1)).equals("y") || (x.substring(i, i + 1)).equals("z") || (x.substring(i, i + 1)).equals("9")) + { + phoneNumber[i] = 9; + } + else if ((x.substring(i, i + 1)).equals("+") || (x.substring(i, i + 1)).equals("0")) + { + phoneNumber[i] = 0; + } + } + } + + System.out.println("Phone Number: "); + System.out.print("Area code: " + phoneNumber[0] + phoneNumber[1] + phoneNumber[2] + "\t"); + System.out.print("Exchange: " + phoneNumber[4] + phoneNumber[5] + phoneNumber[6] + "\t"); + System.out.print("Line Number: " + phoneNumber[8] + phoneNumber[9] + phoneNumber[10] + phoneNumber[11]+ "\t"); + System.out.println(); + } + if(x.length()==10) + { + for(int i=0;i Date: Thu, 26 Apr 2018 09:30:57 -0500 Subject: [PATCH 72/97] StringUtilSilver --- ch09/StringUtilSilver.java | 164 +++++++++++++++++++++++++++---------- 1 file changed, 120 insertions(+), 44 deletions(-) diff --git a/ch09/StringUtilSilver.java b/ch09/StringUtilSilver.java index a8befcf..cd0746e 100644 --- a/ch09/StringUtilSilver.java +++ b/ch09/StringUtilSilver.java @@ -1,9 +1,87 @@ +import java.util.Arrays; + public class StringUtilSilver { public static void main(String[] args) { - printPhoneNumber("1231231234"); + printPhoneNumber("565-786-5677"); + System.out.println("Reverse Hello: " + reverse("Hello")); + System.out.println("Is Palindrome: " + isPalindrome("")); + System.out.println("Starts with learn: " + allLetters("dog")); + String[] names = {"t"}; + System.out.println("All letters no dupes: " + allLettersNoDupes(names)); + } + + private static boolean allLettersNoDupes(String[] x) + { + if(x.length==0) + return false; + if(x.length==1&&x[0].substring(0, 1).equals("l")||x[0].substring(0, 1).equals("e")||x[0].substring(0, 1).equals("a")||x[0].substring(0, 1).equals("r")||x[0].substring(0, 1).equals("n")) + return true; + + int count=0; + for(int i=0;i=0&&i<=3||i>=4&&i<=7||i>=8&&i<=12) + if ((x.substring(i, i + 1)).equals("1")) { - if ((x.substring(i, i + 1)).equals("1")) - { - phoneNumber[i] = 1; - } - - if ((x.substring(i, i + 1)).equals("a") || (x.substring(i, i + 1)).equals("b") || (x.substring(i, i + 1)).equals("c") || (x.substring(i, i + 1)).equals("2")) - { - phoneNumber[i] = 2; - } - else if ((x.substring(i, i + 1)).equals("d") || (x.substring(i, i + 1)).equals("e") || (x.substring(i, i + 1)).equals("f") || (x.substring(i, i + 1)).equals("3")) - { - phoneNumber[i] = 3; - } - else if ((x.substring(i, i + 1)).equals("h") || (x.substring(i, i + 1)).equals("i") || (x.substring(i, i + 1)).equals("j") || (x.substring(i, i + 1)).equals("4")) - { - phoneNumber[i] = 4; - } - else if ((x.substring(i, i + 1)).equals("k") || (x.substring(i, i + 1)).equals("l") || (x.substring(i, i + 1)).equals("m") || (x.substring(i, i + 1)).equals("5")) - { - phoneNumber[i] = 5; - } - else if ((x.substring(i, i + 1)).equals("n") || (x.substring(i, i + 1)).equals("o") || (x.substring(i, i + 1)).equals("p") || (x.substring(i, i + 1)).equals("6")) - { - phoneNumber[i] = 6; - } - else if ((x.substring(i, i + 1)).equals("q") || (x.substring(i, i + 1)).equals("r") || (x.substring(i, i + 1)).equals("s") || (x.substring(i, i + 1)).equals("7")) - { - phoneNumber[i] = 7; - } - else if ((x.substring(i, i + 1)).equals("t") || (x.substring(i, i + 1)).equals("u") || (x.substring(i, i + 1)).equals("v") || (x.substring(i, i + 1)).equals("8")) - { - phoneNumber[i] = 8; - } - else if ((x.substring(i, i + 1)).equals("x") || (x.substring(i, i + 1)).equals("y") || (x.substring(i, i + 1)).equals("z") || (x.substring(i, i + 1)).equals("9")) - { - phoneNumber[i] = 9; - } - else if ((x.substring(i, i + 1)).equals("+") || (x.substring(i, i + 1)).equals("0")) - { - phoneNumber[i] = 0; - } + phoneNumber[i] = 1; + } + else if ((x.substring(i, i + 1)).equals("a") || (x.substring(i, i + 1)).equals("b") || (x.substring(i, i + 1)).equals("c") || (x.substring(i, i + 1)).equals("2")) + { + phoneNumber[i] = 2; + } + else if ((x.substring(i, i + 1)).equals("d") || (x.substring(i, i + 1)).equals("e") || (x.substring(i, i + 1)).equals("f") || (x.substring(i, i + 1)).equals("3")) + { + phoneNumber[i] = 3; + } + else if ((x.substring(i, i + 1)).equals("h") || (x.substring(i, i + 1)).equals("i") || (x.substring(i, i + 1)).equals("j") || (x.substring(i, i + 1)).equals("4")) + { + phoneNumber[i] = 4; + } + else if ((x.substring(i, i + 1)).equals("k") || (x.substring(i, i + 1)).equals("l") || (x.substring(i, i + 1)).equals("m") || (x.substring(i, i + 1)).equals("5")) + { + phoneNumber[i] = 5; + } + else if ((x.substring(i, i + 1)).equals("n") || (x.substring(i, i + 1)).equals("o") || (x.substring(i, i + 1)).equals("p") || (x.substring(i, i + 1)).equals("6")) + { + phoneNumber[i] = 6; + } + else if ((x.substring(i, i + 1)).equals("q") || (x.substring(i, i + 1)).equals("r") || (x.substring(i, i + 1)).equals("s") || (x.substring(i, i + 1)).equals("7")) + { + phoneNumber[i] = 7; + } + else if ((x.substring(i, i + 1)).equals("t") || (x.substring(i, i + 1)).equals("u") || (x.substring(i, i + 1)).equals("v") || (x.substring(i, i + 1)).equals("8")) + { + phoneNumber[i] = 8; + } + else if ((x.substring(i, i + 1)).equals("x") || (x.substring(i, i + 1)).equals("y") || (x.substring(i, i + 1)).equals("z") || (x.substring(i, i + 1)).equals("9")) + { + phoneNumber[i] = 9; + } + else if ((x.substring(i, i + 1)).equals("+") || (x.substring(i, i + 1)).equals("0")) + { + phoneNumber[i] = 0; } } @@ -74,7 +148,7 @@ else if ((x.substring(i, i + 1)).equals("+") || (x.substring(i, i + 1)).equals(" phoneNumber[i] = 1; } - if ((x.substring(i, i + 1)).equals("a") || (x.substring(i, i + 1)).equals("b") || (x.substring(i, i + 1)).equals("c") || (x.substring(i, i + 1)).equals("2")) + else if ((x.substring(i, i + 1)).equals("a") || (x.substring(i, i + 1)).equals("b") || (x.substring(i, i + 1)).equals("c") || (x.substring(i, i + 1)).equals("2")) { phoneNumber[i] = 2; } @@ -119,4 +193,6 @@ else if ((x.substring(i, i + 1)).equals("+") || (x.substring(i, i + 1)).equals(" System.out.println(); } } + + } From d0ca52f0a4ab77f9d48d8d8fdd5de7eda2d508c5 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Fri, 27 Apr 2018 15:16:54 -0500 Subject: [PATCH 73/97] 11-S-2 --- ch11/Date.java | 33 +++++++++++++++++++++++++++++++++ ch11/DateTester.java | 12 ++++++++++++ ch11/Planet.java | 14 ++++++++++++++ ch11/PlanetTester.java | 32 ++++++++++++++++++++++++++++++++ ch11/Player.java | 27 +++++++++++++++++++++++++++ ch11/PlayerTest.java | 15 +++++++++++++++ 6 files changed, 133 insertions(+) create mode 100644 ch11/Date.java create mode 100644 ch11/DateTester.java create mode 100644 ch11/Planet.java create mode 100644 ch11/PlanetTester.java create mode 100644 ch11/Player.java create mode 100644 ch11/PlayerTest.java diff --git a/ch11/Date.java b/ch11/Date.java new file mode 100644 index 0000000..17df2c6 --- /dev/null +++ b/ch11/Date.java @@ -0,0 +1,33 @@ +public class Date +{ + private int day; + private int month; + private int year; + + public Date(int day, int month, int year) + { + this.day = day; + this.month = month; + this.year = year; + } + + public int getDay() + { + return day; + } + + public int getMonth() + { + return month; + } + + public int getYear() + { + return year; + } + + public String getFormattedDate() + { + return "" + year + "-" + month + "-" + day; + } +} diff --git a/ch11/DateTester.java b/ch11/DateTester.java new file mode 100644 index 0000000..a25cacb --- /dev/null +++ b/ch11/DateTester.java @@ -0,0 +1,12 @@ +public class DateTester +{ + public static void main(String[] args) + { + Date currentDate = new Date(27,4,2018); + + System.out.println(currentDate.getDay()); + System.out.println(currentDate.getMonth()); + System.out.println(currentDate.getYear()); + System.out.println(currentDate.getFormattedDate()); + } +} diff --git a/ch11/Planet.java b/ch11/Planet.java new file mode 100644 index 0000000..d181f3a --- /dev/null +++ b/ch11/Planet.java @@ -0,0 +1,14 @@ +public class Planet +{ + private String name; + + public Planet(String name) + { + this.name = name; + } + + public String getName() + { + return name; + } +} diff --git a/ch11/PlanetTester.java b/ch11/PlanetTester.java new file mode 100644 index 0000000..269d664 --- /dev/null +++ b/ch11/PlanetTester.java @@ -0,0 +1,32 @@ +public class PlanetTester +{ + public static void main(String[] args) + { + Planet mercury = new Planet("Mercury"); + Planet venus = new Planet("Venus"); + Planet earth = new Planet("Earth"); + Planet mars = new Planet("mars"); + Planet jupiter = new Planet("Jupiter"); + Planet saturn = new Planet("Saturn"); + Planet uranus = new Planet("Uranus"); + Planet neptune = new Planet("Neptune"); + Planet pluto = new Planet("Pluto"); + + Planet[] planets = new Planet[9]; + + planets[0] = mercury; + planets[1] = venus; + planets[2] = earth; + planets[3] = mars; + planets[4] = jupiter; + planets[5] = saturn; + planets[6] = uranus; + planets[7] = neptune; + planets[8] = pluto; + + for(int i=0;i Date: Fri, 27 Apr 2018 15:17:06 -0500 Subject: [PATCH 74/97] 11-S-2 --- ch11/TimeTester.java | 11 +++++++++++ ch11/Timer.java | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 ch11/TimeTester.java create mode 100644 ch11/Timer.java diff --git a/ch11/TimeTester.java b/ch11/TimeTester.java new file mode 100644 index 0000000..4018eb1 --- /dev/null +++ b/ch11/TimeTester.java @@ -0,0 +1,11 @@ +public class TimeTester +{ + public static void main(String[] args) + { + Timer currentTime = new Timer(2,36,false); + Timer time1 = new Timer(9,45); + Timer time2 = new Timer(14,30); + + System.out.println(currentTime.getMilitaryTime()); + } +} diff --git a/ch11/Timer.java b/ch11/Timer.java new file mode 100644 index 0000000..69e379e --- /dev/null +++ b/ch11/Timer.java @@ -0,0 +1,34 @@ +public class Timer +{ + private int hour; + private int minute; + + public Timer(int hour, int minute) + { + this.hour = hour; + this.minute=minute; + } + + public Timer(int hour, int minute, boolean pm) + { + + if(pm) + this.hour=12+hour; + else + this.hour=hour; + this.minute=minute; + } + + public String getMilitaryTime() + { + return "" + hour + ":" + minute; + } + + public String getTime() + { + if(hour<13) + return "" + hour + ":" + minute + " AM"; + else + return "" + (Math.abs(12-hour)) + ":" + minute + " PM"; + } +} From ca100953a9cab8b8be28ff8ce5e8d9f56462d306 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 30 Apr 2018 10:37:19 -0500 Subject: [PATCH 75/97] 11-S-2 --- ch11/Date.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ch11/Date.java b/ch11/Date.java index 17df2c6..fb078cf 100644 --- a/ch11/Date.java +++ b/ch11/Date.java @@ -28,6 +28,20 @@ public int getYear() public String getFormattedDate() { - return "" + year + "-" + month + "-" + day; + String formattedDate = ""; + + formattedDate += year + "-"; + + if(month<10) + formattedDate+=0; + + formattedDate+= month + "-"; + + if(day<10) + formattedDate+=0; + + formattedDate+=day; + + return formattedDate; } } From bd2e2d1eaf509dd1ec547414c3f62adcdb25d779 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 30 Apr 2018 13:58:09 -0500 Subject: [PATCH 76/97] ch 9 gold string util --- ch09/StringUtilGold.java | 70 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 ch09/StringUtilGold.java diff --git a/ch09/StringUtilGold.java b/ch09/StringUtilGold.java new file mode 100644 index 0000000..adc97b0 --- /dev/null +++ b/ch09/StringUtilGold.java @@ -0,0 +1,70 @@ +public class StringUtilGold +{ + public static void main(String[] args) + { + String[] test = {"zebra", "ant", "car", "boat", "alien", "aardvark", "giraffe", "owl", "earth" }; + for(int i=0;i 0) + return false; + prevCount = count; + } + } + return true; + } + + private static String[] sort(String[] str) + { + + String holder; + for(int i=0;i0) + { + holder=str[i]; + str[i]=str[j]; + str[j]=holder; + } + + } + } + + return str; + } + +} From 800516ffd8a1df4d116c9743a9d5786807923bda Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 30 Apr 2018 15:09:40 -0500 Subject: [PATCH 77/97] Round About --- FinishLine/src/RoundAbout.java | 76 ++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 FinishLine/src/RoundAbout.java diff --git a/FinishLine/src/RoundAbout.java b/FinishLine/src/RoundAbout.java new file mode 100644 index 0000000..f9baaed --- /dev/null +++ b/FinishLine/src/RoundAbout.java @@ -0,0 +1,76 @@ +public class RoundAbout +{ + private static int roundNumber = 0; + private static int gameNumber = 0; + + private static Die die1 = new Die(); + private static Die die2 = new Die(); + + private static Peg redPeg = new Peg(); + private static Peg bluePeg = new Peg(); + + public void play() + { + redPeg.resetPosition(); + bluePeg.resetPosition(); + + gameNumber++; + + while(redPeg.getPosition()<11&&bluePeg.getPosition()<11) + { + takeTurn(redPeg); + System.out.print("Red position: " + redPeg.getPosition() + "\t"); + + if(redPeg.getPosition()==bluePeg.getPosition()) + { + if(bluePeg.getPosition()>0) + System.out.println("Red knocked blue back to start!"); + bluePeg.resetPosition(); + } + + takeTurn(bluePeg); + System.out.println("Blue position: " + bluePeg.getPosition()); + + if(redPeg.getPosition()==bluePeg.getPosition()) + { + if(redPeg.getPosition()>0) + System.out.println("Blue knocked red back to start!"); + + redPeg.resetPosition(); + + } + } + + if(bluePeg.getPosition()==11) + System.out.println("Blue won!"); + if(redPeg.getPosition()==11) + System.out.println("Red won!"); + } + + private static void takeTurn(Peg peg) + { + System.out.println(); + + die1.roll(); + die2.roll(); + + System.out.println(die1.getValue()); + System.out.println(die2.getValue()); + + if(peg.getPosition()==0) + { + if (die1.getValue()==5|| + die2.getValue()==5|| + die1.getValue()+die2.getValue()==5) + peg.positionUp(); + } + + if(peg.getPosition()>0) + { + if (peg.getNextPosition() == die1.getValue() || + peg.getNextPosition() == die2.getValue() || + peg.getNextPosition() == die1.getValue() + die2.getValue()) + peg.positionUp(); + } + } +} From e06c25f4cea06a27e1c0838d4302997556bb72e6 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 30 Apr 2018 15:36:39 -0500 Subject: [PATCH 78/97] Basketball --- FinishLine/src/Basketball.java | 113 +++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 FinishLine/src/Basketball.java diff --git a/FinishLine/src/Basketball.java b/FinishLine/src/Basketball.java new file mode 100644 index 0000000..0cc29e9 --- /dev/null +++ b/FinishLine/src/Basketball.java @@ -0,0 +1,113 @@ +public class Basketball +{ + private static int roundNumber = 0; + private static int gameNumber = 0; + + private static Die die1 = new Die(); + private static Die die2 = new Die(); + + private static Peg redPeg = new Peg(); + private static Peg bluePeg = new Peg(); + + public void play() + { + while(bluePeg.getPosition()<22&&redPeg.getPosition()<22) + { + takeTurn(bluePeg); + System.out.println("Blue has: " + bluePeg.getPosition() + " points"); + System.out.println(); + + takeTurn(redPeg); + System.out.println("Red has: " + redPeg.getPosition() + " points"); + System.out.println(); + } + + if(redPeg.getPosition()>bluePeg.getPosition()) + System.out.println("Red wins with " + redPeg.getPosition() + " points!"); + if(bluePeg.getPosition()>redPeg.getPosition()) + System.out.println("Blue wins with " + bluePeg.getPosition() + " points!"); + if(bluePeg.getPosition()==redPeg.getPosition()) + System.out.println("Wow what a game! It ended in a tie!"); + } + + private void takeTurn(Peg peg) + { + die1.roll(); + die2.roll(); + + System.out.println("The roll is: " + (die1.getValue() + die2.getValue())); + + if(die1.getValue()+die2.getValue()==2) + { + peg.positionUp(); + peg.positionUp(); + peg.positionUp(); + System.out.println("There's a three pointer!"); + } + + if(die1.getValue()+die2.getValue()==3) + { + System.out.println("Walking violation, unlucky!"); + } + + if(die1.getValue()+die2.getValue()==4) + { + peg.positionUp(); + peg.positionUp(); + System.out.println("The two point shot is good!"); + } + + if(die1.getValue()+die2.getValue()==5) + { + peg.positionUp(); + System.out.println("He converts one foul shot!"); + } + + if(die1.getValue()+die2.getValue()==6) + { + peg.positionUp(); + peg.positionUp(); + System.out.println("Two point shot!"); + } + + if(die1.getValue()+die2.getValue()==7) + { + System.out.println("Double dribble violation, unlucky!"); + } + + if(die1.getValue()+die2.getValue()==8) + { + peg.positionUp(); + peg.positionUp(); + System.out.println("He converts both foul shots!"); + } + + if(die1.getValue()+die2.getValue()==9) + { + System.out.println("Ohh, close but no cigar!"); + } + + if(die1.getValue()+die2.getValue()==10) + { + peg.positionUp(); + peg.positionUp(); + peg.positionUp(); + System.out.println("Wow, he made it from downtown!"); + } + + if(die1.getValue()+die2.getValue()==11) + { + System.out.println("Offensive foul, unlucky!"); + } + + if(die1.getValue()+die2.getValue()==12) + { + peg.positionUp(); + peg.positionUp(); + peg.positionUp(); + System.out.println("A wonderful three point shot!"); + } + + + } +} From 40b9adfa356c5c5ef62c6002ba60340d00df61ff Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 1 May 2018 09:04:23 -0500 Subject: [PATCH 79/97] Football --- FinishLine/src/Football.java | 196 +++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 FinishLine/src/Football.java diff --git a/FinishLine/src/Football.java b/FinishLine/src/Football.java new file mode 100644 index 0000000..51382fb --- /dev/null +++ b/FinishLine/src/Football.java @@ -0,0 +1,196 @@ +import java.util.SortedMap; +import java.util.Scanner; + +public class Football +{ + private int yardsToFirst = 10; + + private static Die die1 = new Die(); + private static Die die2 = new Die(); + + private static Peg redPeg = new Peg(); + private static Peg bluePeg = new Peg(); + + private Scanner in = new Scanner(System.in); + + public void play() + { + redPeg.setPosition(20); + while(redPeg.getScore()<28&&bluePeg.getScore()<28) + { + takeTurn(redPeg, bluePeg); + System.out.println("Red's score is now: " + redPeg.getScore()); + + takeTurn(bluePeg, redPeg); + System.out.println("Blue's score is now: " + bluePeg.getScore()); + } + + + if(redPeg.getScore()>bluePeg.getScore()) + { + System.out.println("Red wins with " + redPeg.getScore() + " points and blue had " + bluePeg.getScore()); + } + + if(bluePeg.getScore()>redPeg.getScore()) + { + System.out.println("Blue wins with " + bluePeg.getScore() + " points and red had " + redPeg.getScore()); + } + + if(bluePeg.getScore()==redPeg.getScore()) + { + System.out.println("The game was a tie at " + redPeg.getPosition() + " points! What a game!"); + } + } + + private void takeTurn(Peg peg, Peg otherPeg) + { + int down = 1; + int yardsToFirst = 10; + String punt = ""; + while (down<5) + { + System.out.println(); + die1.roll(); + die2.roll(); + + System.out.println("Down: " + down); + System.out.println("Yards to TD: " + (100 - peg.getPosition())); + System.out.println("Yards to first: " + yardsToFirst); + System.out.println("The roll is: " + (die1.getValue() + die2.getValue())); + + + if (die1.getValue() + die2.getValue() == 2) + { + System.out.println("Touchdown!!!!!!!!"); + peg.scoreUp(6); + + die1.roll(); + if(die1.getValue()>=5) + { + System.out.println("The extra point is good!"); + peg.scoreUp(1); + } + + otherPeg.setPosition(20); + down=5; + } + + if (die1.getValue() + die2.getValue() == 3) + { + System.out.println("Interception!"); + System.out.println("What a job on defense! It's a turnover!"); + + otherPeg.setPosition(100-(peg.getPosition()-10)); + break; + } + + if (die1.getValue() + die2.getValue() == 4) + { + peg.setPosition(peg.getPosition()+20); + System.out.println("It's a reverse for a gain of 20 yards! Amazing!"); + yardsToFirst-=20; + down++; + } + + if (die1.getValue() + die2.getValue() == 5) + { + System.out.println("The quaterback is sacked for a huge loss!"); + if(peg.getPosition()>10) + peg.setPosition(peg.getPosition()-10); + else + peg.setPosition((0)); + yardsToFirst+=10; + down++; + } + + if (die1.getValue() + die2.getValue() == 6) + { + peg.setPosition(peg.getPosition()+5); + System.out.println("Its a draw for a gain of 5"); + yardsToFirst-=5; + down++; + } + + if (die1.getValue() + die2.getValue() == 7) + { + System.out.println("Incomplete pass over the middle"); + down++; + } + + if (die1.getValue() + die2.getValue() == 8) + { + peg.setPosition(peg.getPosition()+5); + System.out.println("The quarterback completes a simple screen pass for 5 yards"); + yardsToFirst-=5; + down++; + } + + if (die1.getValue() + die2.getValue() == 9) + { + System.out.println("What a pass down the middle! It's a 25 yard gain!"); + peg.setPosition(peg.getPosition()+25); + yardsToFirst-=25; + down++; + } + + if (die1.getValue() + die2.getValue() == 10) + { + System.out.println("They stuffed that run!"); + down++; + } + + if (die1.getValue() + die2.getValue() == 11) + { + System.out.println("It's a fumble!"); + down = 5; + } + + if (die1.getValue() + die2.getValue() == 12) + { + peg.setPosition(peg.getPosition()+30); + System.out.println("What a throw, a 30 yard gain!"); + yardsToFirst-=30; + down++; + } + + if(peg.getPosition()>=100) + { + System.out.println("Touchdown!!!!!!!!!"); + peg.scoreUp(6); + die1.roll(); + if(die1.getValue()>=5) + { + System.out.println("The extra point is good!"); + peg.scoreUp(1); + } + otherPeg.setPosition(20); + break; + } + + if(yardsToFirst<=0) + down=1; + if(yardsToFirst<=0) + yardsToFirst=10; + + if(down==4) + { + System.out.println("It's fourth down. Would you like to punt? (Y/N)"); + punt = in.nextLine(); + + if(punt.equals("y")||punt.equals("Y")) + { + die1.roll(); + otherPeg.setPosition(peg.getPosition()-(10*die1.getValue())); + System.out.println("The ball was punted " + (10*die1.getValue()) + " yards"); + break; + } + } + + if(down>4) + { + System.out.println("What a job on defense, its a turnover!"); + otherPeg.setPosition(100-peg.getPosition()); + } + } + } +} From 833a0d480e1a960ccca3139c36cbc61683c6318f Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 1 May 2018 09:09:23 -0500 Subject: [PATCH 80/97] Peg class --- FinishLine/src/Peg.java | 51 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 FinishLine/src/Peg.java diff --git a/FinishLine/src/Peg.java b/FinishLine/src/Peg.java new file mode 100644 index 0000000..4c61f65 --- /dev/null +++ b/FinishLine/src/Peg.java @@ -0,0 +1,51 @@ +public class Peg +{ + private int position; + private int score; + + public Peg() + { + position = 0; + } + + public int getPosition() + { + return position; + } + + public void setPosition(int position) + { + this.position = position; + } + + public int getNextPosition() + { + return position + 1; + } + + public void positionUp() + { + position++; + } + + public void resetPosition() + { + position = 0; + } + + public void positionDown() + { + position--; + } + + public void scoreUp(int score) + { + this.score+= score; + } + + public int getScore() + { + return score; + } + +} From 73ff22063c68102623b7758136fff25118fa0f51 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 1 May 2018 09:09:43 -0500 Subject: [PATCH 81/97] Die class --- FinishLine/src/Die.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 FinishLine/src/Die.java diff --git a/FinishLine/src/Die.java b/FinishLine/src/Die.java new file mode 100644 index 0000000..008ffa9 --- /dev/null +++ b/FinishLine/src/Die.java @@ -0,0 +1,22 @@ +import java.util.Random; + +public class Die +{ + private int value; + + public Die() + { + roll(); + } + + public int getValue() + { + return value; + } + + public void roll() + { + Random random = new Random(); + value = random.nextInt(6) + 1; + } +} From 1e45c7d7abd8c16cdb0cb0dfe6264dd18a72f3fa Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 1 May 2018 09:10:06 -0500 Subject: [PATCH 82/97] Finish line class --- FinishLine/src/FinishLine.java | 99 ++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 FinishLine/src/FinishLine.java diff --git a/FinishLine/src/FinishLine.java b/FinishLine/src/FinishLine.java new file mode 100644 index 0000000..7168db0 --- /dev/null +++ b/FinishLine/src/FinishLine.java @@ -0,0 +1,99 @@ +public class FinishLine +{ + private static int roundNumber = 0; + private static int gameNumber = 0; + + private static Die die1 = new Die(); + private static Die die2 = new Die(); + + private static Peg redPeg = new Peg(); + private static Peg bluePeg = new Peg(); + + private static int redWin = 0; + private static int blueWin = 0; + private static int tie = 0; + + public void play() + { + gameNumber++; + System.out.println("Game number: " + gameNumber); + + redPeg.positionUp(); + bluePeg.positionUp(); + + while (redPeg.getPosition() < 10 && bluePeg.getPosition() < 10) + { + roundNumber++; + System.out.println("Round number: " + roundNumber); + takeTurn(redPeg); + System.out.print("Red position: " + redPeg.getPosition() + "\t"); + + takeTurn(bluePeg); + System.out.println("Blue position: " + bluePeg.getPosition()); + System.out.println(); + } + + resetRound(); + + if (redPeg.getPosition() == bluePeg.getPosition()) + { + System.out.println("It was a tie! Both pegs ended up at 10"); + tie++; + System.out.println(); + } + + else if (bluePeg.getPosition() == 10) + { + System.out.println("Blue won!"); + blueWin++; + } + + else if (redPeg.getPosition() == 10) + { + System.out.println("Red won!"); + redWin++; + } + + bluePeg.resetPosition(); + redPeg.resetPosition(); + } + + public static int getRedWin() + { + return redWin; + } + + public static int getBlueWin() + { + return blueWin; + } + + public static int getTie() + { + return tie; + } + + private static void takeTurn(Peg peg) + { + System.out.println(); + + die1.roll(); + die2.roll(); + + if (peg.getNextPosition() == die1.getValue() || + peg.getNextPosition() == die2.getValue() || + peg.getNextPosition() == die1.getValue() + die2.getValue()) + + peg.positionUp(); + } + + public void resetRound() + { + roundNumber=0; + } + + public static int getGameNumber() + { + return gameNumber; + } +} From e0ddcdcfa6a9cd0fe52ff650296ea4d032186ac5 Mon Sep 17 00:00:00 2001 From: douglasjd1 <38441018+douglasjd1@users.noreply.github.com> Date: Fri, 4 May 2018 09:39:17 -0500 Subject: [PATCH 83/97] Add files via upload --- ListItem.java | 33 +++++++++ ShoppingList.java | 168 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 201 insertions(+) create mode 100644 ListItem.java create mode 100644 ShoppingList.java diff --git a/ListItem.java b/ListItem.java new file mode 100644 index 0000000..7819f95 --- /dev/null +++ b/ListItem.java @@ -0,0 +1,33 @@ +package com.company; + +public class ListItem +{ + private String name; + private int quantity = 1; + + ListItem(String name) + { + this.name = name; + this.quantity = quantity; + } + + public String getName() + { + return name; + } + + public int getQuantity() + { + return quantity; + } + + public void quantityUp() + { + quantity++; + } + + public void quantityDown() + { + quantity--; + } +} diff --git a/ShoppingList.java b/ShoppingList.java new file mode 100644 index 0000000..be27494 --- /dev/null +++ b/ShoppingList.java @@ -0,0 +1,168 @@ +package com.company; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Scanner; + +public class ShoppingList +{ + public static void main(String[] args) + { + ShoppingList shoppingList = new ShoppingList(); + + run(); + } + + public static void run() + { + Scanner scanner = new Scanner(System.in); + boolean go = true; + + System.out.println("Welcome to my store. Go buy something."); + + List shoppingList = new ArrayList<>(); + + while(go) + { + System.out.println("Enter one of the following commands:"); + System.out.println(); + System.out.println("Add "); + System.out.println("Print"); + System.out.println("Remove "); + System.out.println("Find"); + System.out.println("Clear"); + System.out.println("Exit"); + + String commandLine = scanner.nextLine(); + String[] commands = commandLine.split(" "); + String command = commands[0].toUpperCase(); + + boolean isInList = false; + int itemIndex=0; + if(command.equals("ADD")) + { + String itemName = commands[1]; + for(int i = 0;i=6) + { + System.out.println("I'm sorry, Dave, I'm afraid I can't do that."); + } + if(!isInList) + { + shoppingList.add(new ListItem(commands[1])); + } + } + + else if(command.equals("PRINT")) + { + if(shoppingList.size()==0) + { + System.out.println("Your shopping list is empty"); + System.out.println(); + } + for(int i=0;i shoppingList.size()) + { + System.out.println("That's not a valid item number. Try again."); + System.out.println(); + } + + else + { + shoppingList.get(index - 1).quantityDown(); + if (shoppingList.get(index - 1).getQuantity() <= 0) + { + shoppingList.remove(index - 1); + } + } + } + + else + { + System.out.println("That is not a valid item number."); + System.out.println(); + } + + } + + else if(command.equals("CLEAR")) + { + shoppingList.clear(); + } + + else if(command.equals("FIND")) + { + isInList = false; + + if(shoppingList.size()==0) + { + System.out.println("Your shopping list is empty"); + System.out.println(); + } + + for(int i = 0;i Date: Fri, 4 May 2018 09:40:31 -0500 Subject: [PATCH 84/97] Create Shopping List --- Shopping List | 1 + 1 file changed, 1 insertion(+) create mode 100644 Shopping List diff --git a/Shopping List b/Shopping List new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Shopping List @@ -0,0 +1 @@ + From 653da80ba00076f3103e7c7cadb358c7cd93d8cb Mon Sep 17 00:00:00 2001 From: douglasjd1 <38441018+douglasjd1@users.noreply.github.com> Date: Fri, 4 May 2018 09:40:50 -0500 Subject: [PATCH 85/97] Delete Shopping List --- Shopping List | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Shopping List diff --git a/Shopping List b/Shopping List deleted file mode 100644 index 8b13789..0000000 --- a/Shopping List +++ /dev/null @@ -1 +0,0 @@ - From 7d3495ac5bfb89497872f504355f7f782c4561dd Mon Sep 17 00:00:00 2001 From: douglasjd1 <38441018+douglasjd1@users.noreply.github.com> Date: Fri, 4 May 2018 09:44:57 -0500 Subject: [PATCH 86/97] Delete ShoppingList.java --- ShoppingList.java | 168 ---------------------------------------------- 1 file changed, 168 deletions(-) delete mode 100644 ShoppingList.java diff --git a/ShoppingList.java b/ShoppingList.java deleted file mode 100644 index be27494..0000000 --- a/ShoppingList.java +++ /dev/null @@ -1,168 +0,0 @@ -package com.company; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Scanner; - -public class ShoppingList -{ - public static void main(String[] args) - { - ShoppingList shoppingList = new ShoppingList(); - - run(); - } - - public static void run() - { - Scanner scanner = new Scanner(System.in); - boolean go = true; - - System.out.println("Welcome to my store. Go buy something."); - - List shoppingList = new ArrayList<>(); - - while(go) - { - System.out.println("Enter one of the following commands:"); - System.out.println(); - System.out.println("Add "); - System.out.println("Print"); - System.out.println("Remove "); - System.out.println("Find"); - System.out.println("Clear"); - System.out.println("Exit"); - - String commandLine = scanner.nextLine(); - String[] commands = commandLine.split(" "); - String command = commands[0].toUpperCase(); - - boolean isInList = false; - int itemIndex=0; - if(command.equals("ADD")) - { - String itemName = commands[1]; - for(int i = 0;i=6) - { - System.out.println("I'm sorry, Dave, I'm afraid I can't do that."); - } - if(!isInList) - { - shoppingList.add(new ListItem(commands[1])); - } - } - - else if(command.equals("PRINT")) - { - if(shoppingList.size()==0) - { - System.out.println("Your shopping list is empty"); - System.out.println(); - } - for(int i=0;i shoppingList.size()) - { - System.out.println("That's not a valid item number. Try again."); - System.out.println(); - } - - else - { - shoppingList.get(index - 1).quantityDown(); - if (shoppingList.get(index - 1).getQuantity() <= 0) - { - shoppingList.remove(index - 1); - } - } - } - - else - { - System.out.println("That is not a valid item number."); - System.out.println(); - } - - } - - else if(command.equals("CLEAR")) - { - shoppingList.clear(); - } - - else if(command.equals("FIND")) - { - isInList = false; - - if(shoppingList.size()==0) - { - System.out.println("Your shopping list is empty"); - System.out.println(); - } - - for(int i = 0;i Date: Fri, 4 May 2018 09:45:14 -0500 Subject: [PATCH 87/97] Delete ListItem.java --- ListItem.java | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 ListItem.java diff --git a/ListItem.java b/ListItem.java deleted file mode 100644 index 7819f95..0000000 --- a/ListItem.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.company; - -public class ListItem -{ - private String name; - private int quantity = 1; - - ListItem(String name) - { - this.name = name; - this.quantity = quantity; - } - - public String getName() - { - return name; - } - - public int getQuantity() - { - return quantity; - } - - public void quantityUp() - { - quantity++; - } - - public void quantityDown() - { - quantity--; - } -} From e35bc7d2df0dc304067371819cbc6158d73df14d Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Fri, 4 May 2018 10:14:45 -0500 Subject: [PATCH 88/97] Shopping list --- ShoppingList/.idea/vcs.xml | 6 + ShoppingList/src/company/ListItem.java | 33 ++++ ShoppingList/src/company/ShoppingList.java | 167 +++++++++++++++++++++ 3 files changed, 206 insertions(+) create mode 100644 ShoppingList/.idea/vcs.xml create mode 100644 ShoppingList/src/company/ListItem.java create mode 100644 ShoppingList/src/company/ShoppingList.java diff --git a/ShoppingList/.idea/vcs.xml b/ShoppingList/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/ShoppingList/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ShoppingList/src/company/ListItem.java b/ShoppingList/src/company/ListItem.java new file mode 100644 index 0000000..ac0e39c --- /dev/null +++ b/ShoppingList/src/company/ListItem.java @@ -0,0 +1,33 @@ +package company; + +public class ListItem +{ + private String name; + private int quantity = 1; + + ListItem(String name) + { + this.name = name; + this.quantity = quantity; + } + + public String getName() + { + return name; + } + + public int getQuantity() + { + return quantity; + } + + public void quantityUp() + { + quantity++; + } + + public void quantityDown() + { + quantity--; + } +} diff --git a/ShoppingList/src/company/ShoppingList.java b/ShoppingList/src/company/ShoppingList.java new file mode 100644 index 0000000..411fdba --- /dev/null +++ b/ShoppingList/src/company/ShoppingList.java @@ -0,0 +1,167 @@ +package company; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class ShoppingList +{ + public static void main(String[] args) + { + ShoppingList shoppingList = new ShoppingList(); + + run(); + } + + public static void run() + { + Scanner scanner = new Scanner(System.in); + boolean go = true; + + System.out.println("Welcome to my store. Go buy something."); + + List shoppingList = new ArrayList<>(); + + while(go) + { + System.out.println("Enter one of the following commands:"); + System.out.println(); + System.out.println("Add "); + System.out.println("Print"); + System.out.println("Remove "); + System.out.println("Find"); + System.out.println("Clear"); + System.out.println("Exit"); + + String commandLine = scanner.nextLine(); + String[] commands = commandLine.split(" "); + String command = commands[0].toUpperCase(); + + boolean isInList = false; + int itemIndex=0; + if(command.equals("ADD")) + { + String itemName = commands[1]; + for(int i = 0;i=6) + { + System.out.println("I'm sorry, Dave, I'm afraid I can't do that."); + } + if(!isInList) + { + shoppingList.add(new ListItem(commands[1])); + } + } + + else if(command.equals("PRINT")) + { + if(shoppingList.size()==0) + { + System.out.println("Your shopping list is empty"); + System.out.println(); + } + for(int i=0;i shoppingList.size()) + { + System.out.println("That's not a valid item number. Try again."); + System.out.println(); + } + + else + { + shoppingList.get(index - 1).quantityDown(); + if (shoppingList.get(index - 1).getQuantity() <= 0) + { + shoppingList.remove(index - 1); + } + } + } + + else + { + System.out.println("That is not a valid item number."); + System.out.println(); + } + + } + + else if(command.equals("CLEAR")) + { + shoppingList.clear(); + } + + else if(command.equals("FIND")) + { + isInList = false; + + if(shoppingList.size()==0) + { + System.out.println("Your shopping list is empty"); + System.out.println(); + } + + for(int i = 0;i Date: Fri, 4 May 2018 15:33:40 -0500 Subject: [PATCH 89/97] pet hotel silver --- MapExercises/src/PetHotel.java | 200 +++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 MapExercises/src/PetHotel.java diff --git a/MapExercises/src/PetHotel.java b/MapExercises/src/PetHotel.java new file mode 100644 index 0000000..7f75099 --- /dev/null +++ b/MapExercises/src/PetHotel.java @@ -0,0 +1,200 @@ + +import java.util.*; + +public class PetHotel +{ + public static void main(String[] args) + { + run(); + } + static void run() + { + Map petOccupants = new TreeMap<>(); + + Scanner scanner = new Scanner(System.in); + + System.out.println("Welcome to the pet hotel!"); + System.out.println("Enter one of the following commands: "); + System.out.println(); + System.out.println("CheckIn "); + System.out.println("CheckOut "); + System.out.println("MoveUp"); + System.out.println("MoveDown"); + System.out.println("Occupancy"); + System.out.println("CloseForSeason"); + System.out.println("Exit"); + + boolean go = true; + final int roomNumberMin = 100; + final int roomNumberMax = 109; + + while(go) + { + String commandLine = scanner.nextLine(); + String[] commands = commandLine.split(" "); + String command = commands[0].toUpperCase(); + boolean isOccupied = false; + + + if(command.equals("CHECKIN")) + { + String pet = commands[1]; + if(commands.length!=3) + { + System.out.println("That is an invalid entry."); + } + else + { + int room = Integer.valueOf(commands[2]); + + for (int roomNumber : petOccupants.keySet()) + { + if (roomNumber == room) + { + isOccupied = true; + } + } + + if (room < roomNumberMin || room > roomNumberMax) + { + System.out.println("That is not a valid room number. Try again."); + System.out.println(); + } else if (isOccupied) + { + System.out.println("Sorry, that room is already occupied. Try again."); + } else + { + petOccupants.put(room, pet); + petOccupants.put(room, pet); + System.out.println("Checked " + petOccupants.get(room) + " in to room " + room); + System.out.println(); + } + } + + } + + else if(command.equals("CHECKOUT")) + { + int room = Integer.valueOf(commands[1]); + System.out.println("Checked " + petOccupants.get(room) + " out of " + room); + petOccupants.remove(Integer.valueOf(commands[1])); + } + + else if(command.equals("MOVE")) + { + isOccupied = false; + int currentRoom = Integer.valueOf(commands[1]); + int nextRoom = Integer.valueOf(commands[2]); + + for(int roomNumber : petOccupants.keySet()) + { + if(roomNumber == nextRoom) + { + isOccupied = true; + } + } + + if(currentRoom roomNumberMax || + nextRoom roomNumberMax + ) + { + System.out.println("One of those room numbers is invalid. Try again."); + System.out.println(); + } + + else if(isOccupied) + { + System.out.println("The room you want to move to is occupied. Please pick another room."); + } + + else + { + System.out.println("Moved " + petOccupants.get(currentRoom) + " to room " + nextRoom); + petOccupants.put(nextRoom,petOccupants.get(currentRoom)); + } + } + + else if(command.equals("SWAP")) + { + int firstRoom = Integer.valueOf(commands[1]); + int secondRoom = Integer.valueOf(commands [2]); + int holder = Integer.valueOf(commands[1]); + + System.out.println(petOccupants.get(secondRoom) + " is now in room " + firstRoom + " and " + petOccupants.get(firstRoom) + " is now in room " + secondRoom); + + petOccupants.put(firstRoom, petOccupants.get(secondRoom)); + petOccupants.put(secondRoom, petOccupants.get(holder)); + + } + + else if(command.equals("MOVEUP")) + { + System.out.println("All pets were moved into the next room up."); + Map holder = new TreeMap<>(); + + for(Map.Entry entry : petOccupants.entrySet()) + { + int room = entry.getKey() + 1; + if(room > roomNumberMax) + { + room = roomNumberMin; + } + holder.put(room,entry.getValue()); + } + petOccupants = holder; + } + + else if(command.equals("MOVEDOWN")) + { + System.out.println("All pets were moved into the next room down."); + Map holder = new TreeMap<>(); + + for(Map.Entry entry : petOccupants.entrySet()) + { + int room = entry.getKey() - 1; + if(room < roomNumberMin) + { + room = roomNumberMax; + } + holder.put(room,entry.getValue()); + } + petOccupants = holder; + } + + else if(command.equals("OCCUPANCY")) + { + if(petOccupants.size()==0) + { + System.out.println("There are no pets currently checked in"); + } + else + { + Set printPets = petOccupants.entrySet(); + System.out.println("Pet occupants: " + printPets); + } + } + + else if(command.equals("CLOSEFORSEASON")) + { + petOccupants.clear(); + System.out.println("All pets have been checked out."); + } + + else if(command.equals("EXIT")) + { + go = false; + System.out.println("Thank you for using this system. Goodbye!"); + } + + else + { + System.out.println("That is an invalid command."); + } + + if(go) + System.out.println("Enter another command:"); + } + } +} From 1d2d3bf5c40d9d436ab0157aa6c3b1fe3e219f57 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Fri, 4 May 2018 16:31:55 -0500 Subject: [PATCH 90/97] pet hotel gold --- MapExercises/src/PetHotel.java | 102 +++++++++++++++++++++++++-------- 1 file changed, 78 insertions(+), 24 deletions(-) diff --git a/MapExercises/src/PetHotel.java b/MapExercises/src/PetHotel.java index 7f75099..f28e2f5 100644 --- a/MapExercises/src/PetHotel.java +++ b/MapExercises/src/PetHotel.java @@ -9,22 +9,14 @@ public static void main(String[] args) } static void run() { - Map petOccupants = new TreeMap<>(); + Map petOccupants = new TreeMap<>(); Scanner scanner = new Scanner(System.in); System.out.println("Welcome to the pet hotel!"); System.out.println("Enter one of the following commands: "); System.out.println(); - System.out.println("CheckIn "); - System.out.println("CheckOut "); - System.out.println("MoveUp"); - System.out.println("MoveDown"); - System.out.println("Occupancy"); - System.out.println("CloseForSeason"); - System.out.println("Exit"); + printCommands(); boolean go = true; final int roomNumberMin = 100; @@ -40,7 +32,8 @@ static void run() if(command.equals("CHECKIN")) { - String pet = commands[1]; + String petName = commands[1]; + if(commands.length!=3) { System.out.println("That is an invalid entry."); @@ -61,14 +54,28 @@ static void run() { System.out.println("That is not a valid room number. Try again."); System.out.println(); - } else if (isOccupied) + + } + + else if (isOccupied) { - System.out.println("Sorry, that room is already occupied. Try again."); - } else + if(petOccupants.get(room).size() > 3) + { + System.out.println("Sorry, there are too many pets in this room already. Pick a different room."); + } + else + { + System.out.println("Checked " + petName + " in to room " + room); + petOccupants.get(room).add(petName); + } + } + + else { - petOccupants.put(room, pet); - petOccupants.put(room, pet); - System.out.println("Checked " + petOccupants.get(room) + " in to room " + room); + ArrayList petGroup = new ArrayList<>(); + petOccupants.put(room, petGroup); + petOccupants.get(room).add(petName); + System.out.println("Checked " + petName + " in to room " + room); System.out.println(); } } @@ -77,9 +84,38 @@ static void run() else if(command.equals("CHECKOUT")) { + boolean isInRoom = false; int room = Integer.valueOf(commands[1]); - System.out.println("Checked " + petOccupants.get(room) + " out of " + room); - petOccupants.remove(Integer.valueOf(commands[1])); + + if(commands.length==2) + { + System.out.println("Checked all pets out of room " + room); + petOccupants.remove(Integer.valueOf(commands[1])); + } + + else if(commands.length==3) + { + String petName = commands[2]; + int petIndex = 0; + for(int i = 0;i < petOccupants.get(room).size();i++) + { + if(petOccupants.get(room).get(i).equals(petName)) + { + isInRoom = true; + petIndex = i; + } + } + + if(isInRoom) + { + petOccupants.get(room).remove(petName); + System.out.println(petName + " was checked out from room " + room); + } + else + { + System.out.println("Sorry, that pet is not in that room."); + } + } } else if(command.equals("MOVE")) @@ -132,9 +168,9 @@ else if(command.equals("SWAP")) else if(command.equals("MOVEUP")) { System.out.println("All pets were moved into the next room up."); - Map holder = new TreeMap<>(); + Map holder = new TreeMap<>(); - for(Map.Entry entry : petOccupants.entrySet()) + for(Map.Entry entry : petOccupants.entrySet()) { int room = entry.getKey() + 1; if(room > roomNumberMax) @@ -149,9 +185,9 @@ else if(command.equals("MOVEUP")) else if(command.equals("MOVEDOWN")) { System.out.println("All pets were moved into the next room down."); - Map holder = new TreeMap<>(); + Map holder = new TreeMap<>(); - for(Map.Entry entry : petOccupants.entrySet()) + for(Map.Entry entry : petOccupants.entrySet()) { int room = entry.getKey() - 1; if(room < roomNumberMin) @@ -182,6 +218,11 @@ else if(command.equals("CLOSEFORSEASON")) System.out.println("All pets have been checked out."); } + else if(command.equals("SHOWCOMMANDS")) + { + printCommands(); + } + else if(command.equals("EXIT")) { go = false; @@ -194,7 +235,20 @@ else if(command.equals("EXIT")) } if(go) - System.out.println("Enter another command:"); + System.out.println("Enter another command: (To view commands, enter ShowCommands) "); } } + + public static void printCommands() + { + System.out.println("CheckIn "); + System.out.println("CheckOut "); + System.out.println("MoveUp"); + System.out.println("MoveDown"); + System.out.println("Occupancy"); + System.out.println("CloseForSeason"); + System.out.println("Exit"); + } } From cb203f98016a9fe48fe4598a26e0d9c6c5c83994 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 7 May 2018 13:11:30 -0500 Subject: [PATCH 91/97] simple set --- SimpleSet/src/com/company/SimpleSet.java | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 SimpleSet/src/com/company/SimpleSet.java diff --git a/SimpleSet/src/com/company/SimpleSet.java b/SimpleSet/src/com/company/SimpleSet.java new file mode 100644 index 0000000..2bf1888 --- /dev/null +++ b/SimpleSet/src/com/company/SimpleSet.java @@ -0,0 +1,30 @@ +package com.company; + +import java.util.HashSet; +import java.util.Set; + +public class SimpleSet +{ + public static void main(String[] args) + { + SimpleSet simpleSet = new SimpleSet(); + + simpleSet.run(); + } + + static void run() + { + Set mySet = new HashSet<>(); + + for(int i=1;i<6;i++) + { + mySet.add(i); + } + + System.out.println(mySet); + + mySet.add(3); + + System.out.println(mySet); + } +} From 68216dab7a044a7950eb49d24b9f4996af8f8b26 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Mon, 7 May 2018 14:54:30 -0500 Subject: [PATCH 92/97] bingo numbers silver --- SimpleSet/src/com/company/BingoNumbers.java | 249 ++++++++++++++++++++ 1 file changed, 249 insertions(+) create mode 100644 SimpleSet/src/com/company/BingoNumbers.java diff --git a/SimpleSet/src/com/company/BingoNumbers.java b/SimpleSet/src/com/company/BingoNumbers.java new file mode 100644 index 0000000..c2f4b7a --- /dev/null +++ b/SimpleSet/src/com/company/BingoNumbers.java @@ -0,0 +1,249 @@ +package com.company; + +import java.util.HashSet; +import java.util.Random; +import java.util.Scanner; +import java.util.Set; + +public class BingoNumbers +{ + static Set bingoNumbers = new HashSet<>(); + static Set bingoNumbersString = new HashSet<>(); + static boolean isValidNumber = false; + public static void main(String[] args) + { + BingoNumbers bingoNumbers = new BingoNumbers(); + + bingoNumbers.run(); + } + private static void run() + { + Scanner scanner = new Scanner(System.in); + System.out.println("Let's play some bingo. Enter one of the following commands: "); + printCommands(); + String command = ""; + + do + { + try + { + String entered = scanner.nextLine(); + String[] commandLine; + commandLine = entered.split(" "); + + command = commandLine[0].toUpperCase(); + + isValidNumber = false; + + if (commandLine.length > 1 && Integer.parseInt(commandLine[1]) > 0 && Integer.parseInt(commandLine[1]) <= 75) + { + isValidNumber = true; + } + + if (command.equals("CALL")) + { + if(commandLine.length > 1) + { + int numberEntered = Integer.parseInt(commandLine[1]); + call(numberEntered); + } + else + { + Random random = new Random(); + isValidNumber = true; + int bingoNumber = random.nextInt(75) + 1; + call(bingoNumber); + } + } + + + + else if (command.equals("CALLED")) + { + called(); + } + + else if (command.equals("VERIFY")) + { + int numberEntered = Integer.parseInt(commandLine[1]); + verify(numberEntered); + } + + else if (command.equals("TOGO")) + { + toGo(bingoNumbers); + } + + else if (command.equals("CHALLENGE")) + { + int numberEntered = Integer.parseInt(commandLine[1]); + challenge(numberEntered); + } + + else if (command.equals("BINGO")) + { + bingo(); + } + + else + { + System.out.println("Invalid input. Enter another command."); + } + } + catch(Exception e) + { + System.out.println("Invalid input. Enter another command."); + } + + }while(command != "EXIT"); + + + System.out.println("Thanks for playing Bingo. Goodbye!"); + } + + private static void printCommands() + { + System.out.println("Call <1 - 75>"); + System.out.println("Called"); + System.out.println("Verify <1 - 75>"); + System.out.println("Challenge <1 - 75>"); + System.out.println("ToGo"); + System.out.println("Bingo!"); + System.out.println("Exit"); + } + + private static void call(int bingoNumber) + { + if(bingoNumbers.contains(bingoNumber)) + { + printNumber(bingoNumber); + System.out.println(" has already been called. Try another one."); + } + else if (isValidNumber) + { + bingoNumbers.add(bingoNumber); + bingoNumbersString.add(getBingoLetter(bingoNumber) + String.valueOf(bingoNumber)); + System.out.print("Added "); + printNumber(bingoNumber); + System.out.println(" to the list of Bingo numbers"); + } + + else + { + System.out.println("I can't add that number. Enter a number between 1 and 75."); + } + } + + private static void called() + { + if(bingoNumbers.size() > 0) + { + System.out.println("List of numbers called so far: "); + System.out.println(bingoNumbersString); + } + + else + { + System.out.println("No numbers have been called."); + } + } + + private static void verify(int bingoNumber) + { + if(isValidNumber && bingoNumbers.contains(bingoNumber)) + { + System.out.println(bingoNumber + "has already bee called."); + } + + else if(isValidNumber && !bingoNumbers.contains(bingoNumber)) + { + System.out.println("That number has not been called yet."); + } + else if(!isValidNumber) + { + System.out.println("I can't verify that number. Enter a number between 1 and 75."); + } + } + + private static void challenge(int bingoNumber) + { + if(isValidNumber && bingoNumbers.contains(bingoNumber)) + { + bingoNumbers.remove(bingoNumber); + System.out.println(bingoNumber + " removed from the list of called numbers."); + } + else + { + System.out.println("I can't remove that number. Either it hasn't been called yet, or it is an invalid number."); + } + } + + private static void toGo(Set calledNumbers) + { + if(bingoNumbers.size() >= 75) + { + System.out.println("All numbers have been called. Surely you got a bingo."); + } + else + { + Set allBingoNumbers = new HashSet<>(); + Set allBingoNumbersString = new HashSet<>(); + + for (int i = 1; i <= 75; i++) + { + allBingoNumbers.add(i); + } + + allBingoNumbers.removeAll(calledNumbers); + for (int x : allBingoNumbers) + { + allBingoNumbersString.add(getBingoLetter(x) + x); + } + + System.out.println("Numbers that have not been called yet: " + allBingoNumbersString); + } + } + + private static void bingo() + { + if(bingoNumbers.size() >= 4) + { + System.out.println("Congratulations! You win a fruit cake."); + bingoNumbers.clear(); + } + else + { + System.out.println("Cheater! You haven't even called four numbers. You couldn't possibly have won."); + } + } + + private static void printNumber(int number) + { + if (number > 0 && number <= 15) + System.out.print("B" + number); + if (number >= 16 && number <= 30) + System.out.print("I" + number); + if (number >= 31 && number <= 45) + System.out.print("N" + number); + if (number >= 46 && number <= 60) + System.out.print("G" + number); + if (number >= 61 && number <= 75) + System.out.print("O" + number); + } + + private static String getBingoLetter(int number) + { + if (number > 0 && number <= 15) + return "B"; + if (number >= 16 && number <= 30) + return "I"; + if (number >= 31 && number <= 45) + return "N"; + if (number >= 46 && number <= 60) + return "G"; + if (number >= 61 && number <= 75) + return "O"; + else + return ""; + } +} From a73496541221a21d688d470cc3978d887dbe8a6d Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 8 May 2018 12:27:16 -0500 Subject: [PATCH 93/97] Simple Queue silver --- SimpleQueue/src/com/company/SimpleQueue.java | 39 ++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 SimpleQueue/src/com/company/SimpleQueue.java diff --git a/SimpleQueue/src/com/company/SimpleQueue.java b/SimpleQueue/src/com/company/SimpleQueue.java new file mode 100644 index 0000000..895e12c --- /dev/null +++ b/SimpleQueue/src/com/company/SimpleQueue.java @@ -0,0 +1,39 @@ +package com.company; + +import java.util.LinkedList; +import java.util.Queue; + +public class SimpleQueue +{ + public static void main(String[]args) + { + SimpleQueue simpleQueue = new SimpleQueue(); + + simpleQueue.run(); + } + static void run() + { + Queue myQueue = new LinkedList<>(); + + myQueue.add("One"); + myQueue.add("Two"); + myQueue.add("Three"); + myQueue.add("Four"); + myQueue.add("Five"); + + System.out.println(myQueue); + + System.out.println(myQueue.remove()); + + System.out.println(myQueue); + + System.out.println(myQueue.peek()); + System.out.println(myQueue); + System.out.println(myQueue.peek()); + System.out.println(myQueue); + System.out.println(myQueue.peek()); + System.out.println(myQueue); + System.out.println(myQueue.peek()); + System.out.println(myQueue); + } +} From 5cfa5d413d06dd6d64820ae0cb1a520bfe7402b7 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 8 May 2018 12:27:47 -0500 Subject: [PATCH 94/97] task helper silver --- SimpleQueue/src/com/company/TaskHelper.java | 216 ++++++++++++++++++++ 1 file changed, 216 insertions(+) create mode 100644 SimpleQueue/src/com/company/TaskHelper.java diff --git a/SimpleQueue/src/com/company/TaskHelper.java b/SimpleQueue/src/com/company/TaskHelper.java new file mode 100644 index 0000000..4dbb1e2 --- /dev/null +++ b/SimpleQueue/src/com/company/TaskHelper.java @@ -0,0 +1,216 @@ +package com.company; + +import java.util.LinkedList; +import java.util.Queue; +import java.util.Scanner; + +public class TaskHelper +{ + Queue tasks = new LinkedList<>(); + + public static void main(String[] args) + { + TaskHelper taskHelper = new TaskHelper(); + + taskHelper.run(); + } + public void run() + { + String command = ""; + + System.out.println("Welcome to the task manager. Here are a list of commands. At any time, enter PrintCommmands to view the commands."); + printCommands(); + try + { + do + { + Scanner scanner = new Scanner(System.in); + + String commandLine = scanner.nextLine(); + + String[] commands = commandLine.split(" "); + command = commands[0].toUpperCase(); + + if (command.equals("ADD")) + { + if(commands.length >= 2) + { + addTask(commands); + } + + else + { + System.out.println("I have to know what task to add. Enter some tasks this time."); + } + } + + else if (command.equals("PEEK")) + { + int numTasks = 0; + + if (commands.length == 2) + { + numTasks = Integer.valueOf(commands[1]); + peek(numTasks, tasks); + } + else if(commands.length == 1) + { + peek(numTasks, tasks); + } + + else + { + System.out.println("Invalid input. Enter Peek, then one number."); + } + + } + + else if (command.equals("REMOVE")) + { + int numTasks = 0; + + if (commands.length == 2) + { + numTasks = Integer.valueOf(commands[1]); + remove(numTasks); + } + else if(commands.length == 1) + { + remove(numTasks); + } + + else + { + System.out.println("Invalid input. Enter Remove, then one number."); + } + } + + else if (command.equals("HOWMANY")) + { + howMany(); + } + + else if(command.equals("PRINTCOMMANDS")) + { + printCommands(); + } + + else if (command.equals("FLEE")) + { + flee(); + } + + else if (command.equals("EXIT")) + { + exit(); + } + + else + { + System.out.println("Invalid command."); + } + + if(!command.equals("EXIT")) + System.out.print("Enter another command:"); + + } while (!command.equals("EXIT")); + } + catch(Exception e) + { + System.out.println("Invalid input. Try another command."); + } + } + + public void printCommands() + { + System.out.println("Add ..."); + System.out.println("Peek"); + System.out.println("Remove"); + System.out.println("HowMany"); + System.out.println("Flee"); + } + + public void addTask(String[] taskNames) + { + for(int i = 1;i < taskNames.length;i++) + { + tasks.add(taskNames[i]); + System.out.println("Added " + taskNames[i] + " to the list of tasks."); + } + } + + public void peek(int numTasks, Queue holder) + { + if(tasks.size() == 1 && numTasks == 0) + System.out.println("The next task is " + tasks.peek()); + + else if(tasks.size() == 0) + { + System.out.println("There are no tasks in the list."); + } + + else if(tasks.size() > 1 && numTasks <= tasks.size()) + { + holder = new LinkedList<>(); + + for(String task : tasks) + { + holder.add(task); + } + System.out.println("The next " + numTasks + " tasks are: "); + + for(int i = 0;i < numTasks;i++) + { + System.out.println(holder.poll()); + } + } + + else if(numTasks > tasks.size()) + { + System.out.println("There aren't that many tasks in the list."); + } + else + System.out.println("There are no tasks left in the list."); + } + + public void remove(int numTasks) + { + if(tasks.size() == 1) + { + System.out.println("Removed " + tasks.peek() + " from the list."); + tasks.remove(); + } + else if(tasks.size() > 1 && numTasks <= tasks.size()) + { + System.out.println("Removed the following tasks: "); + + for(int i = 0;i < numTasks;i++) + { + System.out.println(tasks.remove()); + } + } + + else if(numTasks > tasks.size()) + { + System.out.println("There aren't that many tasks in the list."); + } + else + System.out.println("There are no tasks left in the list."); + } + + public void howMany() + { + System.out.println("Tasks left to be completed: " + tasks.size()); + } + + public void flee() + { + tasks.clear(); + System.out.println("All tasks have been cleared from the list."); + } + + public void exit() + { + System.out.println("Thank you for using this task manager. Goodbye."); + } +} From 6e4f1cfd0016870dbdcbdcf2fab47fd901839818 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 8 May 2018 12:40:22 -0500 Subject: [PATCH 95/97] Simple Stack --- SimpleQueue/src/com/company/SimpleStack.java | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 SimpleQueue/src/com/company/SimpleStack.java diff --git a/SimpleQueue/src/com/company/SimpleStack.java b/SimpleQueue/src/com/company/SimpleStack.java new file mode 100644 index 0000000..578cd7e --- /dev/null +++ b/SimpleQueue/src/com/company/SimpleStack.java @@ -0,0 +1,33 @@ +package com.company; +import java.util.Stack; + +public class SimpleStack +{ + public static void main(String[] args) + { + SimpleStack simpleStack = new SimpleStack(); + simpleStack.run(); + } + void run() + { + Stack myStack = new Stack<>(); + + myStack.add("One"); + myStack.add("Two"); + myStack.add("Three"); + myStack.add("Four"); + myStack.add("Five"); + + System.out.println(myStack); + + System.out.println(myStack.pop()); + + System.out.println(myStack); + + System.out.println(myStack.peek()); + System.out.println(myStack.peek()); + System.out.println(myStack.peek()); + System.out.println(myStack.peek()); + + } +} From 564277cfd82b162dd0bef92dec7c88cf81c312c7 Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Tue, 8 May 2018 13:11:06 -0500 Subject: [PATCH 96/97] Emergency Tracker --- .../src/com/company/EmergencyTracker.java | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 SimpleQueue/src/com/company/EmergencyTracker.java diff --git a/SimpleQueue/src/com/company/EmergencyTracker.java b/SimpleQueue/src/com/company/EmergencyTracker.java new file mode 100644 index 0000000..9e9e916 --- /dev/null +++ b/SimpleQueue/src/com/company/EmergencyTracker.java @@ -0,0 +1,128 @@ +package com.company; + +import java.util.Scanner; +import java.util.Stack; + +public class EmergencyTracker +{ + Stack emergencyTasks = new Stack<>(); + Scanner scanner = new Scanner(System.in); + String command = ""; + String emergency = ""; + + public static void main(String[] args) + { + EmergencyTracker emergencyTracker = new EmergencyTracker(); + + emergencyTracker.run(); + } + void run() + { + System.out.println("Welcome to the emergency input system. Enter one of the following commands: "); + printCommands(); + + do + { + try + { + String commandLine = scanner.nextLine(); + String[] commands = commandLine.split(" "); + command = commands[0].toUpperCase(); + + if(commands.length == 2) + emergency = commands[1]; + if(commands.length > 2) + { + System.out.println("Invalid input. Don't enter more than 2 values."); + } + else if(command.equals("ADD")) + { + add(emergency); + } + else if(command.equals("PEEK")) + { + peek(); + } + else if(command.equals("REMOVE")) + { + remove(); + } + else if(command.equals("HOWMANY")) + { + remove(); + } + else if(command.equals("PANIC")) + { + panic(); + } + else if(command.equals("EXIT")) + { + System.out.println("Thank you for using this emergency tracking system."); + } + else + { + System.out.println("Invalid command."); + } + } + catch (Exception e) + { + System.out.println("Invalid input."); + } + + if(!command.equals("EXIT")) + System.out.print("Enter another command: \n"); + + }while(!command.equals("EXIT")); + + System.out.println("Goodbye."); + } + + void printCommands() + { + System.out.println("Add "); + System.out.println("Peek"); + System.out.println("Remove"); + System.out.println("HowMany"); + System.out.println("Panic"); + } + + void add(String emergency) + { + emergencyTasks.add(emergency); + System.out.println(emergency + " add to the list of emergencies"); + } + void peek() + { + if (emergencyTasks.size() > 0) + System.out.println("The next emergency is " + emergencyTasks.peek()); + else + System.out.println("Thankfully, there are no emergencies to view."); + } + void remove() + { + if (emergencyTasks.size() > 0) + { + System.out.println(emergencyTasks.pop() + " has been completed and removed."); + if(emergencyTasks.size() > 0) + System.out.println("The next emergency is " + emergencyTasks.peek()); + else + System.out.println("That was the last emergency."); + } + else + { + System.out.println("Thankfully, there are no emergencies to remove."); + } + } + void howMany() + { + if(emergencyTasks.size() > 0) + System.out.println("There are currently " + emergencyTasks.size()); + else + System.out.println("Thankfully, there are not emergencies right now."); + } + void panic() + { + System.out.println("All emergencies have been cleared. That was a close one."); + emergencyTasks.clear(); + } +} From ea65598116bee0e76a5fbbaf3b75c0bd713e8f5f Mon Sep 17 00:00:00 2001 From: douglasjd1 Date: Wed, 12 Sep 2018 14:06:14 -0500 Subject: [PATCH 97/97] Add ability to add multiple items --- ShoppingList/.idea/misc.xml | 6 ++ ShoppingList/.idea/modules.xml | 8 +++ ShoppingList/ShoppingList.iml | 11 ++++ ShoppingList/src/company/ListItem.java | 10 +++- ShoppingList/src/company/ShoppingList.java | 67 +++++++++++++++++----- 5 files changed, 86 insertions(+), 16 deletions(-) create mode 100644 ShoppingList/.idea/misc.xml create mode 100644 ShoppingList/.idea/modules.xml create mode 100644 ShoppingList/ShoppingList.iml diff --git a/ShoppingList/.idea/misc.xml b/ShoppingList/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/ShoppingList/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ShoppingList/.idea/modules.xml b/ShoppingList/.idea/modules.xml new file mode 100644 index 0000000..bd26b87 --- /dev/null +++ b/ShoppingList/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/ShoppingList/ShoppingList.iml b/ShoppingList/ShoppingList.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/ShoppingList/ShoppingList.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/ShoppingList/src/company/ListItem.java b/ShoppingList/src/company/ListItem.java index ac0e39c..23d97c2 100644 --- a/ShoppingList/src/company/ListItem.java +++ b/ShoppingList/src/company/ListItem.java @@ -5,7 +5,7 @@ public class ListItem private String name; private int quantity = 1; - ListItem(String name) + ListItem(String name, int quantity) { this.name = name; this.quantity = quantity; @@ -30,4 +30,12 @@ public void quantityDown() { quantity--; } + + public void addQuantity( int numToAdd) + { + quantity += numToAdd; + } + + public void subQuanitity(int numToSub){ quantity -= numToSub; } + } diff --git a/ShoppingList/src/company/ShoppingList.java b/ShoppingList/src/company/ShoppingList.java index 411fdba..fa8baff 100644 --- a/ShoppingList/src/company/ShoppingList.java +++ b/ShoppingList/src/company/ShoppingList.java @@ -13,7 +13,7 @@ public static void main(String[] args) run(); } - public static void run() + private static void run() { Scanner scanner = new Scanner(System.in); boolean go = true; @@ -26,9 +26,9 @@ public static void run() { System.out.println("Enter one of the following commands:"); System.out.println(); - System.out.println("Add "); + System.out.println("Add "); System.out.println("Print"); - System.out.println("Remove "); + System.out.println("Remove "); System.out.println("Find"); System.out.println("Clear"); System.out.println("Exit"); @@ -37,10 +37,28 @@ public static void run() String[] commands = commandLine.split(" "); String command = commands[0].toUpperCase(); + int quantity = 0; + + if(commands.length == 3) + { + try + { + quantity = Integer.parseInt(commands[2]); + } + catch(Exception e) + { + System.out.println(commands[2] + " is not a valid number. Try again."); + } + } + boolean isInList = false; - int itemIndex=0; + int itemIndex = 0; if(command.equals("ADD")) { + if(commands.length > 3) + { + System.out.println("Please enter only the item name and quantity."); + } String itemName = commands[1]; for(int i = 0;i=6) + if(isInList) { - System.out.println("I'm sorry, Dave, I'm afraid I can't do that."); + if(commands.length == 2) + { + shoppingList.get(itemIndex).quantityUp(); + } + if(commands.length == 3) + { + shoppingList.get(itemIndex).addQuantity(quantity); + } } + if(!isInList) { - shoppingList.add(new ListItem(commands[1])); + if(commands.length == 3) + { + shoppingList.add(new ListItem(commands[1], quantity)); + } + + else + { + shoppingList.add(new ListItem(commands[1], quantity)); + } } } @@ -84,13 +114,22 @@ else if(command.equals("REMOVE")) for(int i = 1;i shoppingList.size()) { @@ -123,8 +162,6 @@ else if(command.equals("CLEAR")) else if(command.equals("FIND")) { - isInList = false; - if(shoppingList.size()==0) { System.out.println("Your shopping list is empty");