From df013a6faee1c9f8dfc4e7733275cfbb697318df Mon Sep 17 00:00:00 2001 From: cdsto Date: Thu, 18 Jan 2018 13:25:25 -0600 Subject: [PATCH 01/70] Because the teacher said to. --- ch01/Hello.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ch01/Hello.java b/ch01/Hello.java index 593557b..56eee74 100644 --- a/ch01/Hello.java +++ b/ch01/Hello.java @@ -1,6 +1,8 @@ -public class Hello { +public class Hello +{ - public static void main(String[] args) { + public static void main(String[] args) + { // generate some simple output System.out.println("Hello, World!"); } From 2e9e4b06bbb9cbc11ec5c7ad0ff93eb82377e750 Mon Sep 17 00:00:00 2001 From: cdsto Date: Thu, 18 Jan 2018 14:29:36 -0600 Subject: [PATCH 02/70] Exercise 2.3 --- ch02/Date.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 ch02/Date.java diff --git a/ch02/Date.java b/ch02/Date.java new file mode 100644 index 0000000..442a743 --- /dev/null +++ b/ch02/Date.java @@ -0,0 +1,28 @@ +public class Date +{ + public static void main(String args[]) + { + String day = "Thursday"; + int date = 18; + String month = "January"; + int year = 2018; + + System.out.print(day); + System.out.print(", "); + System.out.print(month); + System.out.print(" "); + System.out.print(date); + System.out.print(", "); + System.out.println(year); + + System.out.print(day); + System.out.print(" "); + System.out.print(date); + System.out.print(" "); + System.out.print(month); + System.out.print(" "); + System.out.println(year); + + } + +} From bfd7c65c74a139aaed9c3276b0aa8a32a634950b Mon Sep 17 00:00:00 2001 From: cdsto Date: Thu, 18 Jan 2018 15:05:19 -0600 Subject: [PATCH 03/70] Exercise 2.3 --- ch02/Date.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ch02/Date.java b/ch02/Date.java index 442a743..1ec0ce2 100644 --- a/ch02/Date.java +++ b/ch02/Date.java @@ -7,6 +7,8 @@ public static void main(String args[]) String month = "January"; int year = 2018; + System.out.println("American format:"); + System.out.print(day); System.out.print(", "); System.out.print(month); @@ -15,6 +17,8 @@ public static void main(String args[]) System.out.print(", "); System.out.println(year); + System.out.println("European format:"); + System.out.print(day); System.out.print(" "); System.out.print(date); From 261792b03e06d6f23b9c6b96a72263bd0b008693 Mon Sep 17 00:00:00 2001 From: cdsto Date: Thu, 18 Jan 2018 15:08:07 -0600 Subject: [PATCH 04/70] Exercise 2.2 --- ch02/Date.java | 1 + 1 file changed, 1 insertion(+) diff --git a/ch02/Date.java b/ch02/Date.java index 1ec0ce2..333c4b5 100644 --- a/ch02/Date.java +++ b/ch02/Date.java @@ -7,6 +7,7 @@ public static void main(String args[]) String month = "January"; int year = 2018; + System.out.println("American format:"); System.out.print(day); From 84d4195164e8e048c510839c58661cdb03364ef8 Mon Sep 17 00:00:00 2001 From: cdsto Date: Thu, 18 Jan 2018 15:08:32 -0600 Subject: [PATCH 05/70] Exercise 2.2 --- ch02/Date.java | 1 - 1 file changed, 1 deletion(-) diff --git a/ch02/Date.java b/ch02/Date.java index 333c4b5..1ec0ce2 100644 --- a/ch02/Date.java +++ b/ch02/Date.java @@ -7,7 +7,6 @@ public static void main(String args[]) String month = "January"; int year = 2018; - System.out.println("American format:"); System.out.print(day); From 2a61af996acffa3372974c38bc6bca8e906a40ba Mon Sep 17 00:00:00 2001 From: cdsto Date: Thu, 18 Jan 2018 15:38:25 -0600 Subject: [PATCH 06/70] Exercise 2.3 --- ch02/Time.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 ch02/Time.java diff --git a/ch02/Time.java b/ch02/Time.java new file mode 100644 index 0000000..3094f14 --- /dev/null +++ b/ch02/Time.java @@ -0,0 +1,28 @@ +public class Time +{ + public static void main(String args[]) + { + int hour = 14; + int minute = 34; + int second = 30; + + System.out.print("Number of seconds since midnight: "); + System.out.println((hour * 60 * 60) + (minute * 60) + second); + + System.out.print("Number of seconds remaining in the day: "); + System.out.println((24 * 60 * 60) - ((hour * 60 * 60) + (minute * 60) + second)); + + System.out.print("Percent of the day that has passed: "); + System.out.println(((hour * 60 * 60) + (minute * 60) + second) * 100 / (24 * 60 * 60)); + + int hour2 = 14; + int minute2 = 48; + int second2 = 17; + + System.out.print("Time elapsed since beginning this exercise: "); + System.out.print(((hour2 * 60 * 60) + (minute2 * 60) + second2) - ((hour * 60 * 60) + (minute * 60) + second)); + System.out.println(" seconds."); + + } + +} From 2c4462c4aa2b82e4009b2559e46ab863387b9a55 Mon Sep 17 00:00:00 2001 From: cdsto Date: Thu, 18 Jan 2018 15:45:08 -0600 Subject: [PATCH 07/70] Exercise 2-4 --- ch02/IntExtremes.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 ch02/IntExtremes.java diff --git a/ch02/IntExtremes.java b/ch02/IntExtremes.java new file mode 100644 index 0000000..530f8f3 --- /dev/null +++ b/ch02/IntExtremes.java @@ -0,0 +1,22 @@ +public class IntExtremes +{ + public static void main(String args[]) + { + int postiveInt = 2147483647; + + System.out.println(postiveInt); + + postiveInt = 2147483647 + 1; + + System.out.println(postiveInt); + + int negativeInt = -2147483648; + + System.out.println(negativeInt); + + negativeInt = -2147483648 -1; + + System.out.println(negativeInt); + + } +} From b75f64acac7ea67bc8c2030dddca14ad83c8717c Mon Sep 17 00:00:00 2001 From: cdsto Date: Thu, 18 Jan 2018 15:51:20 -0600 Subject: [PATCH 08/70] Exercise 2-5 --- 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..db4b750 --- /dev/null +++ b/ch02/IntByZero.java @@ -0,0 +1,11 @@ +public class IntByZero +{ + public static void main(String[] args) + { + int a = 42; + int b = 0; + int result = a / b; + + System.out.println(result); + } +} From 6992cd13597e2c98575f47071ab8c4b752d7bc58 Mon Sep 17 00:00:00 2001 From: cdsto Date: Thu, 18 Jan 2018 15:58:18 -0600 Subject: [PATCH 09/70] Exercise 2-5 --- ch02/DoubleByZero.java | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 ch02/DoubleByZero.java diff --git a/ch02/DoubleByZero.java b/ch02/DoubleByZero.java new file mode 100644 index 0000000..57d8d7c --- /dev/null +++ b/ch02/DoubleByZero.java @@ -0,0 +1,11 @@ +public class DoubleByZero +{ + public static void main(String[] args) + { + double a = 42.0; + double b = 0.0; + double result = a / b; + + System.out.println(result); + } +} From 42be09db84f0517adedd2825c667d2994022fb7a Mon Sep 17 00:00:00 2001 From: cdsto Date: Thu, 18 Jan 2018 20:45:26 -0600 Subject: [PATCH 10/70] Exercise 2-7 --- ch02/Withdrawal.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 ch02/Withdrawal.java diff --git a/ch02/Withdrawal.java b/ch02/Withdrawal.java new file mode 100644 index 0000000..b86c7c5 --- /dev/null +++ b/ch02/Withdrawal.java @@ -0,0 +1,28 @@ +public class Withdrawal +{ + public static void main(String[] args) + { + int withhdrawal = 137; + + int twenty = withhdrawal / 20; + int remainder1 = withhdrawal % 20; + + System.out.println(twenty + " $20 bills"); + + int ten = remainder1 / 10; + int remainder2 = remainder1 % 10; + + System.out.println(ten + " $10 bills"); + + int five = remainder2 / 5; + int remainder3 = remainder2 % 5; + + System.out.println(five + " $5 bills"); + + int one = remainder3; + + System.out.println(one + " $1 bills"); + + + } +} From 28e8c08588c63f6260ae30de9e832618e1c9639c Mon Sep 17 00:00:00 2001 From: cdsto Date: Fri, 19 Jan 2018 10:41:10 -0600 Subject: [PATCH 11/70] Exercise 3.2 --- ch03/ConvertTemp.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 ch03/ConvertTemp.java diff --git a/ch03/ConvertTemp.java b/ch03/ConvertTemp.java new file mode 100644 index 0000000..80d1beb --- /dev/null +++ b/ch03/ConvertTemp.java @@ -0,0 +1,22 @@ +import java.util.Scanner; + +public class ConvertTemp +{ + public static void main(String[] args) { + double degC; + double degF; + + Scanner in = new Scanner(System.in); + + // prompt the user and get the value + System.out.print("Exactly how many degrees Celsius? "); + degC = in.nextDouble(); + + final double DEGC_TO_DEGF = (degC * 9 / 5) + 32; + + // convert and output the result + degF = DEGC_TO_DEGF; + System.out.printf("%.1f degrees Celsius = %.1f degrees Fahrrenheit.\n", + degC, degF); + } +} From 44ec283ec8b44cfda1bfd560ce7d99e9b9a467c3 Mon Sep 17 00:00:00 2001 From: cdsto Date: Fri, 19 Jan 2018 11:30:03 -0600 Subject: [PATCH 12/70] Exercise 3.3 --- ch03/ConvertTime.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 ch03/ConvertTime.java diff --git a/ch03/ConvertTime.java b/ch03/ConvertTime.java new file mode 100644 index 0000000..cb8c7c9 --- /dev/null +++ b/ch03/ConvertTime.java @@ -0,0 +1,27 @@ +import java.util.Scanner; + +public class ConvertTime +{ + public static void main(String[] args) { + int secondsIn; + + Scanner in = new Scanner(System.in); + + // prompt the user and get the value + System.out.print("Exactly how many seconds? "); + secondsIn = in.nextInt(); + + final int SECONDS_PER_HOUR = 60 * 60; + + int hours = secondsIn / SECONDS_PER_HOUR; + + final int SECONDS_PER_MINUTE = 60; + + int minutes = (secondsIn - (hours * SECONDS_PER_HOUR)) / SECONDS_PER_MINUTE; + + int seconds = (secondsIn - (hours * SECONDS_PER_HOUR)) % SECONDS_PER_MINUTE; + + System.out.printf(secondsIn + " seconds = %d hours, %d minutes, and %d seconds.\n", + hours, minutes, seconds); + } +} From e4f3b05f8e089d6d2552508f9cd44510f8995dde Mon Sep 17 00:00:00 2001 From: cdsto Date: Fri, 19 Jan 2018 11:49:43 -0600 Subject: [PATCH 13/70] Exercise 3.4 --- ch03/GuessNumber.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 ch03/GuessNumber.java diff --git a/ch03/GuessNumber.java b/ch03/GuessNumber.java new file mode 100644 index 0000000..14edc04 --- /dev/null +++ b/ch03/GuessNumber.java @@ -0,0 +1,27 @@ +import java.util.Scanner; +import java.util.Random; + +public class GuessNumber +{ + public static void main(String[] args) + { + int userGuess; + + Scanner in = new Scanner (System.in); + + //prompt user to guess a number + System.out.print("Guess a number between 1 and 100: "); + userGuess = in.nextInt(); + + // pick a random number + Random random = new Random(); + int number = random.nextInt(100) + 1; + + int difference = Math.abs(number - userGuess); + + System.out.println("Your guess was " + userGuess + "."); + System.out.println("The number I was thinking of was " + number + "."); + System.out.println("The difference between your guess and my number is " + difference + "."); + } + +} From 95a48dd6f7296eaade2b9a59b04331093fe972f5 Mon Sep 17 00:00:00 2001 From: cdsto Date: Fri, 19 Jan 2018 14:09:46 -0600 Subject: [PATCH 14/70] Exercise 3.4 --- ch03/ConvertTemp.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch03/ConvertTemp.java b/ch03/ConvertTemp.java index 80d1beb..75122ae 100644 --- a/ch03/ConvertTemp.java +++ b/ch03/ConvertTemp.java @@ -16,7 +16,7 @@ public static void main(String[] args) { // convert and output the result degF = DEGC_TO_DEGF; - System.out.printf("%.1f degrees Celsius = %.1f degrees Fahrrenheit.\n", + System.out.printf("%.1f degrees Celsius = %.1f degrees Fahrenheit.\n", degC, degF); } } From 4fb3fcfb54392f472d2a896b5b86b29ab712acc2 Mon Sep 17 00:00:00 2001 From: cdsto Date: Fri, 19 Jan 2018 15:48:06 -0600 Subject: [PATCH 15/70] 4-2 a & b --- ch04/SimpleMethhods.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 ch04/SimpleMethhods.java diff --git a/ch04/SimpleMethhods.java b/ch04/SimpleMethhods.java new file mode 100644 index 0000000..3f39a14 --- /dev/null +++ b/ch04/SimpleMethhods.java @@ -0,0 +1,21 @@ +public class SimpleMethhods +{ + public static void main(String[] args) + { + printCount(5); + + printSum(4, 6); + printSum(7, 2); + } + + public static void printCount(int count) + { + System.out.println("This count is: " + count); + } + + public static void printSum(int x, int y) + { + int z = x + y; + System.out.println(x + " + " + y + " = " + z); + } +} From c978d1fceeb7d645fcd80d00bf6cee505de3e934 Mon Sep 17 00:00:00 2001 From: cdsto Date: Fri, 19 Jan 2018 16:14:20 -0600 Subject: [PATCH 16/70] 4-3 --- ch04/ExerciseFourPointThree.java | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 ch04/ExerciseFourPointThree.java diff --git a/ch04/ExerciseFourPointThree.java b/ch04/ExerciseFourPointThree.java new file mode 100644 index 0000000..42c33e9 --- /dev/null +++ b/ch04/ExerciseFourPointThree.java @@ -0,0 +1,42 @@ +public class ExerciseFourPointThree +{ + public static void main(String args[]) + { + String day = "Thursday"; + int date = 18; + String month = "January"; + int year = 2018; + + printAmerican(day, date, month, year); + + printEuropean(day, date, month, year); + + } + + public static void printAmerican(String day, int date, String month, int year) + { + System.out.println("American format:"); + + System.out.print(day); + System.out.print(", "); + System.out.print(month); + System.out.print(" "); + System.out.print(date); + System.out.print(", "); + System.out.println(year); + + } + + public static void printEuropean(String day, int date, String month, int year) + { + System.out.println("European format:"); + + System.out.print(day); + System.out.print(" "); + System.out.print(date); + System.out.print(" "); + System.out.print(month); + System.out.print(" "); + System.out.println(year); + } +} From 3e709cc6f9e9798f9515ca58a833ea6c5d73269e Mon Sep 17 00:00:00 2001 From: cdsto Date: Mon, 22 Jan 2018 10:58:15 -0600 Subject: [PATCH 17/70] Exercise 5-A --- ch05/LogicMethods.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 ch05/LogicMethods.java diff --git a/ch05/LogicMethods.java b/ch05/LogicMethods.java new file mode 100644 index 0000000..cb0cec6 --- /dev/null +++ b/ch05/LogicMethods.java @@ -0,0 +1,18 @@ +public class LogicMethods +{ + public static void main(String[] args) + { + + } + + private static void printIsLarge(int number) + { + if (number > 99) + { + System.out.println("The number is large"); + + System.out.println(); + } + } + +} From 48aaf73778be82d31b0285bf9348a6eacb845684 Mon Sep 17 00:00:00 2001 From: cdsto Date: Mon, 22 Jan 2018 11:03:58 -0600 Subject: [PATCH 18/70] Exercise 5-B --- ch05/LogicMethods.java | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/ch05/LogicMethods.java b/ch05/LogicMethods.java index cb0cec6..e028356 100644 --- a/ch05/LogicMethods.java +++ b/ch05/LogicMethods.java @@ -2,10 +2,24 @@ public class LogicMethods { public static void main(String[] args) { - + printIsLargeOrSmall(5); + printIsLargeOrSmall(100); + printIsLargeOrSmall(50); } - private static void printIsLarge(int number) + //5-A + /*private static void printIsLarge(int number) + { + if (number > 99) + { + System.out.println("The number is large"); + + System.out.println(); + } + }*/ + + //5-B + private static void printIsLargeOrSmall(int number) { if (number > 99) { @@ -13,6 +27,13 @@ private static void printIsLarge(int number) System.out.println(); } + + if (number < 10) + { + System.out.println("The number is small"); + + System.out.println(); + } } } From 6284b5c1707abe3b922aca123b48b04cb9f16361 Mon Sep 17 00:00:00 2001 From: cdsto Date: Mon, 22 Jan 2018 11:16:01 -0600 Subject: [PATCH 19/70] Exercise 5-C --- ch05/LogicMethods.java | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/ch05/LogicMethods.java b/ch05/LogicMethods.java index e028356..8490cbf 100644 --- a/ch05/LogicMethods.java +++ b/ch05/LogicMethods.java @@ -2,9 +2,9 @@ public class LogicMethods { public static void main(String[] args) { - printIsLargeOrSmall(5); - printIsLargeOrSmall(100); - printIsLargeOrSmall(50); + printLargest(5,6); + printLargest(6,5); + printLargest(6,6); } //5-A @@ -19,7 +19,7 @@ public static void main(String[] args) }*/ //5-B - private static void printIsLargeOrSmall(int number) + /*private static void printIsLargeOrSmall(int number) { if (number > 99) { @@ -34,6 +34,32 @@ private static void printIsLargeOrSmall(int number) System.out.println(); } + }*/ + + //5-C + private static void printLargest( int number1, int number2) + { + + if (number1 > number2) + { + System.out.println("The largest number is: " + number1); + + System.out.println(); + } + + else if (number2 > number1) { + + System.out.println("The largest number is: " + number2); + + System.out.println(); + } + + else if (number1 == number2) { + + System.out.println("The numbers are equal"); + System.out.println(); + + } } } From 901eea42fafde2eec44c6f9f6e6bd4c525aba6a7 Mon Sep 17 00:00:00 2001 From: cdsto Date: Mon, 22 Jan 2018 11:45:17 -0600 Subject: [PATCH 20/70] Exercise 5-D --- ch05/LogicMethods.java | 58 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/ch05/LogicMethods.java b/ch05/LogicMethods.java index 8490cbf..5b064fc 100644 --- a/ch05/LogicMethods.java +++ b/ch05/LogicMethods.java @@ -2,9 +2,12 @@ public class LogicMethods { public static void main(String[] args) { - printLargest(5,6); - printLargest(6,5); - printLargest(6,6); + printLargestOdd(5,7); + printLargestOdd(7,5); + printLargestOdd(5,8); + printLargestOdd(8,5); + printLargestOdd(8,8); + printLargestOdd(5,5); } //5-A @@ -37,7 +40,7 @@ public static void main(String[] args) }*/ //5-C - private static void printLargest( int number1, int number2) + /*private static void printLargest(int number1, int number2) { if (number1 > number2) @@ -60,6 +63,53 @@ else if (number1 == number2) { System.out.println(); } + } */ + + private static void printLargestOdd(int number1, int number2) + { + int remainder1 = (number1 % 2); + int remainder2 = (number2 % 2); + + if (remainder1 == 1 && remainder2 == 1) + { + if (number1 > number2) + { + System.out.println("The largest odd number is: " + number1); + + System.out.println(); + } + + else if (number2 > number1) { + + System.out.println("The largest odd number is: " + number2); + + System.out.println(); + } + + else if (number1 == number2) { + + System.out.println("Two odds make an even"); + System.out.println(number1 + " + " + number2 + " = " + (number1 + number2)); + + } + } + + else if (remainder1 == 1 && remainder2 == 0) + { + System.out.println("The largest odd number is: " + number1); + System.out.println(); + } + + else if (remainder1 == 0 && remainder2 == 1) + { + System.out.println("The largest odd number is: " + number2); + System.out.println(); + } + + else { + System.out.println("Neither number is odd"); + System.out.println(); + } } } From 5de4f31bad6c397d8939b69278e6f5e37eb1a1e3 Mon Sep 17 00:00:00 2001 From: cdsto Date: Mon, 22 Jan 2018 12:51:44 -0600 Subject: [PATCH 21/70] Exercise 5-4 --- ch05/LogicMethods.java | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/ch05/LogicMethods.java b/ch05/LogicMethods.java index 5b064fc..47cb9a8 100644 --- a/ch05/LogicMethods.java +++ b/ch05/LogicMethods.java @@ -2,12 +2,31 @@ public class LogicMethods { public static void main(String[] args) { - printLargestOdd(5,7); - printLargestOdd(7,5); - printLargestOdd(5,8); - printLargestOdd(8,5); - printLargestOdd(8,8); - printLargestOdd(5,5); + checkFermat(3, 4, 5, 3); + checkFermat(3, 4, 5, 2); + } + + //5-4 + private static void checkFermat(int a, int b, int c, int n) + { + + if (n > 2) + { + if (Math.pow(a, n) + Math.pow(b, n) == Math.pow(c, n)) + { + System.out.println("Holy smokes, Fermat was wrong!"); + System.out.println(); + } else + { + + System.out.println("No, that doesn't work."); + System.out.println(); + } + } else + { + System.out.println("Holy smokes, Fermat was right!"); + System.out.println(); + } } //5-A @@ -65,7 +84,8 @@ else if (number1 == number2) { } } */ - private static void printLargestOdd(int number1, int number2) + //5-D + /* private static void printLargestOdd(int number1, int number2) { int remainder1 = (number1 % 2); int remainder2 = (number2 % 2); @@ -110,6 +130,6 @@ else if (remainder1 == 0 && remainder2 == 1) System.out.println("Neither number is odd"); System.out.println(); } - } + } */ } From a770e0c28bfcef5b29cc3b0d06527a5dd6dc5f48 Mon Sep 17 00:00:00 2001 From: cdsto Date: Mon, 22 Jan 2018 16:29:37 -0600 Subject: [PATCH 22/70] Exercise 5-E --- ch05/LogicMethods.java | 92 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 84 insertions(+), 8 deletions(-) diff --git a/ch05/LogicMethods.java b/ch05/LogicMethods.java index 47cb9a8..8b9fe43 100644 --- a/ch05/LogicMethods.java +++ b/ch05/LogicMethods.java @@ -1,12 +1,88 @@ +import java.util.Scanner; + public class LogicMethods { public static void main(String[] args) { - checkFermat(3, 4, 5, 3); - checkFermat(3, 4, 5, 2); + Scanner in = new Scanner(System.in); + + System.out.println("What size cheese would you like? "); + int diameter = in.nextInt(); + System.out.println(); + + if (diameter < 0 || diameter > 3) + { + System.err.println("Your order is too crazy! Please try again."); + return; + } + + System.out.println("How many yards of cheese would you like? "); + int yards = in.nextInt(); + System.out.println(); + + if (yards < 0 || yards > 1000000) + { + System.err.println("Your order is too crazy! Please try again."); + return; + } + + crazyCheesePrice(diameter,yards); + } + + private static void crazyCheesePrice(int diameter, int yards) + { + int costPerYard = 0; + + final int PRICE_ONE_INCH = 2; + final int PRICE_TWO_INCH = 4; + final int PRICE_THREE_INCH = 6; + + if (diameter == 1) + { + costPerYard = (PRICE_ONE_INCH * yards); + } else if (diameter == 2) + { + costPerYard = (PRICE_TWO_INCH * yards); + } else if (diameter == 3) + { + costPerYard = (PRICE_THREE_INCH * yards); + } + + + int shipping = 0; + + final int SHIPPING_ONE_OR_TWO = 2; + final int SHIPPING_THREE = 4; + + final int FREE_SHIPPING_ONE_INCH = 50; + final int FREE_SHIPPING_TWO_INCHES = 75; + final int FREE_SHIPPING_THREE_INCHES = 25; + + + if ((diameter == 1 && yards <= FREE_SHIPPING_ONE_INCH) || (diameter == 2 && yards <= FREE_SHIPPING_TWO_INCHES)) + + { + shipping = SHIPPING_ONE_OR_TWO * yards; + + } else if (diameter == 3 && yards <= FREE_SHIPPING_THREE_INCHES) + { + shipping = SHIPPING_THREE * yards; + } + + + final int HANDLING = 5; + int totalPrice = costPerYard + shipping + HANDLING; + + System.out.println("The total cost for your order is $" + totalPrice); + System.out.println(); + } - //5-4 + + + + + /* //5-4 private static void checkFermat(int a, int b, int c, int n) { @@ -27,9 +103,9 @@ private static void checkFermat(int a, int b, int c, int n) System.out.println("Holy smokes, Fermat was right!"); System.out.println(); } - } + } */ - //5-A +//5-A /*private static void printIsLarge(int number) { if (number > 99) @@ -40,7 +116,7 @@ private static void checkFermat(int a, int b, int c, int n) } }*/ - //5-B +//5-B /*private static void printIsLargeOrSmall(int number) { if (number > 99) @@ -58,7 +134,7 @@ private static void checkFermat(int a, int b, int c, int n) } }*/ - //5-C +//5-C /*private static void printLargest(int number1, int number2) { @@ -84,7 +160,7 @@ else if (number1 == number2) { } } */ - //5-D +//5-D /* private static void printLargestOdd(int number1, int number2) { int remainder1 = (number1 % 2); From 700e366ed64f6193d7d40b27caddbd85b4a30cb7 Mon Sep 17 00:00:00 2001 From: cdsto Date: Mon, 22 Jan 2018 20:27:35 -0600 Subject: [PATCH 23/70] Exercise 5-F --- ch05/TicketNumber.java | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 ch05/TicketNumber.java diff --git a/ch05/TicketNumber.java b/ch05/TicketNumber.java new file mode 100644 index 0000000..508ccbf --- /dev/null +++ b/ch05/TicketNumber.java @@ -0,0 +1,32 @@ +import java.util.Scanner; + +public class TicketNumber +{ + public static void main(String[] args) + { + int ticketNumber; + + Scanner in = new Scanner(System.in); + + System.out.println("What is your ticket number? "); + ticketNumber = in.nextInt(); + System.out.println(); + + int lastDigit = ticketNumber % 10; + int ticketPrefix = ticketNumber / 10; + + final int TICKET_TEST = 7; + boolean result = ((ticketPrefix % TICKET_TEST) == lastDigit); + + + if (result) + { + System.out.println("Good Number"); + } else + { + System.out.println("Bad Number"); + } + + + } +} From 9a564e61b7e98280222853d39240831f2df346bb Mon Sep 17 00:00:00 2001 From: cdsto Date: Tue, 23 Jan 2018 09:30:07 -0600 Subject: [PATCH 24/70] Exercise 6.2 --- ch06/ValueMethods.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 ch06/ValueMethods.java diff --git a/ch06/ValueMethods.java b/ch06/ValueMethods.java new file mode 100644 index 0000000..a269df0 --- /dev/null +++ b/ch06/ValueMethods.java @@ -0,0 +1,22 @@ +public class ValueMethods +{ + public static void main(String[] args) + { + System.out.println("Boolean test"); + System.out.println(isDivisible(4,2)); + System.out.println(isDivisible(5,2)); + + } + + //Exercise 6.2 + private static boolean isDivisible(int n, int m) + { + if (n % m == 0) + { + return true; + } else + { + return false; + } + } +} From f59fbfae9bd4f0210bbc7bf0e4e0bf200a881d66 Mon Sep 17 00:00:00 2001 From: cdsto Date: Tue, 23 Jan 2018 09:33:46 -0600 Subject: [PATCH 25/70] Exercise 6.2 --- ch06/ValueMethods.java | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/ch06/ValueMethods.java b/ch06/ValueMethods.java index a269df0..efd08db 100644 --- a/ch06/ValueMethods.java +++ b/ch06/ValueMethods.java @@ -2,21 +2,14 @@ public class ValueMethods { public static void main(String[] args) { - System.out.println("Boolean test"); - System.out.println(isDivisible(4,2)); - System.out.println(isDivisible(5,2)); + System.out.println(isDivisible(4, 2)); + System.out.println(isDivisible(5, 2)); } //Exercise 6.2 private static boolean isDivisible(int n, int m) { - if (n % m == 0) - { - return true; - } else - { - return false; - } + return (n % m == 0); } } From d3175d18faa8da73255c06ff65354c9ca4d17cc3 Mon Sep 17 00:00:00 2001 From: cdsto Date: Tue, 23 Jan 2018 09:48:59 -0600 Subject: [PATCH 26/70] Exercise 6.3 --- ch06/ValueMethods.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/ch06/ValueMethods.java b/ch06/ValueMethods.java index efd08db..dc77f09 100644 --- a/ch06/ValueMethods.java +++ b/ch06/ValueMethods.java @@ -2,14 +2,24 @@ public class ValueMethods { public static void main(String[] args) { - System.out.println(isDivisible(4, 2)); - System.out.println(isDivisible(5, 2)); + System.out.println(isTriangle(3,4,5)); + System.out.println(isTriangle(1,1,12)); + } + //Exercise 6.3 + private static boolean isTriangle (int a, int b, int c) + { + return (a <= b + c && b <= a + c && c <= a + b); } + + + + + //Exercise 6.2 - private static boolean isDivisible(int n, int m) + /* private static boolean isDivisible(int n, int m) { return (n % m == 0); - } + } */ } From f87b43ec724accf3568aa9ad8d878a3dd377a260 Mon Sep 17 00:00:00 2001 From: cdsto Date: Tue, 23 Jan 2018 10:17:13 -0600 Subject: [PATCH 27/70] Exercise 6.4: parts 1, 2, and 3 --- ch06/Multadd.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 ch06/Multadd.java diff --git a/ch06/Multadd.java b/ch06/Multadd.java new file mode 100644 index 0000000..72ce8de --- /dev/null +++ b/ch06/Multadd.java @@ -0,0 +1,15 @@ +public class Multadd +{ + public static void main(String[] args) + { + System.out.println(multadd(1.0, 2.0, 3.0)); + System.out.println(multadd(12.0, 20.0, 4.0)); + System.out.println(multadd(-11.0, 242.0, -30.0)); + } + + //Exercise 6.4: 1, 2, and 3 + private static double multadd(double a, double b, double c) + { + return (a * b + c); + } +} From 5ea0a3404a28f91bc9e27d09c181cffe8e338076 Mon Sep 17 00:00:00 2001 From: cdsto Date: Wed, 24 Jan 2018 14:02:43 -0600 Subject: [PATCH 28/70] Exercise 7-A --- ch07/MyLoops.java | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 ch07/MyLoops.java diff --git a/ch07/MyLoops.java b/ch07/MyLoops.java new file mode 100644 index 0000000..c6af2a1 --- /dev/null +++ b/ch07/MyLoops.java @@ -0,0 +1,68 @@ +public class MyLoops +{ + public static void main(String[] args) + { + exercise7aForLoop(); + exercise7aForLoopBackwards(); + + exercise7aWhile(); + exercise7aWhileBackwards(); + + exercise7aDoWhile(); + exercise7aDoWhileBackwards(); + } + + private static void exercise7aDoWhileBackwards() + { + int x = 10; + do{ + System.out.println(x); + x--; + } while (x >= 1); + } + + private static void exercise7aDoWhile() + { + int x = 1; + do{ + System.out.println(x); + x++; + } while (x <= 10); + } + + private static void exercise7aWhileBackwards() + { + int x = 10; + while (x >= 1) + { + System.out.println(x); + x--; + } + } + + private static void exercise7aWhile() + { + int x = 1; + while (x <= 10) + { + System.out.println(x); + x++; + } + } + + private static void exercise7aForLoopBackwards() + { + for (int x = 10; x >= 1; x--) + { + System.out.println(x); + } + } + + private static void exercise7aForLoop() + { + for (int x = 1; x <= 10; x++) + { + System.out.println(x); + } + } +} From 9779d807ae6637db779b52782641c4a68c127c3f Mon Sep 17 00:00:00 2001 From: cdsto Date: Wed, 24 Jan 2018 14:07:53 -0600 Subject: [PATCH 29/70] Exercise 7-B --- ch07/MyLoopsB.java | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 ch07/MyLoopsB.java diff --git a/ch07/MyLoopsB.java b/ch07/MyLoopsB.java new file mode 100644 index 0000000..2b81622 --- /dev/null +++ b/ch07/MyLoopsB.java @@ -0,0 +1,38 @@ +public class MyLoopsB +{ + public static void main(String[] args) + { + exercise7bForLoop(); + + exercise7bWhile(); + + exercise7bDoWhile(); + } + + private static void exercise7bForLoop() + { + for (int x = 0; x <= 100; x += 10) + { + System.out.println(x); + } + } + + private static void exercise7bWhile() + { + int x = 0; + while (x <= 100) + { + System.out.println(x); + x += 10; + } + } + + private static void exercise7bDoWhile() + { + int x = 0; + do{ + System.out.println(x); + x += 10; + } while (x <= 100); + } +} From 71eb2cb0ff27d5ce08645aff3e799a4ae3e1bc60 Mon Sep 17 00:00:00 2001 From: cdsto Date: Wed, 24 Jan 2018 14:14:54 -0600 Subject: [PATCH 30/70] Exercise 7-B --- ch07/MyLoopsC.java | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 ch07/MyLoopsC.java diff --git a/ch07/MyLoopsC.java b/ch07/MyLoopsC.java new file mode 100644 index 0000000..3433c06 --- /dev/null +++ b/ch07/MyLoopsC.java @@ -0,0 +1,38 @@ +public class MyLoopsC +{ + public static void main(String[] args) + { + exercise7cForLoop(); + + exercise7cWhile(); + + exercise7cDoWhile(); + } + + private static void exercise7cForLoop() + { + for (int x = 100; x >= -100; x -= 8) + { + System.out.println(x); + } + } + + private static void exercise7cWhile() + { + int x = 100; + while (x >= -100) + { + System.out.println(x); + x -= 8; + } + } + + private static void exercise7cDoWhile() + { + int x = 100; + do{ + System.out.println(x); + x -= 8; + } while (x >= -100); + } +} From e5940dc466482522a4d7814c69c26a3512bf314c Mon Sep 17 00:00:00 2001 From: cdsto Date: Wed, 24 Jan 2018 14:15:35 -0600 Subject: [PATCH 31/70] Exercise 7-C --- ch07/MyLoopsC.java | 1 + 1 file changed, 1 insertion(+) diff --git a/ch07/MyLoopsC.java b/ch07/MyLoopsC.java index 3433c06..271cb16 100644 --- a/ch07/MyLoopsC.java +++ b/ch07/MyLoopsC.java @@ -17,6 +17,7 @@ private static void exercise7cForLoop() } } + private static void exercise7cWhile() { int x = 100; From 9d809f75c82f16c844af5495f133f14cdcd38384 Mon Sep 17 00:00:00 2001 From: cdsto Date: Wed, 24 Jan 2018 14:19:56 -0600 Subject: [PATCH 32/70] Exercise 7-D --- ch07/MyLoopsD.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 ch07/MyLoopsD.java diff --git a/ch07/MyLoopsD.java b/ch07/MyLoopsD.java new file mode 100644 index 0000000..2d6dd02 --- /dev/null +++ b/ch07/MyLoopsD.java @@ -0,0 +1,16 @@ +public class MyLoopsD +{ + public static void main(String[] args) + { + exercise7d(10); + } + + private static void exercise7d(int x) + { + int y = 1; + while (y <= x){ + System.out.println(y); + y++; + } + } +} From d6260add53760fd62ca7bfdfed3266cf05f0e63b Mon Sep 17 00:00:00 2001 From: cdsto Date: Wed, 24 Jan 2018 14:38:06 -0600 Subject: [PATCH 33/70] Exercise 7-E --- ch07/MyLoopsE.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 ch07/MyLoopsE.java diff --git a/ch07/MyLoopsE.java b/ch07/MyLoopsE.java new file mode 100644 index 0000000..845a8af --- /dev/null +++ b/ch07/MyLoopsE.java @@ -0,0 +1,28 @@ +import java.util.Scanner; + +public class MyLoopsE +{ + public static void main(String[] args) + { + exercise7E(); + } + + private static void exercise7E() + { + Scanner in = new Scanner(System.in); + + int x = 1; + + while (x != 0) + { + System.out.println("Please enter a number: "); + x= in.nextInt(); + + if (x != 0) + { + System.out.println("Oops! You did not enter \"0\"!"); + System.out.println(); + } + } + } +} From ab8a09b5e126ccd0169a84584aee4ed322f8653b Mon Sep 17 00:00:00 2001 From: cdsto Date: Wed, 24 Jan 2018 15:07:22 -0600 Subject: [PATCH 34/70] Exercise 7-F --- ch07/MyLoopsF.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 ch07/MyLoopsF.java diff --git a/ch07/MyLoopsF.java b/ch07/MyLoopsF.java new file mode 100644 index 0000000..e234cf0 --- /dev/null +++ b/ch07/MyLoopsF.java @@ -0,0 +1,29 @@ +import java.util.Scanner; + +public class MyLoopsF +{ + public static void main(String[] args) + { + exercise7F(); + } + + private static void exercise7F() + { + Scanner in = new Scanner(System.in); + + int x; + int y = 0; + + while (y <= 1000) + { + System.out.println("Please enter a number: "); + x = in.nextInt(); + y = y + x; + System.out.println(); + + } + + System.out.println(); + System.out.println(y); + } +} From 8b77ac167603f8fb11fad2b357bfd76ff90f013f Mon Sep 17 00:00:00 2001 From: cdsto Date: Wed, 24 Jan 2018 22:52:44 -0600 Subject: [PATCH 35/70] Finish Line Game --- Games/src/FinishLineGame.java | 113 ++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 Games/src/FinishLineGame.java diff --git a/Games/src/FinishLineGame.java b/Games/src/FinishLineGame.java new file mode 100644 index 0000000..dad5608 --- /dev/null +++ b/Games/src/FinishLineGame.java @@ -0,0 +1,113 @@ +import java.util.Random; + +public class FinishLineGame +{ + public static void main(String[] args) + { + final int GAME_START = 1; + int place1 = GAME_START; + int place2 = GAME_START; + + System.out.println("Let the game begin!"); + System.out.println(); + + //The game ends when either player reaches 10 + final int GAME_OVER = 10; + + //Turn for both players + while (place1 < GAME_OVER && place2 < GAME_OVER) + { + //Player 1 rolls + int next1 = place1 + 1; + + int player1Die1 = die1(); + int player1Die2 = die2(); + int rollTotal1 = player1Die1 + player1Die2; + + System.out.println("Player 1 rolled a " + player1Die1 + " and " + player1Die2 + " for a total of " + + rollTotal1 + " ."); + + if (player1Die1 == next1 || player1Die2 == next1 || rollTotal1 == next1) + { + System.out.println("Player 1 is moving up!"); + + place1++; + } + + //Player 2 rolls + int next2 = place2 + 1; + + int player2Die1 = die1(); + int player2Die2 = die2(); + int rollTotal2 = player2Die1 + player2Die2; + + System.out.println("Player 2 rolled a " + player2Die1 + " and " + player2Die2 + " for a total of " + + rollTotal2 + " ."); + + if (player2Die1 == next2 || player2Die2 == next2 || rollTotal2 == next2) + { + System.out.println("Player 2 is moving up!"); + + place2++; + } + + } + + int player1FinalScore = place1; + int player2FinalScore = place2; + + System.out.println(); + + if (winner1(player1FinalScore,player2FinalScore)) + { + System.out.println("Player 1 is the winner!"); + } + else if (winner2(player1FinalScore,player2FinalScore)) + { + System.out.println("Player 2 is the winner"); + } + else if (tieGame(player1FinalScore, player2FinalScore)) + { + System.out.println("Tie game!"); + } + else { + System.out.println("Oops, something went wrong in the game winning announcement if / else if ."); + } + + } + + private static int die1() + { + final int MAX_NUMBER = 6; + + Random random = new Random(); + int randomNumber = random.nextInt(MAX_NUMBER) + 1; + + return randomNumber; + } + + private static int die2() + { + final int MAX_NUMBER = 6; + + Random random = new Random(); + int randomNumber = random.nextInt(MAX_NUMBER) + 1; + + return randomNumber; + } + + private static boolean winner1(int player1FinalScore, int player2FinalScore) + { + return player1FinalScore > player2FinalScore; + } + + private static boolean winner2(int player1FinalScore, int player2FinalScore) + { + return player2FinalScore > player1FinalScore; + } + + private static boolean tieGame(int player1FinalScore, int player2FinalScore) + { + return player1FinalScore == player2FinalScore; + } +} From dd7233a9cbce82969a49f5771ed57fce221865b2 Mon Sep 17 00:00:00 2001 From: cdsto Date: Fri, 26 Jan 2018 11:06:57 -0600 Subject: [PATCH 36/70] Exercise 8-A --- ch08/Ch8Exercises.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 ch08/Ch8Exercises.java diff --git a/ch08/Ch8Exercises.java b/ch08/Ch8Exercises.java new file mode 100644 index 0000000..743540b --- /dev/null +++ b/ch08/Ch8Exercises.java @@ -0,0 +1,17 @@ +public class Ch8Exercises +{ + public static void main (String [] args) + { + int testArray[] = {1,2,3,4,5}; + printArray(testArray); + } + + //Exercise 8-A + private static void printArray(int[] y) + { + for (int x : y) + { + System.out.println(x); + } + } +} From a4430a12b2eb4e901ededd6124c4ca0215b049b2 Mon Sep 17 00:00:00 2001 From: cdsto Date: Fri, 26 Jan 2018 11:13:26 -0600 Subject: [PATCH 37/70] Exercise 8-A --- ch08/Ch8Exercises.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ch08/Ch8Exercises.java b/ch08/Ch8Exercises.java index 743540b..9b605a1 100644 --- a/ch08/Ch8Exercises.java +++ b/ch08/Ch8Exercises.java @@ -1,3 +1,5 @@ +import java.util.Arrays; + public class Ch8Exercises { public static void main (String [] args) @@ -6,12 +8,11 @@ public static void main (String [] args) printArray(testArray); } + //Exercise 8-A private static void printArray(int[] y) { - for (int x : y) - { - System.out.println(x); - } + System.out.println(Arrays.toString(y)); + } } From dd2be63da54b9adccfcfce7ac5cf76b4dfcfcadb Mon Sep 17 00:00:00 2001 From: cdsto Date: Fri, 26 Jan 2018 11:41:31 -0600 Subject: [PATCH 38/70] Exercise 8-B --- ch08/Ch8Exercises.java | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/ch08/Ch8Exercises.java b/ch08/Ch8Exercises.java index 9b605a1..5ca458c 100644 --- a/ch08/Ch8Exercises.java +++ b/ch08/Ch8Exercises.java @@ -2,17 +2,32 @@ public class Ch8Exercises { - public static void main (String [] args) + public static void main(String[] args) { - int testArray[] = {1,2,3,4,5}; - printArray(testArray); + int testArray[] = {1, 2, 3, 4, 5}; + arrayTotal(testArray); + } + + + //Exercise 8-B + private static void arrayTotal(int[] a) + { + int total = 0; + + for (int b : a) + { + total += b; + } + + System.out.println("The sum of all of the values in the array is: " + total); + } //Exercise 8-A - private static void printArray(int[] y) + /* private static void printArray(int[] y) { System.out.println(Arrays.toString(y)); - } + } */ } From 97962c71496de9f51998438f3836211700d6e57b Mon Sep 17 00:00:00 2001 From: cdsto Date: Fri, 26 Jan 2018 12:34:52 -0600 Subject: [PATCH 39/70] Exercise 8-C --- ch08/Ch8Exercises.java | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/ch08/Ch8Exercises.java b/ch08/Ch8Exercises.java index 5ca458c..e35b87d 100644 --- a/ch08/Ch8Exercises.java +++ b/ch08/Ch8Exercises.java @@ -4,13 +4,29 @@ public class Ch8Exercises { public static void main(String[] args) { - int testArray[] = {1, 2, 3, 4, 5}; - arrayTotal(testArray); + int testArray[] = {5,4,3,55,2,1}; + arrayMax(testArray); + } + + //Exercise 8-C + private static void arrayMax(int[] a) + { + int max = 0; + int max2 = 0; + + for (int b : a) + { + max = Math.max(a[0], b); + max2 = Math.max(max2, max); + + } + + System.out.println(max2); } //Exercise 8-B - private static void arrayTotal(int[] a) + /* private static void arrayTotal(int[] a) { int total = 0; @@ -21,7 +37,7 @@ private static void arrayTotal(int[] a) System.out.println("The sum of all of the values in the array is: " + total); - } + } */ //Exercise 8-A From 57d3876bbb4d3c76a718f08c30f2b4bdba3eab87 Mon Sep 17 00:00:00 2001 From: cdsto Date: Fri, 26 Jan 2018 14:43:53 -0600 Subject: [PATCH 40/70] Exercise 8-F --- ch08/Exercise8F.java | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 ch08/Exercise8F.java diff --git a/ch08/Exercise8F.java b/ch08/Exercise8F.java new file mode 100644 index 0000000..e61b594 --- /dev/null +++ b/ch08/Exercise8F.java @@ -0,0 +1,34 @@ +import java.util.Arrays; +import java.util.Scanner; + +public class Exercise8F +{ + public static void main(String[] args) + { + petNames(); + } + + private static void petNames() + { + Scanner in = new Scanner(System.in); + + System.out.println("How many pets do you have? "); + int numberOfPets = in.nextInt(); + in.nextLine(); + + String[] petNames = new String [numberOfPets]; + + int currentPet = 0; + + while (currentPet < numberOfPets) + { + System.out.println("What is the name of pet number " + (currentPet + 1) + "? "); + petNames [currentPet] = in.nextLine(); + currentPet++; + } + + System.out.println("The names of your pets are" + Arrays.toString(petNames) + "."); + + } + +} From e7d4f712c51fd5467550d0ac1f59b86cc452f3fd Mon Sep 17 00:00:00 2001 From: cdsto Date: Fri, 26 Jan 2018 14:51:54 -0600 Subject: [PATCH 41/70] Exercise 8-F --- ch08/Exercise8F.java | 1 + 1 file changed, 1 insertion(+) diff --git a/ch08/Exercise8F.java b/ch08/Exercise8F.java index e61b594..29d77fc 100644 --- a/ch08/Exercise8F.java +++ b/ch08/Exercise8F.java @@ -31,4 +31,5 @@ private static void petNames() } + } From a82bf97236cff52ec9b5fd8207d4cbdfea8ddeb8 Mon Sep 17 00:00:00 2001 From: cdsto Date: Fri, 26 Jan 2018 14:52:57 -0600 Subject: [PATCH 42/70] Exercise 8-A --- ch08/Exercise8A.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 ch08/Exercise8A.java diff --git a/ch08/Exercise8A.java b/ch08/Exercise8A.java new file mode 100644 index 0000000..01e79fb --- /dev/null +++ b/ch08/Exercise8A.java @@ -0,0 +1,17 @@ +import java.util.Arrays; + +public class Exercise8A +{ + public static void main(String[] args) + { + int testArray[] = {1,2,3,55,4,5,6}; + + printArray(testArray); + } + + private static void printArray(int[] y) + { + System.out.println(Arrays.toString(y)); + + } +} From eece237e28d0fdcebb2bf4e945894659a160d4ea Mon Sep 17 00:00:00 2001 From: cdsto Date: Fri, 26 Jan 2018 14:55:50 -0600 Subject: [PATCH 43/70] Exercise 8-A --- ch08/Exercise8A.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch08/Exercise8A.java b/ch08/Exercise8A.java index 01e79fb..2564e4f 100644 --- a/ch08/Exercise8A.java +++ b/ch08/Exercise8A.java @@ -11,7 +11,7 @@ public static void main(String[] args) private static void printArray(int[] y) { - System.out.println(Arrays.toString(y)); + System.out.println("The values in this array are: " + Arrays.toString(y)); } } From 6d25e2dc2e5e3a19cd98e77a78a552af9bcc4f7f Mon Sep 17 00:00:00 2001 From: cdsto Date: Fri, 26 Jan 2018 14:58:19 -0600 Subject: [PATCH 44/70] Exercise 8-B --- ch08/Exercise8B.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 ch08/Exercise8B.java diff --git a/ch08/Exercise8B.java b/ch08/Exercise8B.java new file mode 100644 index 0000000..20fff47 --- /dev/null +++ b/ch08/Exercise8B.java @@ -0,0 +1,22 @@ +public class Exercise8B +{ + public static void main(String[] args) + { + int testArray[] = {1,2,3,4,5}; + + arrayTotal(testArray); + } + + private static void arrayTotal(int[] a) + { + int total = 0; + + for (int b : a) + { + total += b; + } + + System.out.println("The sum of all of the values in the array is: " + total); + + } +} From 23a3bf42fe1a1bc7baaf27fb5aed404651609b3b Mon Sep 17 00:00:00 2001 From: cdsto Date: Fri, 26 Jan 2018 15:00:07 -0600 Subject: [PATCH 45/70] Exercise 8-C --- ch08/Exercise8C.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 ch08/Exercise8C.java diff --git a/ch08/Exercise8C.java b/ch08/Exercise8C.java new file mode 100644 index 0000000..1d43d34 --- /dev/null +++ b/ch08/Exercise8C.java @@ -0,0 +1,24 @@ +public class Exercise8C +{ + public static void main(String[] args) + { + int testArray[] = {1,2,3,55,600,4,5}; + + arrayMax(testArray); + } + + private static void arrayMax(int[] a) + { + int max; + int max2 = 0; + + for (int b : a) + { + max = Math.max(a[0], b); + max2 = Math.max(max2, max); + + } + + System.out.println("The largest value in the array is: " + max2); + } +} From 974bf8239d816933d9fac54bfa2209cd72362968 Mon Sep 17 00:00:00 2001 From: cdsto Date: Fri, 26 Jan 2018 15:01:53 -0600 Subject: [PATCH 46/70] Exercise 8-D --- ch08/Exercise8D.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 ch08/Exercise8D.java diff --git a/ch08/Exercise8D.java b/ch08/Exercise8D.java new file mode 100644 index 0000000..b1e9f2a --- /dev/null +++ b/ch08/Exercise8D.java @@ -0,0 +1,24 @@ +public class Exercise8D +{ + public static void main(String[] args) + { + int testArray[] = {1,2,3,60,4,5}; + + arrayMaxIndex(testArray); + } + + private static void arrayMaxIndex(int[] values) + { + int maxValueIndex = 0; + + for(int i = 0; i < values.length; i++) + { + if (values[i] > values[maxValueIndex]) + { + maxValueIndex = i; + } + } + + System.out.println("The index in this array with the highest value is index: " + maxValueIndex); + } +} From 9533c2ae708c49cdb91cb97f5ea8040386d09187 Mon Sep 17 00:00:00 2001 From: cdsto Date: Fri, 26 Jan 2018 15:02:53 -0600 Subject: [PATCH 47/70] Exercise 8-E --- ch08/Exercise8E.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 ch08/Exercise8E.java diff --git a/ch08/Exercise8E.java b/ch08/Exercise8E.java new file mode 100644 index 0000000..c05b0dc --- /dev/null +++ b/ch08/Exercise8E.java @@ -0,0 +1,23 @@ +public class Exercise8E +{ + public static void main(String[] args) + { + double testArray[] = {3.0, 5.0, 1.0, 9.0, 7.0}; + + arrayAverage(testArray); + } + + private static void arrayAverage(double[] values) + { + double total = 0.0; + + for (double value : values) + { + total += value; + } + + double avg = total / values.length; + + System.out.println("The average of all of the values in the array is: " + avg); + } +} From 15ab61d698f31ec5963c33e75be5c6d0780e2c3f Mon Sep 17 00:00:00 2001 From: cdsto Date: Mon, 29 Jan 2018 14:35:51 -0600 Subject: [PATCH 48/70] Exercise 9-A --- ch09/Exercise9A.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 ch09/Exercise9A.java diff --git a/ch09/Exercise9A.java b/ch09/Exercise9A.java new file mode 100644 index 0000000..0d7ecff --- /dev/null +++ b/ch09/Exercise9A.java @@ -0,0 +1,16 @@ +public class Exercise9A +{ + public static void main(String[] args) + { + printFirstCharacter("Hello"); + printFirstCharacter("Goodbye"); + + } + + private static void printFirstCharacter(String text) + { + System.out.println("Print the first character in " + text); + + System.out.println(text.charAt(0)); + } +} From b3fdcca75faad2de2b5b5b17a097ca038c5d4ce7 Mon Sep 17 00:00:00 2001 From: cdsto Date: Mon, 29 Jan 2018 15:08:51 -0600 Subject: [PATCH 49/70] Exercise 8-G --- ch08/Exercise8G.java | 61 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 ch08/Exercise8G.java diff --git a/ch08/Exercise8G.java b/ch08/Exercise8G.java new file mode 100644 index 0000000..73facef --- /dev/null +++ b/ch08/Exercise8G.java @@ -0,0 +1,61 @@ +import java.util.Scanner; + +import java.util.Arrays; + +public class Exercise8G +{ + public static void main(String[] args) + { + final int MENU_SIZE = 5; + + int[] menuChoices = new int[MENU_SIZE]; + + int customerChoice; + + Scanner in = new Scanner(System.in); + + while (menuChoices[0] < 99 && menuChoices[1] < 99 && menuChoices[2] < 99 && menuChoices[3] < 99 && menuChoices[4] < 99) + { + + do + { + System.out.println("Would you like menu item 0, 1, 2, 3, or 4? "); + customerChoice = in.nextInt(); + in.nextLine(); + + if (customerChoice < 0 || customerChoice > 4) + { + System.out.println("Sorry, that is not one of the items that we offer. Please make a different selection."); + } + } while (customerChoice < 0 || customerChoice > 4); + + menuChoices[customerChoice]++; + System.out.println(Arrays.toString(menuChoices)); + + + } + + + System.out.println("Gooodbye!"); + + boolean employeeScreen = false; + + + System.out.println("Are you an IVM employee? Please answer yes or no. "); + String employeeTest = in.nextLine(); + + if (employeeTest.equals("yes")); + { + employeeScreen = true; + } + + if (employeeScreen) + { + System.out.println(Arrays.toString(menuChoices)); + } + else + System.out.println("Sorry, this information is only accessible by IVM employees."); + } + + +} \ No newline at end of file From f7f2ec991ba6294af28a90795cafa08db8293cc3 Mon Sep 17 00:00:00 2001 From: cdsto Date: Mon, 29 Jan 2018 15:24:26 -0600 Subject: [PATCH 50/70] Exercise 9-B --- ch09/Exercise9B.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 ch09/Exercise9B.java diff --git a/ch09/Exercise9B.java b/ch09/Exercise9B.java new file mode 100644 index 0000000..c21750a --- /dev/null +++ b/ch09/Exercise9B.java @@ -0,0 +1,16 @@ +public class Exercise9B +{ + public static void main(String[] args) + { + printLastCharacter("Hello"); + printLastCharacter("Goodbye"); + + } + + private static void printLastCharacter(String text) + { + System.out.println("Print the last character in " + text); + + System.out.println(text.charAt(text.length() - 1)); + } +} From 43871f96aa075401505168fb42440772f7f6672a Mon Sep 17 00:00:00 2001 From: cdsto Date: Mon, 29 Jan 2018 16:13:20 -0600 Subject: [PATCH 51/70] Exercise 9-C --- ch09/Exercise9C.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 ch09/Exercise9C.java diff --git a/ch09/Exercise9C.java b/ch09/Exercise9C.java new file mode 100644 index 0000000..7ca0080 --- /dev/null +++ b/ch09/Exercise9C.java @@ -0,0 +1,17 @@ +public class Exercise9C +{ + public static void main(String[] args) + { + printCharacters("Hello"); + } + + private static void printCharacters(String text) + { + for (int i = 0; i < text.length(); i++) + { + char letter = text.charAt(i); + + System.out.println("" + letter + i); + } + } +} From 504d75ec00d482d23add0a284c8fa5d1060bd4dc Mon Sep 17 00:00:00 2001 From: cdsto Date: Mon, 29 Jan 2018 16:16:56 -0600 Subject: [PATCH 52/70] Exercise 9-D --- ch09/Exercise9D.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 ch09/Exercise9D.java diff --git a/ch09/Exercise9D.java b/ch09/Exercise9D.java new file mode 100644 index 0000000..38325f8 --- /dev/null +++ b/ch09/Exercise9D.java @@ -0,0 +1,13 @@ +public class Exercise9D +{ + public static void main(String[] args) + { + printAllButFirstThree("Hello"); + printAllButFirstThree("Goodbye"); + } + + private static void printAllButFirstThree(String text) + { + System.out.println(text.substring(3)); + } +} From 4611e4e10e7123f8f07d8f1945b9c0edd94b1af3 Mon Sep 17 00:00:00 2001 From: cdsto Date: Mon, 29 Jan 2018 16:20:26 -0600 Subject: [PATCH 53/70] Exercise 9-E --- ch09/Exercise9E.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 ch09/Exercise9E.java diff --git a/ch09/Exercise9E.java b/ch09/Exercise9E.java new file mode 100644 index 0000000..1a19500 --- /dev/null +++ b/ch09/Exercise9E.java @@ -0,0 +1,14 @@ +public class Exercise9E +{ + public static void main(String[] args) + { + printFirstThree("Hello"); + printFirstThree("Goodbye"); + + } + + private static void printFirstThree(String text) + { + System.out.println(text.substring(0, 3)); + } +} From 0fd2bba3964447aedb7471e1a4f5382173613ca8 Mon Sep 17 00:00:00 2001 From: cdsto Date: Mon, 29 Jan 2018 16:39:43 -0600 Subject: [PATCH 54/70] Exercise 9-F --- ch09/Exercise9F.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 ch09/Exercise9F.java diff --git a/ch09/Exercise9F.java b/ch09/Exercise9F.java new file mode 100644 index 0000000..ed187a0 --- /dev/null +++ b/ch09/Exercise9F.java @@ -0,0 +1,14 @@ +public class Exercise9F +{ + public static void main(String[] args) + { + printPhoneNumber("501-555-0100"); + } + + private static void printPhoneNumber (String digits) + { + System.out.println("Area Code: " + digits.substring(0,3)); + System.out.println("Exchange: " + digits.substring(4,7)); + System.out.println("Line Number: " + digits.substring(8,12)); + } +} From 2fa82d22b756f15f4d437fb82485a16b785a5b83 Mon Sep 17 00:00:00 2001 From: cdsto Date: Mon, 29 Jan 2018 16:46:19 -0600 Subject: [PATCH 55/70] Exercise 9-G --- ch09/Exercise9G.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 ch09/Exercise9G.java diff --git a/ch09/Exercise9G.java b/ch09/Exercise9G.java new file mode 100644 index 0000000..827c240 --- /dev/null +++ b/ch09/Exercise9G.java @@ -0,0 +1,13 @@ +public class Exercise9G +{ + public static void main(String[] args) + { + System.out.println(findFirstE("Hello")); + System.out.println(findFirstE("Goodbye")); + } + + private static int findFirstE(String text) + { + return text.indexOf('e'); + } +} From be225a157935bc637609dfc6314fcfdfeff542f8 Mon Sep 17 00:00:00 2001 From: cdsto Date: Mon, 29 Jan 2018 16:58:44 -0600 Subject: [PATCH 56/70] Exercise 9-H --- ch09/Exercise9H.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 ch09/Exercise9H.java diff --git a/ch09/Exercise9H.java b/ch09/Exercise9H.java new file mode 100644 index 0000000..ad45695 --- /dev/null +++ b/ch09/Exercise9H.java @@ -0,0 +1,16 @@ +public class Exercise9H +{ + public static void main(String[] args) + { + System.out.println(isFinn("Finn")); + System.out.println(isFinn("Jake")); + + } + + private static boolean isFinn(String adventure) + { + String theHuman = "Finn"; + + return (adventure.equals(theHuman)); + } +} From fc746bc24a7bb38a36e666a4ae2ed5a3bc8cd419 Mon Sep 17 00:00:00 2001 From: cdsto Date: Tue, 30 Jan 2018 09:33:56 -0600 Subject: [PATCH 57/70] Exercise 9-F --- ch09/Exercise9F.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ch09/Exercise9F.java b/ch09/Exercise9F.java index ed187a0..2cb12d2 100644 --- a/ch09/Exercise9F.java +++ b/ch09/Exercise9F.java @@ -7,8 +7,14 @@ public static void main(String[] args) private static void printPhoneNumber (String digits) { - System.out.println("Area Code: " + digits.substring(0,3)); - System.out.println("Exchange: " + digits.substring(4,7)); - System.out.println("Line Number: " + digits.substring(8,12)); + //Expected format: ###-###-#### + + String areaCode = digits.substring(0,3); + String exchange = digits.substring(4,7); + String lineNumber = digits.substring(8,12); + + System.out.println("Area Code: " + areaCode); + System.out.println("Exchange: " + exchange); + System.out.println("Line Number: " + lineNumber); } } From 8bf6f907bac647c3995528de267299292b5041cd Mon Sep 17 00:00:00 2001 From: cdsto Date: Wed, 31 Jan 2018 14:53:07 -0600 Subject: [PATCH 58/70] Exercise 11-A --- ch11/Date.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 ch11/Date.java diff --git a/ch11/Date.java b/ch11/Date.java new file mode 100644 index 0000000..7a1587e --- /dev/null +++ b/ch11/Date.java @@ -0,0 +1,14 @@ +public class Date +{ + private int day; + private int month; + private int year; + + public Date(int a, int b, int c) + { + day = a; + month = b; + year = c; + } + +} From b1bd565cd08bf4e3c186979f10557f00fedef571 Mon Sep 17 00:00:00 2001 From: cdsto Date: Wed, 31 Jan 2018 15:07:19 -0600 Subject: [PATCH 59/70] Exercise 11-B --- ch11/Player.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 ch11/Player.java diff --git a/ch11/Player.java b/ch11/Player.java new file mode 100644 index 0000000..948a33f --- /dev/null +++ b/ch11/Player.java @@ -0,0 +1,24 @@ +public class Player +{ + private String name; + private int score; + + public Player(String name) + { + score = 0; + + this.name = name; + } + + public void increaseScore() + { + score++; + } + + public void resetScore() + { + score = 0; + } + + +} From 27f13e3c5f10346120e8ead0ea6a5e39c6bc86f4 Mon Sep 17 00:00:00 2001 From: cdsto Date: Wed, 31 Jan 2018 15:24:01 -0600 Subject: [PATCH 60/70] Exercise 11-C --- ch11/Product.java | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 ch11/Product.java diff --git a/ch11/Product.java b/ch11/Product.java new file mode 100644 index 0000000..6368f44 --- /dev/null +++ b/ch11/Product.java @@ -0,0 +1,41 @@ +public class Product +{ + + private String name; + private int quantityInStock; + + public Product(String name) + { + this.name = name; + } + + public String getName() + { + return this.name; + } + + public int getQuantityInStock() + { + return this.quantityInStock; + } + + public void stock(int newStock) + { + quantityInStock += newStock; + } + + public void sell(int soldStock) + { + quantityInStock -= soldStock; + } + + public void recordLoss(int stockLost) + { + quantityInStock -= stockLost; + } + + public String toString() + { + return "" + name + ", Quantity in Stock " + quantityInStock; + } +} From 39fddd86ee37cbd8cbbbb7ad4b706919e5e1a1d3 Mon Sep 17 00:00:00 2001 From: cdsto Date: Wed, 31 Jan 2018 15:59:14 -0600 Subject: [PATCH 61/70] Exercise 11-D --- ch11/Rectangle.java | 62 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 ch11/Rectangle.java diff --git a/ch11/Rectangle.java b/ch11/Rectangle.java new file mode 100644 index 0000000..9f2fcc8 --- /dev/null +++ b/ch11/Rectangle.java @@ -0,0 +1,62 @@ +import java.util.Objects; + +public class Rectangle +{ + private int height; + private int width; + + public Rectangle() + { + height = 1; + width = 1; + } + + public Rectangle(int height,int width) + { + this.height = height; + this.width = width; + } + + public int getHeight() + { + return height; + } + + public int getWidth() + { + return width; + } + + public Rectangle(Rectangle rectangle) + { + this.height = rectangle.getHeight(); + this.width = rectangle.getWidth(); + } + + public int getArea() + { + return height * width; + } + + public String toString() + { + return "Height: " + height + ", Width: " + width + ", Area: " + getArea(); + } + + @Override + public boolean equals(Object o) + { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Rectangle rectangle = (Rectangle) o; + return height == rectangle.height && + width == rectangle.width; + } + + @Override + public int hashCode() + { + + return Objects.hash(height, width); + } +} From 4a03385f3f8fcf6968a97ef6374ff9aa190aab4b Mon Sep 17 00:00:00 2001 From: cdsto Date: Mon, 5 Feb 2018 13:38:13 -0600 Subject: [PATCH 62/70] Simple List Creation --- ArrayListFun/src/com/company/SimpleList.java | 38 ++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 ArrayListFun/src/com/company/SimpleList.java diff --git a/ArrayListFun/src/com/company/SimpleList.java b/ArrayListFun/src/com/company/SimpleList.java new file mode 100644 index 0000000..8ad1192 --- /dev/null +++ b/ArrayListFun/src/com/company/SimpleList.java @@ -0,0 +1,38 @@ +package com.company; + +import java.util.ArrayList; + +public class SimpleList +{ + public static void main(String[] args) + { + SimpleList simpleList = new SimpleList(); + + simpleList.demo(); + } + + private void demo() + { + ArrayList myList = new ArrayList<>(); + + myList.add("Red"); + myList.add("Blue"); + myList.add("Green"); + myList.add("Purple"); + myList.add("Orange"); + + for (int i = 0; i < myList.size(); i++) + { + System.out.println(myList.get(i)); + } + + for (String name : myList) + { + System.out.println(name); + } + + + } + + +} From 746401cca84055031d6dff7c1b8fe677b5cad7df Mon Sep 17 00:00:00 2001 From: cdsto Date: Mon, 5 Feb 2018 14:23:28 -0600 Subject: [PATCH 63/70] Simple List Manipulation --- ArrayListFun/src/com/company/ListDemo.java | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 ArrayListFun/src/com/company/ListDemo.java diff --git a/ArrayListFun/src/com/company/ListDemo.java b/ArrayListFun/src/com/company/ListDemo.java new file mode 100644 index 0000000..1049c84 --- /dev/null +++ b/ArrayListFun/src/com/company/ListDemo.java @@ -0,0 +1,69 @@ +package com.company; + +import java.util.ArrayList; + +public class ListDemo +{ + public static void main(String[] args) + { + ListDemo listDemo = new ListDemo(); + + listDemo.execute(); + } + + private void execute() + { + ArrayList myList = new ArrayList<>(); + + myList.add("Table"); + myList.add("Chair"); + myList.add("Sofa"); + + printList(myList); + + myList.add(myList.size(), "Recliner"); + + printList(myList); + + myList.add(0, "Loveseat"); + + printList(myList); + + myList.add(2, "Ottoman"); + + printList(myList); + + myList.remove(myList.size() - 1); + + printList(myList); + + myList.remove(0); + + printList(myList); + + myList.remove(1); + + printList(myList); + + + } + + private void printList(ArrayList list) + { + String element; + + for (int i = 0; i < list.size(); i++) + { + element = list.get(i); + if (i != (list.size() - 1)) + { + System.out.print("" + element + " : "); + } + else if (i == (list.size() - 1)) + { + System.out.println("" + element); + } + } + + } +} From b9c8adea817ab57d989e03a142d0f0ea47af5952 Mon Sep 17 00:00:00 2001 From: cdsto Date: Mon, 5 Feb 2018 16:28:44 -0600 Subject: [PATCH 64/70] Shopping List --- .../src/com/company/ShoppingList.java | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 ArrayListFun/src/com/company/ShoppingList.java diff --git a/ArrayListFun/src/com/company/ShoppingList.java b/ArrayListFun/src/com/company/ShoppingList.java new file mode 100644 index 0000000..674e8b1 --- /dev/null +++ b/ArrayListFun/src/com/company/ShoppingList.java @@ -0,0 +1,138 @@ +package com.company; + +import java.util.ArrayList; +import java.util.Scanner; + +public class ShoppingList +{ + private int total = 0; + + private Scanner scanner = new Scanner(System.in); + + + public static void main(String[] args) + { + ShoppingList shoppingList = new ShoppingList(); + + shoppingList.run(); + } + + private void run() + { + ArrayList myList = new ArrayList<>(); + + String add = "Add"; + String remove = "Remove"; + String print = "Print"; + String clear = "Clear"; + String exit = "Exit"; + + String command; + + do{ + if (myList.size() == 0) + { + System.out.println("There aren't any items in this list. Please add items beginning at position 0."); + } + + + do{ + System.out.println("Enter one of the following commands: Add, Remove, Print, Clear, or Exit."); + + command = scanner.nextLine(); + + if (!command.equalsIgnoreCase(add) && !command.equalsIgnoreCase(remove) && + !command.equalsIgnoreCase(print) && !command.equalsIgnoreCase(clear) && !command.equalsIgnoreCase(exit)) + { + System.out.println("Oops! Please enter a valid command."); + } + } while (!command.equalsIgnoreCase(add) && !command.equalsIgnoreCase(remove) && + !command.equalsIgnoreCase(print) && !command.equalsIgnoreCase(clear) && !command.equalsIgnoreCase(exit)); + + + if (command.equalsIgnoreCase(add)) + { + add(myList); + } + else if (command.equalsIgnoreCase(remove)) + { + remove(myList); + } + else if (command.equalsIgnoreCase(print)) + { + print(myList); + } + else if (command.equalsIgnoreCase(clear)) + { + clear(myList); + } + + } while (!command.equalsIgnoreCase("Exit")); + + System.out.println("You have finished editing your list."); + print(myList); + } + + private void add(ArrayList list) + { + System.out.print("Enter the position where you would like to add to the list: "); + + int number = scanner.nextInt(); + scanner.nextLine(); + + System.out.println("What would you like to add to the list at position " + number + "?"); + + String addToList = scanner.nextLine(); + + list.add(number,addToList); + + print(list); + } + + private void remove(ArrayList list) + { + System.out.print("Enter the position of the item you would like to remove from the list: "); + + int number = scanner.nextInt(); + scanner.nextLine(); + + list.remove(number); + + print(list); + } + + private void print(ArrayList list) + { + String element; + + for (int i = 0; i < list.size(); i++) + { + element = list.get(i); + + if (i == 0 && list.size() == 1) + { + System.out.println("Item in position number " + i + ": " + element + "."); + } + else if (i == 0) + { + System.out.print("Item in position number " + i + ": " + element + ", "); + } + else if (i != (list.size() - 1)) + { + System.out.print("item in position number " + i + ": " + element + ", "); + } + else if (i == (list.size() - 1)) + { + System.out.println("item in position number " + i + ": " + element); + } + } + + } + + private void clear(ArrayList list) + { + list.clear(); + } + + +} From 67f95cb9ad14d9136ad13347a135600db7d1ec74 Mon Sep 17 00:00:00 2001 From: cdsto Date: Mon, 5 Feb 2018 16:29:52 -0600 Subject: [PATCH 65/70] Shopping List --- ArrayListFun/src/com/company/ShoppingList.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/ArrayListFun/src/com/company/ShoppingList.java b/ArrayListFun/src/com/company/ShoppingList.java index 674e8b1..d7c4085 100644 --- a/ArrayListFun/src/com/company/ShoppingList.java +++ b/ArrayListFun/src/com/company/ShoppingList.java @@ -5,8 +5,6 @@ public class ShoppingList { - private int total = 0; - private Scanner scanner = new Scanner(System.in); From 9d9e04f563d7cb02a0675ef6131626b9a831ceb7 Mon Sep 17 00:00:00 2001 From: cdsto Date: Mon, 5 Feb 2018 19:53:44 -0600 Subject: [PATCH 66/70] Shopping List-Silver --- .../src/com/company/ShoppingListSilver.java | 165 ++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 ArrayListFun/src/com/company/ShoppingListSilver.java diff --git a/ArrayListFun/src/com/company/ShoppingListSilver.java b/ArrayListFun/src/com/company/ShoppingListSilver.java new file mode 100644 index 0000000..07313d6 --- /dev/null +++ b/ArrayListFun/src/com/company/ShoppingListSilver.java @@ -0,0 +1,165 @@ +package com.company; + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Scanner; + +public class ShoppingListSilver +{ + private Scanner scanner = new Scanner(System.in); + + public static void main(String[] args) + { + ShoppingListSilver shoppingListSilver = new ShoppingListSilver(); + + shoppingListSilver.run(); + } + + private void run() + { + ArrayList myList = new ArrayList<>(); + + String add = "Add"; + String remove = "Remove"; + String print = "Print"; + String clear = "Clear"; + String exit = "Exit"; + String sort = "Sort"; + String find = "Find"; + + String command; + + do{ + if (myList.size() == 0) + { + System.out.println("There aren't any items in this list. Please add items beginning at position 0."); + } + + do{ + System.out.println("Enter one of the following commands: Add, Remove, Print, Clear, Sort, Find, or Exit."); + + command = scanner.nextLine(); + + if (!command.equalsIgnoreCase(add) && !command.equalsIgnoreCase(remove) && + !command.equalsIgnoreCase(print) && !command.equalsIgnoreCase(clear) && + !command.equalsIgnoreCase(exit) && !command.equalsIgnoreCase(sort) && !command.equalsIgnoreCase(find)) + { + System.out.println("Oops! Please enter a valid command."); + } + } while (!command.equalsIgnoreCase(add) && !command.equalsIgnoreCase(remove) && + !command.equalsIgnoreCase(print) && !command.equalsIgnoreCase(clear) && + !command.equalsIgnoreCase(exit)&& !command.equalsIgnoreCase(sort) && !command.equalsIgnoreCase(find)); + + if (command.equalsIgnoreCase(add)) + { + add(myList); + } + else if (command.equalsIgnoreCase(remove)) + { + remove(myList); + } + else if (command.equalsIgnoreCase(print)) + { + print(myList); + } + else if (command.equalsIgnoreCase(clear)) + { + clear(myList); + } + else if (command.equalsIgnoreCase(sort)) + { + sort(myList); + } + else if(command.equalsIgnoreCase(find)) + { + find(myList); + } + + } while (!command.equalsIgnoreCase("Exit")); + + System.out.println("You have finished editing your list."); + print(myList); + } + + private void add(ArrayList list) + { + System.out.println("What would you like to add to the list?"); + + String addToList = scanner.nextLine(); + + list.add(addToList); + + print(list); + } + + private void remove(ArrayList list) + { + System.out.print("Enter the position of the item you would like to remove from the list: "); + + int number = scanner.nextInt(); + scanner.nextLine(); + + list.remove(number); + + print(list); + } + + private void print(ArrayList list) + { + String element; + + for (int i = 0; i < list.size(); i++) + { + element = list.get(i); + + if (i == 0 && list.size() == 1) + { + System.out.println("Item in position number " + i + ": " + element + "."); + } + else if (i == 0) + { + System.out.print("Item in position number " + i + ": " + element + ", "); + } + else if (i != (list.size() - 1)) + { + System.out.print("item in position number " + i + ": " + element + ", "); + } + else if (i == (list.size() - 1)) + { + System.out.println("item in position number " + i + ": " + element + "."); + } + } + + } + + private void clear(ArrayList list) + { + list.clear(); + + print(list); + } + + private void sort(ArrayList list) + { + Collections.sort(list); + + print(list); + } + + private void find(ArrayList list) + { + System.out.println("Which item on the list are you looking for?"); + + String searchFor = scanner.nextLine(); + + if (list.contains(searchFor)) + { + System.out.println("Found it! It is in position " + list.indexOf(searchFor)); + } + else + { + System.out.println("No such item!"); + } + } +} From 7d774b3729b10fa7672e7f4b500884bfe95aa82d Mon Sep 17 00:00:00 2001 From: cdsto Date: Tue, 6 Feb 2018 13:22:14 -0600 Subject: [PATCH 67/70] Simple Map Creation --- Maps/src/com/company/SimpleMap.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Maps/src/com/company/SimpleMap.java diff --git a/Maps/src/com/company/SimpleMap.java b/Maps/src/com/company/SimpleMap.java new file mode 100644 index 0000000..4090562 --- /dev/null +++ b/Maps/src/com/company/SimpleMap.java @@ -0,0 +1,24 @@ +package com.company; + +import java.util.HashMap; + +public class SimpleMap +{ + public static void main(String[] args) + { + SimpleMap simpleMap = new SimpleMap(); + + simpleMap.demo(); + } + + private void demo() + { + HashMap hashMap = new HashMap<>(); + + hashMap.put("USA","United States"); + hashMap.put("MEX","Mexico"); + hashMap.put("CAN", "Canada"); + + System.out.println("The value stored with USA is: " + hashMap.get("USA")); + } +} From b11af2cb5687f7ffb9786cddef1c189f3adce520 Mon Sep 17 00:00:00 2001 From: cdsto Date: Tue, 6 Feb 2018 14:02:05 -0600 Subject: [PATCH 68/70] Simple Map Manipulation --- Maps/src/com/company/TimeZoneDemo.java | 52 ++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Maps/src/com/company/TimeZoneDemo.java diff --git a/Maps/src/com/company/TimeZoneDemo.java b/Maps/src/com/company/TimeZoneDemo.java new file mode 100644 index 0000000..68ac56f --- /dev/null +++ b/Maps/src/com/company/TimeZoneDemo.java @@ -0,0 +1,52 @@ +package com.company; + +import java.util.HashMap; + +public class TimeZoneDemo +{ + HashMap hashMap = new HashMap<>(); + + public static void main(String[] args) + { + TimeZoneDemo timeZoneDemo = new TimeZoneDemo(); + + timeZoneDemo.demo(); + } + + private void demo() + { + hashMap.put("EST",-5); + hashMap.put("CST",-6); + hashMap.put("MST",-7); + hashMap.put("PST",-8); + hashMap.put("GMT",0); + + System.out.println(getTimeDiff("PST","EST")); + System.out.println(getTimeDiff("EST","PST")); + + } + + private double getTimeDiff(String timeZone1, String timeZone2) + { + double timeDiff = 0; + + double tz1 = hashMap.get(timeZone1); + double tz2 = hashMap.get(timeZone2); + + //Check to make sure the result would be the same regardless of the order the keys are entered in + if (tz1 < tz2) + { + timeDiff = tz1 - tz2; + } + else if (tz2 < tz1) + { + timeDiff = tz2 - tz1; + } + else + { + System.out.println("Oops."); + } + + return timeDiff; + } +} From 78f3b2b03afca85d62d3259ce072de2b5a39b214 Mon Sep 17 00:00:00 2001 From: cdsto Date: Tue, 6 Feb 2018 21:00:19 -0600 Subject: [PATCH 69/70] Pet Hotel - Bronze --- Maps/src/com/company/PetHotel.java | 196 +++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 Maps/src/com/company/PetHotel.java diff --git a/Maps/src/com/company/PetHotel.java b/Maps/src/com/company/PetHotel.java new file mode 100644 index 0000000..10c9e9b --- /dev/null +++ b/Maps/src/com/company/PetHotel.java @@ -0,0 +1,196 @@ +package com.company; + +import java.util.Map; +import java.util.Scanner; +import java.util.TreeMap; + +public class PetHotel +{ + TreeMap hotelRooms; + private Scanner scanner; + + private String petName; + private int roomNumber; + + public PetHotel() + { + hotelRooms = new TreeMap<>(); + scanner = new Scanner(System.in); + } + + public static void main(String[] args) + { + PetHotel petHotel = new PetHotel(); + + petHotel.run(); + } + + private void run() + { + String command; + String[] words; + + do + { + int fromRoomNumber; + int toRoomNumber; + + if (hotelRooms.isEmpty()) + { + System.out.println("There aren't any pets here right now."); + } + + System.out.print("Enter command: "); + String inputLine = scanner.nextLine(); + words = inputLine.split(" "); + command = words[0]; + + if (command.equalsIgnoreCase("CheckIn")) + { + petName = words[1]; + roomNumber = Integer.parseInt(words[2]); + + boolean roomExists = (roomNumber >= 100 && roomNumber <= 109); + + if (!roomExists) + { + System.out.println("Sorry, we only have rooms 100 - 109. Please select a room that exists."); + } + else if (hotelRooms.get(roomNumber) != null) + { + System.out.println("Sorry, this room already has an occupant. Please select a different one."); + } + else if (hotelRooms.get(roomNumber) == null) + { + checkIn(petName, roomNumber); + } + } + else if (command.equalsIgnoreCase("CheckOut")) + { + String petName = words[1]; + roomNumber = Integer.parseInt(words[2]); + + if (hotelRooms.get(roomNumber) == null) + { + System.out.println("Your pet is not in room number " + roomNumber + "."); + } + else + { + boolean roomExists = (roomNumber >= 100 && roomNumber <= 109); + boolean isRightPet = hotelRooms.get(roomNumber).equalsIgnoreCase(petName); + + if (!roomExists) + { + System.out.println("Sorry, we only have rooms 100 - 109. Please select a room that exists."); + } + else if (hotelRooms.get(roomNumber) == null) + { + System.out.println("Sorry, this room is empty."); + } + else if (!isRightPet) + { + System.out.println("This is not the pet you're looking for."); + } + else if (hotelRooms.get(roomNumber) != null) + { + checkOut(petName, roomNumber); + } + } + } + else if (command.equalsIgnoreCase("Move")) + { + String petName = words[1]; + fromRoomNumber = Integer.parseInt(words[2]); + toRoomNumber = Integer.parseInt(words[3]); + + if (hotelRooms.get(fromRoomNumber) == null) + { + System.out.println("Your pet is not in room number " + fromRoomNumber + "."); + } + else + { + boolean toRoomExists = (toRoomNumber >= 100 && toRoomNumber <= 109); + boolean fromRoomExists = (fromRoomNumber >= 100 && fromRoomNumber <=109); + boolean isRightPet = hotelRooms.get(fromRoomNumber).equalsIgnoreCase(petName); + + if (!fromRoomExists) + { + System.out.println("Sorry, we only have rooms 100 - 109. " + petName + " is currently in a room that exists. Please try again." ); + } + else if (!toRoomExists) + { + System.out.println("Sorry, we only have rooms 100 - 109. We cannot move " + petName + " to a room that does not exist."); + } + else if (hotelRooms.get(toRoomNumber) != null) + { + System.out.println("Sorry, room number " + toRoomNumber + " already has an occupant. Please select a different one."); + } + else if (!isRightPet) + { + System.out.println("This is not the pet you're looking for."); + } + else + { + move(petName,fromRoomNumber,toRoomNumber); + } + } + } + else if (command.equalsIgnoreCase("Occupancy")) + { + getOccupancy(); + } + else if (command.equalsIgnoreCase("CloseForSeason")) + { + closeForSeason(); + + } + else if (command.equalsIgnoreCase("Exit")) + { + System.out.println("You have ended the program!"); + + } + else + { + System.out.println("Sorry, I didn't understand that. Please enter a valid command."); + } + } while (!command.equalsIgnoreCase("Exit")); + + } + + private void checkIn(String petName, int roomNumber) + { + hotelRooms.put(roomNumber, petName); + System.out.println("" + petName + " checked into room " + roomNumber + "."); + + System.out.println(hotelRooms); + } + + private void checkOut(String petName, int roomNumber) + { + hotelRooms.remove(roomNumber, petName); + System.out.println("" + petName + " has been checked out of room number " + roomNumber + "."); + + System.out.println(hotelRooms); + } + + private void move(String petname, int fromRoomNumber, int toRoomNumber) + { + hotelRooms.remove(fromRoomNumber); + hotelRooms.put(toRoomNumber, petname); + System.out.println("" + petname + " has been moved from room number " + fromRoomNumber + " to room number " + toRoomNumber + "."); + + System.out.println(hotelRooms); + } + + private void getOccupancy() + { + hotelRooms.forEach((roomNumber,petName) -> System.out.println("" + petName + " is in room number " + roomNumber + ".")); + } + + private void closeForSeason() + { + hotelRooms.clear(); + System.out.println("We are closed for the rest of the season. Thank you for your patronage!"); + } + +} From 1ecd7ec42f93cd87f3edd60460b02042d945454b Mon Sep 17 00:00:00 2001 From: cdsto Date: Tue, 13 Feb 2018 15:00:38 -0600 Subject: [PATCH 70/70] Pet Hotel - Silver --- Maps/src/com/company/PetHotel.java | 273 +++++++++++++++++++---------- 1 file changed, 182 insertions(+), 91 deletions(-) diff --git a/Maps/src/com/company/PetHotel.java b/Maps/src/com/company/PetHotel.java index 10c9e9b..b47e78c 100644 --- a/Maps/src/com/company/PetHotel.java +++ b/Maps/src/com/company/PetHotel.java @@ -2,6 +2,7 @@ import java.util.Map; import java.util.Scanner; +import java.util.Set; import java.util.TreeMap; public class PetHotel @@ -9,9 +10,6 @@ public class PetHotel TreeMap hotelRooms; private Scanner scanner; - private String petName; - private int roomNumber; - public PetHotel() { hotelRooms = new TreeMap<>(); @@ -30,6 +28,9 @@ private void run() String command; String[] words; + String petName; + int roomNumber; + do { int fromRoomNumber; @@ -40,6 +41,16 @@ private void run() System.out.println("There aren't any pets here right now."); } + /* Valid commands: + Checkin ; + Checkout ; + Move ; + Swap ; + MoveOver (moves all pets to roomNumber + 1, 109 goes to 100) + Occupancy (list occupants); + CloseForSeason (clear map); + Exit (ends program) */ + System.out.print("Enter command: "); String inputLine = scanner.nextLine(); words = inputLine.split(" "); @@ -50,90 +61,26 @@ private void run() petName = words[1]; roomNumber = Integer.parseInt(words[2]); - boolean roomExists = (roomNumber >= 100 && roomNumber <= 109); - - if (!roomExists) - { - System.out.println("Sorry, we only have rooms 100 - 109. Please select a room that exists."); - } - else if (hotelRooms.get(roomNumber) != null) - { - System.out.println("Sorry, this room already has an occupant. Please select a different one."); - } - else if (hotelRooms.get(roomNumber) == null) - { - checkIn(petName, roomNumber); - } + checkIn(petName, roomNumber); } else if (command.equalsIgnoreCase("CheckOut")) { - String petName = words[1]; + petName = words[1]; roomNumber = Integer.parseInt(words[2]); - if (hotelRooms.get(roomNumber) == null) - { - System.out.println("Your pet is not in room number " + roomNumber + "."); - } - else - { - boolean roomExists = (roomNumber >= 100 && roomNumber <= 109); - boolean isRightPet = hotelRooms.get(roomNumber).equalsIgnoreCase(petName); - - if (!roomExists) - { - System.out.println("Sorry, we only have rooms 100 - 109. Please select a room that exists."); - } - else if (hotelRooms.get(roomNumber) == null) - { - System.out.println("Sorry, this room is empty."); - } - else if (!isRightPet) - { - System.out.println("This is not the pet you're looking for."); - } - else if (hotelRooms.get(roomNumber) != null) - { - checkOut(petName, roomNumber); - } - } + checkOut(petName, roomNumber); } else if (command.equalsIgnoreCase("Move")) { - String petName = words[1]; + petName = words[1]; fromRoomNumber = Integer.parseInt(words[2]); toRoomNumber = Integer.parseInt(words[3]); - if (hotelRooms.get(fromRoomNumber) == null) - { - System.out.println("Your pet is not in room number " + fromRoomNumber + "."); - } - else - { - boolean toRoomExists = (toRoomNumber >= 100 && toRoomNumber <= 109); - boolean fromRoomExists = (fromRoomNumber >= 100 && fromRoomNumber <=109); - boolean isRightPet = hotelRooms.get(fromRoomNumber).equalsIgnoreCase(petName); - - if (!fromRoomExists) - { - System.out.println("Sorry, we only have rooms 100 - 109. " + petName + " is currently in a room that exists. Please try again." ); - } - else if (!toRoomExists) - { - System.out.println("Sorry, we only have rooms 100 - 109. We cannot move " + petName + " to a room that does not exist."); - } - else if (hotelRooms.get(toRoomNumber) != null) - { - System.out.println("Sorry, room number " + toRoomNumber + " already has an occupant. Please select a different one."); - } - else if (!isRightPet) - { - System.out.println("This is not the pet you're looking for."); - } - else - { - move(petName,fromRoomNumber,toRoomNumber); - } - } + move(petName, fromRoomNumber, toRoomNumber); + } + else if (command.equalsIgnoreCase("MoveOver")) + { + moveOver(); } else if (command.equalsIgnoreCase("Occupancy")) { @@ -142,12 +89,17 @@ else if (command.equalsIgnoreCase("Occupancy")) else if (command.equalsIgnoreCase("CloseForSeason")) { closeForSeason(); + } + else if (command.equalsIgnoreCase("Swap")) + { + int firstRoomNumber = Integer.parseInt(words[1]); + int secondRoomNumber = Integer.parseInt(words[2]); + swap(firstRoomNumber, secondRoomNumber); } else if (command.equalsIgnoreCase("Exit")) { System.out.println("You have ended the program!"); - } else { @@ -157,34 +109,174 @@ else if (command.equalsIgnoreCase("Exit")) } - private void checkIn(String petName, int roomNumber) + private boolean roomNotReal(int roomNumber) + { + return !(roomNumber >= 100 && roomNumber <= 109); + } + + private boolean roomTaken(int roomNumber) { - hotelRooms.put(roomNumber, petName); - System.out.println("" + petName + " checked into room " + roomNumber + "."); + return !(hotelRooms.get(roomNumber) == null); + } + + private boolean wrongPet(String petName, int roomNumber) + { + return !(hotelRooms.get(roomNumber).equalsIgnoreCase(petName)); + } - System.out.println(hotelRooms); + private void checkIn(String petName, int roomNumber) + { + if (roomNotReal(roomNumber)) + { + System.out.println("Sorry, we only have rooms 100 - 109. Please select a room that exists."); + } + else if (roomTaken(roomNumber)) + { + System.out.println("Sorry, this room already has an occupant. Please select a different one."); + } + else + { + hotelRooms.put(roomNumber, petName); + System.out.println("" + petName + " checked into room " + roomNumber + "."); + } } private void checkOut(String petName, int roomNumber) { - hotelRooms.remove(roomNumber, petName); - System.out.println("" + petName + " has been checked out of room number " + roomNumber + "."); + if (roomNotReal(roomNumber)) + { + System.out.println("Sorry, we only have rooms 100 - 109. Please select a room that exists."); + } + else if (!roomTaken(roomNumber)) + { + System.out.println("Your pet is not in room number " + roomNumber + "."); + } + else + { + if (wrongPet(petName, roomNumber)) + { + System.out.println("This is not the pet you're looking for."); + } + else if (roomTaken(roomNumber)) + { + hotelRooms.remove(roomNumber, petName); + System.out.println("" + petName + " has been checked out of room number " + roomNumber + "."); + } + } + } + + private void move(String petName, int fromRoomNumber, int toRoomNumber) + { + if (roomNotReal(fromRoomNumber)) + { + System.out.println("Sorry, we only have rooms 100 - 109. " + petName + " is currently in a room that exists. Please try again."); + } + else if (roomNotReal(toRoomNumber)) + { + System.out.println("Sorry, we only have rooms 100 - 109. We cannot move " + petName + " to a room that does not exist."); + } + else if (!roomTaken(fromRoomNumber)) + { + System.out.println("Your pet is not in room number " + fromRoomNumber + "."); + } + else + { + if (roomTaken(toRoomNumber)) + { + System.out.println("Sorry, room number " + toRoomNumber + " already has an occupant. Please select a different one."); + } + else if (wrongPet(petName, fromRoomNumber)) + { + System.out.println("This is not the pet you're looking for."); + } + else + { + hotelRooms.remove(fromRoomNumber); + hotelRooms.put(toRoomNumber, petName); + System.out.println("" + petName + " has been moved from room number " + fromRoomNumber + " to room number " + toRoomNumber + "."); + } + } + } + + private void swap(int firstRoomNumber, int secondRoomNumber) + { + if (roomNotReal(firstRoomNumber)) + { + System.out.println("Sorry, we only have rooms 100 - 109. The pet you are looking for is currently in a room that exists. Please try again."); + } + else if (roomNotReal(secondRoomNumber)) + { + System.out.println("Sorry, we only have rooms 100 - 109. We cannot move the pet you are looking for to a room that does not exist."); + } + else if (!roomTaken(firstRoomNumber)) + { + System.out.println("The pet you are looking for is not in room number " + firstRoomNumber + "."); + } + else + { + if (!roomTaken(secondRoomNumber)) + { + System.out.println("Sorry, there aren't any pets in this room to swap with!"); + } + else + { + String petInFirstRoom = hotelRooms.get(firstRoomNumber); + String petInSecondRoom = hotelRooms.get(secondRoomNumber); - System.out.println(hotelRooms); + hotelRooms.put(secondRoomNumber, petInFirstRoom); + hotelRooms.put(firstRoomNumber, petInSecondRoom); + System.out.println("" + petInFirstRoom + " has been moved to room number " + secondRoomNumber + + ", and " + petInSecondRoom + " has been moved to room number " + firstRoomNumber + "."); + } + } } - private void move(String petname, int fromRoomNumber, int toRoomNumber) + private void moveOver() { - hotelRooms.remove(fromRoomNumber); - hotelRooms.put(toRoomNumber, petname); - System.out.println("" + petname + " has been moved from room number " + fromRoomNumber + " to room number " + toRoomNumber + "."); + String firstPet = null; + + if (hotelRooms.get(109) != null) + { + firstPet = hotelRooms.get(109); + hotelRooms.remove(109); + } + + for (int i = 108; i >= 100; i--) + { + String currentPet = null; + + if (hotelRooms.get(i) != null) + { + currentPet = hotelRooms.get(i); + } - System.out.println(hotelRooms); + if (currentPet != null) + { + hotelRooms.put(i + 1, currentPet); + hotelRooms.remove(i); + } + } + + if (firstPet != null) + { + hotelRooms.put(100, firstPet); + } } private void getOccupancy() { - hotelRooms.forEach((roomNumber,petName) -> System.out.println("" + petName + " is in room number " + roomNumber + ".")); + Set> entries = hotelRooms.entrySet(); + + for (Map.Entry entry : entries) + { + int roomNumber = entry.getKey(); + String petName = entry.getValue(); + + System.out.println("" + petName + " is in room number " + roomNumber + "."); + } + + //Lambda version: + // hotelRooms.forEach((roomNumber,petName) -> System.out.println("" + petName + " is in room number " + roomNumber + ".")); } private void closeForSeason() @@ -192,5 +284,4 @@ private void closeForSeason() hotelRooms.clear(); System.out.println("We are closed for the rest of the season. Thank you for your patronage!"); } - }