From fb870f74a99a31121a795a91e41c6e28b6559190 Mon Sep 17 00:00:00 2001 From: feralgoon Date: Tue, 17 Apr 2018 13:51:43 -0500 Subject: [PATCH 01/34] added comment --- ch02/Variables.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ch02/Variables.java b/ch02/Variables.java index a295abf..e283326 100644 --- a/ch02/Variables.java +++ b/ch02/Variables.java @@ -1,9 +1,12 @@ /** * Examples from Chapter 2. */ -public class Variables { +public class Variables +{ - public static void main(String[] args) { + public static void main(String[] args) + { + //Joseph Teague String message; int x; @@ -59,7 +62,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 5ef0dc314a5c9f4fade602c1eebdb665911c4f5a Mon Sep 17 00:00:00 2001 From: feralgoon Date: Tue, 17 Apr 2018 14:43:40 -0500 Subject: [PATCH 02/34] New exercises uploaded --- ch02/Arithmetic.java | 21 +++++++++++++++++++++ ch02/Assignment.java | 29 +++++++++++++++++++++++++++++ ch02/Constants.java | 19 +++++++++++++++++++ ch02/DataTypes.java | 18 ++++++++++++++++++ ch02/FirstVariable.java | 11 +++++++++++ 5 files changed, 98 insertions(+) create mode 100644 ch02/Arithmetic.java create mode 100644 ch02/Assignment.java create mode 100644 ch02/Constants.java create mode 100644 ch02/DataTypes.java create mode 100644 ch02/FirstVariable.java diff --git a/ch02/Arithmetic.java b/ch02/Arithmetic.java new file mode 100644 index 0000000..93798b9 --- /dev/null +++ b/ch02/Arithmetic.java @@ -0,0 +1,21 @@ +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); + } +} diff --git a/ch02/Assignment.java b/ch02/Assignment.java new file mode 100644 index 0000000..4eac65c --- /dev/null +++ b/ch02/Assignment.java @@ -0,0 +1,29 @@ +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 = 150) + System.out.println("Multiplication sum: " + sum); + + + sum /= factor; //Assign result (150 / 5 = 30) + System.out.println("Division sum: " + sum); + + } +} diff --git a/ch02/Constants.java b/ch02/Constants.java new file mode 100644 index 0000000..8153058 --- /dev/null +++ b/ch02/Constants.java @@ -0,0 +1,19 @@ +public class Constants +{ + public static void main(String[] args) + { + final int TOUCHDOWN = 6; + final int CONVERSION = 1; + final int FIELDGOAL = 3; + + int td, pat, fg, total; + + td = 4 * TOUCHDOWN; + pat = 3 * CONVERSION; + fg = 2 * FIELDGOAL; + + total = (td + pat + fg); + + System.out.println("Score: " + total); + } +} diff --git a/ch02/DataTypes.java b/ch02/DataTypes.java new file mode 100644 index 0000000..4b55ab9 --- /dev/null +++ b/ch02/DataTypes.java @@ -0,0 +1,18 @@ +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); + + } +} diff --git a/ch02/FirstVariable.java b/ch02/FirstVariable.java new file mode 100644 index 0000000..56608c0 --- /dev/null +++ b/ch02/FirstVariable.java @@ -0,0 +1,11 @@ +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 0eebe0863b0bbe0e316adac6922c5f98824f1b13 Mon Sep 17 00:00:00 2001 From: feralgoon Date: Tue, 17 Apr 2018 15:19:12 -0500 Subject: [PATCH 03/34] New exercises uploaded --- ch02/Date.java | 21 +++++++++++++++++++++ ch02/IntExtremes.java | 24 ++++++++++++++++++++++++ ch02/Time.java | 27 +++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 ch02/Date.java create mode 100644 ch02/IntExtremes.java create mode 100644 ch02/Time.java diff --git a/ch02/Date.java b/ch02/Date.java new file mode 100644 index 0000000..bac3747 --- /dev/null +++ b/ch02/Date.java @@ -0,0 +1,21 @@ +public class Date +{ + public static void main(String[] args) + { + String day = "Tuesday"; + int date = 17; + String month = "April"; + int year = 2018; + + //System.out.println(day); + //System.out.println(date); + // System.out.println(month); + //System.out.println(year); + + System.out.println("American Format:"); + System.out.println(day + ", " + month + " " + date + ", " + year); + + System.out.println("European Format:"); + System.out.println(day + " " + date + " " + month + " " + year); + } +} diff --git a/ch02/IntExtremes.java b/ch02/IntExtremes.java new file mode 100644 index 0000000..987d323 --- /dev/null +++ b/ch02/IntExtremes.java @@ -0,0 +1,24 @@ +public class IntExtremes +{ + public static void main(String[] args) + { + int positiveInt; + positiveInt = 2147483647; + + System.out.println(positiveInt); + + positiveInt++; + + System.out.println(positiveInt); + + int negativeInt; + negativeInt = -2147483648; + + System.out.println(negativeInt); + + negativeInt--; + + System.out.println(negativeInt); + + } +} diff --git a/ch02/Time.java b/ch02/Time.java new file mode 100644 index 0000000..5cb3cc7 --- /dev/null +++ b/ch02/Time.java @@ -0,0 +1,27 @@ +public class Time +{ + public static void main(String[] args) + { + int hour = 14; + int min = 58; + int sec = 35; + + int secSinceMidnight = (hour*60*60) + (min*60) + (sec); + System.out.println("There have been " + secSinceMidnight + " seconds since midnight."); + + int secRemaining = ((24-hour) * 60 * 60) + ((60-min) * 60) + (60-sec); + System.out.println("There are " + secRemaining + " seconds remaining today."); + + float percRemain = ((float)secRemaining * 100) / (60*60*24); + System.out.println("There is " + percRemain + " percent of the day remaining."); + + hour = 15; + min = 11; + sec = 17; + + int elapsedTime = (hour * 60 * 60) + (min * 60) + sec; + elapsedTime -= secSinceMidnight; + + System.out.println("It took " + elapsedTime + " seconds to write this program."); + } +} From c826ddfd1b2bb65624dc522c0e1605b3f17d4eb0 Mon Sep 17 00:00:00 2001 From: feralgoon Date: Tue, 17 Apr 2018 15:36:23 -0500 Subject: [PATCH 04/34] New exercises uploaded --- ch02/DoubleByZero.java | 12 ++++++++++++ ch02/IntByZero.java | 12 ++++++++++++ ch02/LoveJava.java | 11 +++++++++++ ch02/Withdrawal.java | 18 ++++++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 ch02/DoubleByZero.java create mode 100644 ch02/IntByZero.java create mode 100644 ch02/LoveJava.java create mode 100644 ch02/Withdrawal.java diff --git a/ch02/DoubleByZero.java b/ch02/DoubleByZero.java new file mode 100644 index 0000000..6832ac7 --- /dev/null +++ b/ch02/DoubleByZero.java @@ -0,0 +1,12 @@ +public class DoubleByZero +{ + public static void main(String[] args) + { + double x = 42; + double y = 0; + + double z = x/y; + + System.out.println(z); + } +} diff --git a/ch02/IntByZero.java b/ch02/IntByZero.java new file mode 100644 index 0000000..13c1fab --- /dev/null +++ b/ch02/IntByZero.java @@ -0,0 +1,12 @@ +public class IntByZero +{ + public static void main(String[] args) + { + int x = 42; + int y = 0; + + int z = x/y; + + System.out.println(z); + } +} diff --git a/ch02/LoveJava.java b/ch02/LoveJava.java new file mode 100644 index 0000000..160de69 --- /dev/null +++ b/ch02/LoveJava.java @@ -0,0 +1,11 @@ +public class LoveJava +{ + public static void main(String[] args) + { + String first = "I"; + String second = "love"; + String third = "Java!"; + + System.out.println(first + " " + second + " " + third); + } +} diff --git a/ch02/Withdrawal.java b/ch02/Withdrawal.java new file mode 100644 index 0000000..9301c25 --- /dev/null +++ b/ch02/Withdrawal.java @@ -0,0 +1,18 @@ +public class Withdrawal +{ + public static void main(String[] args) + { + int withdrawal = 259; + + int twenties = withdrawal / 20; + withdrawal %= 20; + + int tens = withdrawal / 10; + withdrawal %= 10; + + int fives = withdrawal / 5; + withdrawal %= 5; + + System.out.println("$20(" + twenties + "), $10(" + tens + "), $5(" + fives + "), $1(" + withdrawal + ")"); + } +} From 3a6fb0b2904dc855a0586b5d0af33a032c68ad40 Mon Sep 17 00:00:00 2001 From: feralgoon Date: Wed, 18 Apr 2018 12:53:42 -0500 Subject: [PATCH 05/34] Upload new Exercises --- ch03/CelsiusToFahrenheit.java | 17 +++++++++++++++++ ch03/Echo.java | 6 ++++-- ch03/GuessMyNumber.java | 26 ++++++++++++++++++++++++++ ch03/Printf.java | 7 +++++++ ch03/SecondsToHours.java | 22 ++++++++++++++++++++++ 5 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 ch03/CelsiusToFahrenheit.java create mode 100644 ch03/GuessMyNumber.java create mode 100644 ch03/Printf.java create mode 100644 ch03/SecondsToHours.java diff --git a/ch03/CelsiusToFahrenheit.java b/ch03/CelsiusToFahrenheit.java new file mode 100644 index 0000000..c310cd6 --- /dev/null +++ b/ch03/CelsiusToFahrenheit.java @@ -0,0 +1,17 @@ +import java.util.Scanner; + +public class CelsiusToFahrenheit +{ + public static void main(String[] args) + { + double temp,convertedTemp; + Scanner in = new Scanner(System.in); + + System.out.println("Enter the temperature to convert in Celcius: "); + temp = in.nextDouble(); + + convertedTemp = (9.0/5.0) * temp + 32; + + System.out.printf("%.1f C = %.1f F", temp, convertedTemp); + } +} diff --git a/ch03/Echo.java b/ch03/Echo.java index 500fd9f..033c41c 100644 --- a/ch03/Echo.java +++ b/ch03/Echo.java @@ -1,8 +1,10 @@ import java.util.Scanner; -public class Echo { +public class Echo +{ - public static void main(String[] args) { + public static void main(String[] args) + { String line; Scanner in = new Scanner(System.in); diff --git a/ch03/GuessMyNumber.java b/ch03/GuessMyNumber.java new file mode 100644 index 0000000..04f216a --- /dev/null +++ b/ch03/GuessMyNumber.java @@ -0,0 +1,26 @@ +import java.util.Scanner; +import java.util.Random; + +public class GuessMyNumber +{ + public static void main(String[] args) + { + int guess,number, diff; + Random random = new Random(); + Scanner in = new Scanner(System.in); + + number = random.nextInt(100) + 1; + + System.out.println("I'm thinking of a number between 1 and 100 (including both)."); + System.out.println("Can you guess what it is?"); + System.out.println("Type a number: "); + + guess = in.nextInt(); + diff = Math.abs(guess - number); + + System.out.printf("Your guess is: %d", guess); + System.out.printf("\nThe number I was thinking of is: %d", number); + System.out.printf("\nYou were off by: %d", diff); + + } +} diff --git a/ch03/Printf.java b/ch03/Printf.java new file mode 100644 index 0000000..fc1d789 --- /dev/null +++ b/ch03/Printf.java @@ -0,0 +1,7 @@ +public class Printf +{ + public static void main(String[] args) + { + System.out.printf("%d",31); + } +} diff --git a/ch03/SecondsToHours.java b/ch03/SecondsToHours.java new file mode 100644 index 0000000..008358e --- /dev/null +++ b/ch03/SecondsToHours.java @@ -0,0 +1,22 @@ +import java.util.Scanner; + +public class SecondsToHours +{ + public static void main(String[] args) + { + int secIn, sec, hour, min; + Scanner in = new Scanner(System.in); + + System.out.println("Please enter the number of seconds: "); + secIn = in.nextInt(); + sec = secIn; + + hour = sec/3600; + sec -= hour*3600; + + min = sec/60; + sec -= min*60; + + System.out.printf("%d Seconds = %d hours, %d minutes, and %d seconds. ", secIn, hour, min, sec); + } +} From 2a90f10ac9015c315b635a143e885604feaac0d5 Mon Sep 17 00:00:00 2001 From: feralgoon Date: Wed, 18 Apr 2018 13:02:32 -0500 Subject: [PATCH 06/34] Upload new Exercises --- ch03/Escape.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 ch03/Escape.java diff --git a/ch03/Escape.java b/ch03/Escape.java new file mode 100644 index 0000000..9016834 --- /dev/null +++ b/ch03/Escape.java @@ -0,0 +1,17 @@ +public class Escape +{ + public static void main(String[] args) + { + String header = "\n\tNEW 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\n"; + + System.out.println(header + forecast); + } +} From 628b9e03a4f03cebf3c4082f52de823105e3fca1 Mon Sep 17 00:00:00 2001 From: feralgoon Date: Wed, 18 Apr 2018 16:15:45 -0500 Subject: [PATCH 07/34] Upload new exercises --- ch04/BigMethodSignature.java | 17 +++++++++++++++++ ch04/DemoMath.java | 30 ++++++++++++++++++++++++++++++ ch04/MathUtil.java | 19 +++++++++++++++++++ ch04/SimpleMethods.java | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 ch04/BigMethodSignature.java create mode 100644 ch04/DemoMath.java create mode 100644 ch04/MathUtil.java create mode 100644 ch04/SimpleMethods.java diff --git a/ch04/BigMethodSignature.java b/ch04/BigMethodSignature.java new file mode 100644 index 0000000..c3f2649 --- /dev/null +++ b/ch04/BigMethodSignature.java @@ -0,0 +1,17 @@ +public class BigMethodSignature +{ + public static void main(String[] args) + { + printSum(4, 1, 35, 81, -3, 90, + 444, -65, 901, 11); + } + + private static void printSum(int num1, int num2, int num3, int num4, int num5, int num6, + int num7, int num8, int num9, int num0) + { + int sum = num1 + num2 + num3 + num4 + num5 + num6 + + num7 + num8 + num9 + num0; + + System.out.println("The sum is " + sum); + } +} diff --git a/ch04/DemoMath.java b/ch04/DemoMath.java new file mode 100644 index 0000000..a3713fa --- /dev/null +++ b/ch04/DemoMath.java @@ -0,0 +1,30 @@ +import java.sql.SQLOutput; + +public class DemoMath +{ + public static void main(String[] args) + { + int num1 = -4; + int num2 = 3; + double dub1 = 2.3; + double dub2 = 1.1; + + System.out.println(num1); + System.out.println("ABS: " + Math.abs(num1)); + + System.out.println(); + + System.out.println(num1 + " and " + num2); + System.out.println("MAX: " + Math.max(num1, num2)); + + System.out.println(); + + System.out.println(dub1 + " and " + dub2); + System.out.println("POW: " + Math.pow(dub1, dub2)); + + System.out.println(); + + System.out.println(Math.PI); + + } +} diff --git a/ch04/MathUtil.java b/ch04/MathUtil.java new file mode 100644 index 0000000..03416b2 --- /dev/null +++ b/ch04/MathUtil.java @@ -0,0 +1,19 @@ +public class MathUtil +{ + private static void printDifference(int num1, int num2) + { + int diff = num1 - num2; + printAbsValue(diff); + + } + + private static void printAbsValue(int num1) + { + System.out.println(Math.abs(num1)); + } + + public static void main(String[] args) + { + printDifference(1000, 4000000); + } +} diff --git a/ch04/SimpleMethods.java b/ch04/SimpleMethods.java new file mode 100644 index 0000000..cf257ff --- /dev/null +++ b/ch04/SimpleMethods.java @@ -0,0 +1,32 @@ +import java.sql.SQLOutput; + +public class SimpleMethods +{ + public static void main(String[] args) + { + printCount(5); + + printSum(4,6); + printSum(7,2); + + printBoolean(true); + printBoolean(false); + } + + private static void printCount(int count) + { + System.out.println("The count it: " + count); + } + + private static void printSum(int val1, int val2) + { + int sum = val1 + val2; + + System.out.println(val1 + " + " + val2 + " = " + sum); + } + + private static void printBoolean(boolean isStudent) + { + System.out.println("I am a student: " + isStudent); + } +} From 94b0ca10e45022da65b68d1c580201f1ff160b45 Mon Sep 17 00:00:00 2001 From: feralgoon Date: Wed, 18 Apr 2018 17:01:23 -0500 Subject: [PATCH 08/34] Upload silver exercise --- 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..815c1e0 --- /dev/null +++ b/ch04/Employee.java @@ -0,0 +1,68 @@ +import java.util.Random; +import java.util.Scanner; + +public class Employee +{ + public static void main(String[] args) + { + int birthYear = 1993; + boolean isUnionMember = false; + String firstName = "Joseph"; + String midName = "Daniel"; + String lastName = "Teague"; + int employeeNumber; + + Scanner scanner = new Scanner(System.in); + + printHeader(); + + System.out.println("Please enter your 5 digit employee number: "); + employeeNumber = scanner.nextInt(); + + printFullName(firstName, midName, lastName); + printUnionStatus(isUnionMember); + printAge(birthYear); + printEvenOrOdd(employeeNumber); + printGenerateSecretPassword(employeeNumber); + } + + private static void printHeader() + { + System.out.println("Welcome to WallabyTech Employee Application"); + System.out.println("==========================================="); + } + + private static void printFullName(String first, String mid, String last) + { + System.out.println(last + ", " + first + " " + mid); + } + + private static void printUnionStatus(boolean status) + { + System.out.println("Your union status is: " + status); + } + + private static void printAge(int birthYear) + { + int age = 2018 - birthYear; + + System.out.println("Your age is: " + age); + } + + private static void printEvenOrOdd(int number) + { + number %= 2; + System.out.println("Employee number is even/odd (1=odd, 0=even): " + number); + } + + private static void printGenerateSecretPassword(int num) + { + Random rNum = new Random(); + int randNum = rNum.nextInt(10) + 1; + + int password = (num + randNum) * 5; + + System.out.println("Employee's random secret pw is: " + password); + + } +} From 99d4decad522b05bda63a5abc00805aff545240e Mon Sep 17 00:00:00 2001 From: feralgoon Date: Thu, 19 Apr 2018 14:06:23 -0500 Subject: [PATCH 09/34] Upload new Exercises for Ch. 5 --- ch05/CheeseOrder.java | 147 ++++++++++++++++++++++++++++++++++++++++ ch05/Comparison.java | 23 +++++++ ch05/Condition.java | 16 +++++ ch05/Else.java | 14 ++++ ch05/If.java | 20 ++++++ ch05/Logic.java | 22 ++++++ ch05/LogicMethods.java | 109 +++++++++++++++++++++++++++++ ch05/Precedence.java | 14 ++++ ch05/Switch.java | 21 ++++++ ch05/SwitchExample.java | 63 +++++++++++++++++ 10 files changed, 449 insertions(+) create mode 100644 ch05/CheeseOrder.java create mode 100644 ch05/Comparison.java create mode 100644 ch05/Condition.java create mode 100644 ch05/Else.java create mode 100644 ch05/If.java create mode 100644 ch05/Logic.java create mode 100644 ch05/LogicMethods.java create mode 100644 ch05/Precedence.java create mode 100644 ch05/Switch.java create mode 100644 ch05/SwitchExample.java diff --git a/ch05/CheeseOrder.java b/ch05/CheeseOrder.java new file mode 100644 index 0000000..eb6190e --- /dev/null +++ b/ch05/CheeseOrder.java @@ -0,0 +1,147 @@ +import java.util.Scanner; + +public class CheeseOrder +{ + public static void main(String[] args) + { + printHeader(); + promptUser(); + + + //This code moved to its own method, in order to utilize + //recursion in case of invalid entry for diameter or yards. + + /*final int PRICE_ONE_INCH = 2; + final int PRICE_TWO_INCH = 4; + final int PRICE_THREE_INCH = 6; + + int diameter; + int yards; + + Scanner in = new Scanner(System.in); + + printHeader(); + + System.out.print("What diameter cheese would you like to order? ---> "); + diameter = in.nextInt(); + in.nextLine(); + + if ((diameter > 3) || (diameter < 1)) + System.out.println("Diameter is too crazy!"); + else + { + System.out.print("How many yards would you like to order? ---> "); + yards = in.nextInt(); + in.nextLine(); + + if(yards < 0) + System.out.println("Can't order that few yards!"); + else + { + if (diameter == 1) + getCost(diameter, yards, PRICE_ONE_INCH); + else if (diameter == 2) + getCost(diameter, yards, PRICE_TWO_INCH); + else + getCost(diameter, yards, PRICE_THREE_INCH); + } + }*/ + } + + private static void printHeader() + { + System.out.println(" Crazy Ed's Wholesale String Cheese "); + System.out.println("======================================================"); + System.out.println(); + System.out.println(" Diameter options: 1 inch, 2 inch, 3 inch"); + System.out.println(" Shipping: $2 per yard(1 inch and 2 inch)"); + System.out.println(" $4 per yard(3 inch)"); + System.out.println(); + System.out.println(" Free Shipping available! "); + System.out.println("1 inch: 50 yards | 2 inch: 75 yards | 3 inch: 25 yards"); + System.out.println("======================================================"); + System.out.println(); + System.out.println(); + } + + private static void getCost(int diameter, int yards, int price) + { + int cost; + + if (diameter == 1) + { + if (yards > 50) + { + cost = price * yards; + } + else + { + cost = (price * yards) + (yards * 2); + } + } + else if (diameter == 2) + { + if (yards > 75) + cost = price * yards; + else + cost = (price * yards) + (yards * 2); + } + else + { + if (yards > 25) + cost = price * yards; + else + cost = (price * yards) + (yards * 2); + } + + cost += 5; + + System.out.println(); + System.out.println("The total price for the order is: $" + cost); + } + + private static void promptUser() + { + final int PRICE_ONE_INCH = 2; + final int PRICE_TWO_INCH = 4; + final int PRICE_THREE_INCH = 6; + + int diameter; + int yards; + + Scanner in = new Scanner(System.in); + + System.out.print("What diameter cheese would you like to order? ---> "); + diameter = in.nextInt(); + in.nextLine(); + + if ((diameter > 3) || (diameter < 1)) + { + System.out.println("Diameter is too crazy!"); + System.out.println(); + promptUser(); + } + else + { + System.out.print("How many yards would you like to order? ---> "); + yards = in.nextInt(); + in.nextLine(); + + if(yards <= 0) + { + System.out.println("Can't order that few yards!"); + System.out.println(); + promptUser(); + } + else + { + if (diameter == 1) + getCost(diameter, yards, PRICE_ONE_INCH); + else if (diameter == 2) + getCost(diameter, yards, PRICE_TWO_INCH); + else + getCost(diameter, yards, PRICE_THREE_INCH); + } + } + } +} diff --git a/ch05/Comparison.java b/ch05/Comparison.java new file mode 100644 index 0000000..723c1b3 --- /dev/null +++ b/ch05/Comparison.java @@ -0,0 +1,23 @@ +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); + } +} diff --git a/ch05/Condition.java b/ch05/Condition.java new file mode 100644 index 0000000..df03307 --- /dev/null +++ b/ch05/Condition.java @@ -0,0 +1,16 @@ +public class Condition +{ + public static void main(String[] args) + { + int num1 = 1357; + int num2 = 2468; + String result; + + result = (num1 % 2 != 0) ? "Odd" : "Even"; + System.out.println(num1 + " is " + result); + + result = (num2 % 2 != 0) ? "Odd" : "Even"; + System.out.println(num2 + " is " + result); + + } +} diff --git a/ch05/Else.java b/ch05/Else.java new file mode 100644 index 0000000..7d3e186 --- /dev/null +++ b/ch05/Else.java @@ -0,0 +1,14 @@ +public class Else +{ + public static void main(String[] args) + { + int hrs = 21; + + if (hrs < 13) + System.out.println("Good morning: " + hrs); + else if (hrs < 18) + System.out.println("Good afternoon: " + hrs); + else + System.out.println("Good evening: " + hrs); + } +} diff --git a/ch05/If.java b/ch05/If.java new file mode 100644 index 0000000..55d3238 --- /dev/null +++ b/ch05/If.java @@ -0,0 +1,20 @@ +public class If +{ + public static void main(String[] args) + { + int num = 13; + + 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."); + } + + if(((num > 5) && (num < 10)) || (num == 12)) + System.out.println("Number is 6-9 inclusive, or 12"); + + } +} diff --git a/ch05/Logic.java b/ch05/Logic.java new file mode 100644 index 0000000..970936d --- /dev/null +++ b/ch05/Logic.java @@ -0,0 +1,22 @@ +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(); + + System.out.println("Either YesYes True: " + (yes || yes)); + System.out.println("Either YesNo True: " + (yes || no)); + System.out.println("Either NoNo True: " + (no || no)); + + System.out.println(); + + System.out.println("Original Yes Value: " + yes); + System.out.println("Inverse Yes Value : " + !yes); + } +} diff --git a/ch05/LogicMethods.java b/ch05/LogicMethods.java new file mode 100644 index 0000000..aa4550a --- /dev/null +++ b/ch05/LogicMethods.java @@ -0,0 +1,109 @@ +public class LogicMethods +{ + public static void main(String[] args) + { + printIsLarge(79); + printIsLarge(301); + printIsLarge(12); + printIsLarge(-344); + printIsLarge(734); + printIsLarge(45); + + System.out.println(); + + printIsLargeOrSmall(9); + printIsLargeOrSmall(64); + printIsLargeOrSmall(357); + printIsLargeOrSmall(-999); + printIsLargeOrSmall(91); + printIsLargeOrSmall(101); + + System.out.println(); + + printLargest(6,9); + printLargest(-23,-9); + printLargest(45,17); + printLargest(55,55); + printLargest(889, 789); + printLargest(3,3); + printLargest(45,34); + + System.out.println(); + + printLargestOdd(35,66); + printLargestOdd(77,77); + printLargestOdd(88,88); + printLargestOdd(45,99); + printLargestOdd(61,59); + printLargestOdd(87,89); + + } + + 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 num1, int num2) + { + if (num1 > num2) + System.out.println("The largest number is: " + num1); + else if (num1 < num2) + System.out.println("The largest number is: " + num2); + else + System.out.println("The numbers are equal."); + } + + private static void printLargestOdd(int number1, int number2) + { + + //Messed up royally with this code, including using the modulus incorrectly. + //Use this comment as an example of what not to do when referring back. + + /*if (number1 > number2) + { + if (2 % number1 == 1) + System.out.println("The largest odd number is: " + number1); + } + else if (number1 < number2) + { + if (2 % number2 == 1) + System.out.println("The largest odd number is: " + number2); + } + else if ((2 % number1 == 0) && (2 % number2 == 0)) + System.out.println("Neither number is odd."); + else if ((2 % number1 == 1) && (2 % number2 == 1)) + { + int sum = number1 + number2; + System.out.println("Two odds make an even: " + sum); + }*/ + + if ((number1 % 2 == 1) && (number2 % 2 == 1)) + { + if (number1 > number2) + System.out.println("The largest odd number is: " + number1); + else if (number1 < number2) + System.out.println("The largest odd number is: " + number2); + else + { + int sum = number1 + number2; + System.out.println("Two odds make an even: " + sum); + } + } + else if (number1 % 2 == 1) + System.out.println("The largest odd number is: " + number1); + else if (number2 % 2 == 1) + System.out.println("The largest odd number is: " + number2); + else + System.out.println("Neither number is odd."); + } +} diff --git a/ch05/Precedence.java b/ch05/Precedence.java new file mode 100644 index 0000000..0734d5e --- /dev/null +++ b/ch05/Precedence.java @@ -0,0 +1,14 @@ +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 specific order: " + sum); + } +} diff --git a/ch05/Switch.java b/ch05/Switch.java new file mode 100644 index 0000000..874e7d6 --- /dev/null +++ b/ch05/Switch.java @@ -0,0 +1,21 @@ +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"); + } +} diff --git a/ch05/SwitchExample.java b/ch05/SwitchExample.java new file mode 100644 index 0000000..bf8da3c --- /dev/null +++ b/ch05/SwitchExample.java @@ -0,0 +1,63 @@ +public class SwitchExample +{ + public static void main(String[] args) + { + lastNameWinner("Jones"); + lastNameWinner("Smith"); + lastNameWinner("Teague"); + lastNameWinner("Lazenby"); + lastNameWinner("Jason"); + + dayOfWeek(4); + dayOfWeek(2); + dayOfWeek(-2); + dayOfWeek(100); + } + + private static void lastNameWinner(String name) + { + switch (name) + { + 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."); + break; + } + } + + private static void dayOfWeek(int num) + { + switch (num) + { + 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: " + num); + break; + } + } +} From b1cf2a255bda725beaa5e7aed357bab542f75661 Mon Sep 17 00:00:00 2001 From: feralgoon Date: Thu, 19 Apr 2018 14:25:33 -0500 Subject: [PATCH 10/34] Upload new Exercises for Ch. 5 --- ch05/CheeseOrder.java | 109 +++++++++++++++--------------------------- 1 file changed, 39 insertions(+), 70 deletions(-) diff --git a/ch05/CheeseOrder.java b/ch05/CheeseOrder.java index eb6190e..115a5e1 100644 --- a/ch05/CheeseOrder.java +++ b/ch05/CheeseOrder.java @@ -5,47 +5,7 @@ public class CheeseOrder public static void main(String[] args) { printHeader(); - promptUser(); - - - //This code moved to its own method, in order to utilize - //recursion in case of invalid entry for diameter or yards. - - /*final int PRICE_ONE_INCH = 2; - final int PRICE_TWO_INCH = 4; - final int PRICE_THREE_INCH = 6; - - int diameter; - int yards; - - Scanner in = new Scanner(System.in); - - printHeader(); - - System.out.print("What diameter cheese would you like to order? ---> "); - diameter = in.nextInt(); - in.nextLine(); - - if ((diameter > 3) || (diameter < 1)) - System.out.println("Diameter is too crazy!"); - else - { - System.out.print("How many yards would you like to order? ---> "); - yards = in.nextInt(); - in.nextLine(); - - if(yards < 0) - System.out.println("Can't order that few yards!"); - else - { - if (diameter == 1) - getCost(diameter, yards, PRICE_ONE_INCH); - else if (diameter == 2) - getCost(diameter, yards, PRICE_TWO_INCH); - else - getCost(diameter, yards, PRICE_THREE_INCH); - } - }*/ + startOrder(); } private static void printHeader() @@ -64,7 +24,7 @@ private static void printHeader() System.out.println(); } - private static void getCost(int diameter, int yards, int price) + private static void getCost(int diameter, int yards, int price, int shipping) { int cost; @@ -76,7 +36,7 @@ private static void getCost(int diameter, int yards, int price) } else { - cost = (price * yards) + (yards * 2); + cost = (price * yards) + (yards * shipping); } } else if (diameter == 2) @@ -84,14 +44,14 @@ else if (diameter == 2) if (yards > 75) cost = price * yards; else - cost = (price * yards) + (yards * 2); + cost = (price * yards) + (yards * shipping); } else { if (yards > 25) cost = price * yards; else - cost = (price * yards) + (yards * 2); + cost = (price * yards) + (yards * shipping); } cost += 5; @@ -100,14 +60,9 @@ else if (diameter == 2) System.out.println("The total price for the order is: $" + cost); } - private static void promptUser() + private static void startOrder() { - final int PRICE_ONE_INCH = 2; - final int PRICE_TWO_INCH = 4; - final int PRICE_THREE_INCH = 6; - int diameter; - int yards; Scanner in = new Scanner(System.in); @@ -119,29 +74,43 @@ private static void promptUser() { System.out.println("Diameter is too crazy!"); System.out.println(); - promptUser(); + startOrder(); } else - { - System.out.print("How many yards would you like to order? ---> "); - yards = in.nextInt(); - in.nextLine(); + finishOrder(diameter); + } - if(yards <= 0) - { - System.out.println("Can't order that few yards!"); - System.out.println(); - promptUser(); - } + private static void finishOrder(int diameter) + { + final int PRICE_ONE_INCH = 2; + final int PRICE_TWO_INCH = 4; + final int PRICE_THREE_INCH = 6; + final int SHIP_ONE_INCH = 2; + final int SHIP_TWO_INCH = 2; + final int SHIP_THREE_INCH = 4; + + int yards; + + Scanner in = new Scanner(System.in); + + System.out.print("How many yards would you like to order? ---> "); + yards = in.nextInt(); + in.nextLine(); + + if(yards <= 0) + { + System.out.println("Can't order that few yards!"); + System.out.println(); + finishOrder(diameter); + } + else + { + if (diameter == 1) + getCost(diameter, yards, PRICE_ONE_INCH, SHIP_ONE_INCH); + else if (diameter == 2) + getCost(diameter, yards, PRICE_TWO_INCH, SHIP_TWO_INCH); else - { - if (diameter == 1) - getCost(diameter, yards, PRICE_ONE_INCH); - else if (diameter == 2) - getCost(diameter, yards, PRICE_TWO_INCH); - else - getCost(diameter, yards, PRICE_THREE_INCH); - } + getCost(diameter, yards, PRICE_THREE_INCH, SHIP_THREE_INCH); } } } From 36c6821a1ff2dabd772bce290593b4e0d9a9b309 Mon Sep 17 00:00:00 2001 From: feralgoon Date: Thu, 19 Apr 2018 14:26:48 -0500 Subject: [PATCH 11/34] Split method to get order into two separate methods, so as not to have to re-enter diameter in case of invalid yard entry. --- ch05/CheeseOrder.java | 1 + 1 file changed, 1 insertion(+) diff --git a/ch05/CheeseOrder.java b/ch05/CheeseOrder.java index 115a5e1..ef97d47 100644 --- a/ch05/CheeseOrder.java +++ b/ch05/CheeseOrder.java @@ -2,6 +2,7 @@ public class CheeseOrder { + public static void main(String[] args) { printHeader(); From 6d3d69b3ad5ed123d46dd8ae14aeaa5387dee74c Mon Sep 17 00:00:00 2001 From: feralgoon Date: Thu, 19 Apr 2018 15:47:13 -0500 Subject: [PATCH 12/34] Chapter 5 exercises --- ch05/CoffeDiscounts.java | 62 ++++++++++++++++++++++++++++++++++++++++ ch05/TicketNumber.java | 33 +++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 ch05/CoffeDiscounts.java create mode 100644 ch05/TicketNumber.java diff --git a/ch05/CoffeDiscounts.java b/ch05/CoffeDiscounts.java new file mode 100644 index 0000000..4186e6b --- /dev/null +++ b/ch05/CoffeDiscounts.java @@ -0,0 +1,62 @@ +import java.util.Scanner; + +public class CoffeDiscounts +{ + public static void main(String[] args) + { + int commute; + int busOrBike; + Scanner scan = new Scanner(System.in); + + printHeader(); + + System.out.print("Please enter the distance of the commute: ------> "); + commute = scan.nextInt(); + scan.nextLine(); + System.out.println(); + + System.out.print("Please enter a 1 for bicycle, and a 2 for bus: ->"); + busOrBike = scan.nextInt(); + scan.nextLine(); + System.out.println(); + + findDiscounts(commute, busOrBike); + + } + + private static void printHeader() + { + System.out.println("Green Coffee Growers of Antarctica"); + System.out.println(" Discount Check Software"); + System.out.println("=================================="); + System.out.println(); + } + + private static void findDiscounts(int commute, int busOrBike) + { + String result = "You get-----> "; + int discount; + + if (commute < 21) + result += "a free coffee: "; + if(busOrBike == 1) + { + if ((commute >= 30) && ( commute < 50)) + result += "20% discount. "; + else if (commute < 50) + result += "30% discount."; + else + result += "No discount"; + } + else if (busOrBike == 2) + { + if ((commute >=35) && (commute < 50)) + result += "50% discount."; + else if (commute > 35) + result += "30% discount."; + else + result += "No discount."; + } + System.out.println(result); + } +} \ No newline at end of file diff --git a/ch05/TicketNumber.java b/ch05/TicketNumber.java new file mode 100644 index 0000000..cac8769 --- /dev/null +++ b/ch05/TicketNumber.java @@ -0,0 +1,33 @@ +import java.util.Scanner; + +public class TicketNumber +{ + public static void main(String[] args) + { + int ticketNumber; + Scanner in = new Scanner(System.in); + + printHeader(); + + System.out.print("Please enter the ticket number: "); + ticketNumber = in.nextInt(); + in.nextLine(); + + int lastDigit = ticketNumber % 10; + int ticketPrefix = ticketNumber / 10; + + boolean result = ((ticketPrefix % 7) == lastDigit); + + if (result) + System.out.println("Good Number"); + else + System.out.println("Bad number"); + } + + private static void printHeader() + { + System.out.println("Travel Tickets Company Ticket Checking Software"); + System.out.println("==============================================="); + System.out.println(); + } +} From 7b164644b1f9a78c9806f147cb756f7f046f6db4 Mon Sep 17 00:00:00 2001 From: feralgoon Date: Thu, 19 Apr 2018 15:58:24 -0500 Subject: [PATCH 13/34] Deleted int discount due to non use. --- ch05/CoffeDiscounts.java | 1 - 1 file changed, 1 deletion(-) diff --git a/ch05/CoffeDiscounts.java b/ch05/CoffeDiscounts.java index 4186e6b..86ac5d9 100644 --- a/ch05/CoffeDiscounts.java +++ b/ch05/CoffeDiscounts.java @@ -35,7 +35,6 @@ private static void printHeader() private static void findDiscounts(int commute, int busOrBike) { String result = "You get-----> "; - int discount; if (commute < 21) result += "a free coffee: "; From f5adb119b288ea5388e7cbb23fbd2dd9916e02fd Mon Sep 17 00:00:00 2001 From: feralgoon Date: Sat, 21 Apr 2018 17:52:32 -0500 Subject: [PATCH 14/34] Chapter 6 exercises --- ch06/MethodChecks.java | 40 ++++++++++++++++++++++++++++++++++++++++ ch06/Multadd.java | 13 +++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 ch06/MethodChecks.java create mode 100644 ch06/Multadd.java diff --git a/ch06/MethodChecks.java b/ch06/MethodChecks.java new file mode 100644 index 0000000..64f89b2 --- /dev/null +++ b/ch06/MethodChecks.java @@ -0,0 +1,40 @@ +public class MethodChecks +{ + public static void main(String[] args) + { + System.out.println(isDivisible(3,5)); + System.out.println(isDivisible(10,5)); + + System.out.println(); + + System.out.println(isTriangle(12, 1, 3)); + System.out.println(isTriangle(4, 5, 9)); + System.out.println(isTriangle(61, 455, 288)); + System.out.println(isTriangle(15, 16, 12)); + System.out.println(isTriangle(61, 455, 28)); + System.out.println(isTriangle(6, 45, 28)); + System.out.println(isTriangle(10, 8,19)); + } + + private static boolean isDivisible(int n, int m) + { + if (n % m == 0) + return true; + else + return false; + } + + private static boolean isTriangle(int a, int b, int c) + { + boolean result = true; + + if ( a > (b + c)) + result = false; + else if (b > (a + c)) + result = false; + else if (c > (b + c)) + result = false; + + return result; + } +} diff --git a/ch06/Multadd.java b/ch06/Multadd.java new file mode 100644 index 0000000..00eeb79 --- /dev/null +++ b/ch06/Multadd.java @@ -0,0 +1,13 @@ +public class Multadd +{ + public static void main(String[] args) + { + System.out.println(multadd(1.0, 2.0, 3.0)); + System.out.println(multadd(3.0, 2.0, 4.0)); + } + + private static double multadd(double a, double b, double c) + { + return (a * b + c); + } +} From 2d8ed9279b4ce4f2c884f65364594302e45ab375 Mon Sep 17 00:00:00 2001 From: feralgoon Date: Sat, 21 Apr 2018 17:58:47 -0500 Subject: [PATCH 15/34] Chapter 6 exercises --- ch06/MathUtil.java | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 ch06/MathUtil.java diff --git a/ch06/MathUtil.java b/ch06/MathUtil.java new file mode 100644 index 0000000..db84a37 --- /dev/null +++ b/ch06/MathUtil.java @@ -0,0 +1,32 @@ +public class MathUtil +{ + public static void main(String[] args) + { + System.out.println(absoluteSum(10, -20)); + System.out.println(absoluteSum(0, -1)); + System.out.println(absoluteSum(12, 12)); + + System.out.println(); + + System.out.println(absoluteSum(-5, 4, -1)); + System.out.println(absoluteSum(3, -10, -2)); + } + + private static int absoluteSum(int a, int b) + { + a = Math.abs(a); + b = Math.abs(b); + + return a + b; + } + + private static int absoluteSum(int a, int b, int c) + { + a = Math.abs(a); + b = Math.abs(b); + c = Math.abs(c); + + return a + b + c; + } + +} From 02876f902ffd19eb6f5a50a56a8f524613173149 Mon Sep 17 00:00:00 2001 From: feralgoon Date: Mon, 23 Apr 2018 13:16:18 -0500 Subject: [PATCH 16/34] chapter seven exercises --- ch07/DoWhile.java | 13 +++++++ ch07/For.java | 18 ++++++++++ ch07/SevenA.java | 91 +++++++++++++++++++++++++++++++++++++++++++++++ ch07/SevenB.java | 48 +++++++++++++++++++++++++ ch07/SevenC.java | 48 +++++++++++++++++++++++++ ch07/SevenD.java | 32 +++++++++++++++++ ch07/SevenE.java | 24 +++++++++++++ ch07/SevenF.java | 27 ++++++++++++++ ch07/While.java | 13 +++++++ 9 files changed, 314 insertions(+) create mode 100644 ch07/DoWhile.java create mode 100644 ch07/For.java create mode 100644 ch07/SevenA.java create mode 100644 ch07/SevenB.java create mode 100644 ch07/SevenC.java create mode 100644 ch07/SevenD.java create mode 100644 ch07/SevenE.java create mode 100644 ch07/SevenF.java create mode 100644 ch07/While.java diff --git a/ch07/DoWhile.java b/ch07/DoWhile.java new file mode 100644 index 0000000..77b8e1f --- /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); + } +} diff --git a/ch07/For.java b/ch07/For.java new file mode 100644 index 0000000..cff4ad2 --- /dev/null +++ b/ch07/For.java @@ -0,0 +1,18 @@ +public class For +{ + public static void main(String[] args) + { + int num = 0; + + for (int i = 1; i < 4; i++) + { + System.out.println("Outer Loop i = " + i); + + for (int j = 1; j < 4; j++) + { + System.out.print("\tInner Loop = " + j); + System.out.println("\t\tOuter Loop = " + i); + } + } + } +} diff --git a/ch07/SevenA.java b/ch07/SevenA.java new file mode 100644 index 0000000..d7915da --- /dev/null +++ b/ch07/SevenA.java @@ -0,0 +1,91 @@ +public class SevenA +{ + public static void main(String[] args) + { + forUp(); + whileUp(); + doWhileUp(); + forDown(); + whileDown(); + doWhileDown(); + } + + private static void forUp() + { + System.out.println("Method forUp"); + System.out.println(); + + for(int i = 1; i <= 10; i++) + { + System.out.println("i = " + i); + } + } + + private static void whileUp() + { + System.out.println("Method whileUp"); + System.out.println(); + + int i = 1; + + while (i <= 10) + { + System.out.println("i = " + i); + i++; + } + } + + private static void doWhileUp() + { + System.out.println("Method doWhileUp"); + System.out.println(); + + int i = 1; + + do + { + System.out.println("i = " + i); + i++; + } while (i <= 10); + } + + private static void forDown() + { + System.out.println("Method forDown"); + System.out.println(); + + for(int i = 10; i > 0; i--) + { + System.out.println("i = " + i); + } + } + + private static void whileDown() + { + System.out.println("Method whileDown"); + System.out.println(); + + int i = 10; + + while (i > 0) + { + System.out.println("i = " + i); + i--; + } + } + + private static void doWhileDown() + { + System.out.println("Method doWhileDown"); + System.out.println(); + + int i = 10; + + do + { + System.out.println("i = " + i); + i--; + } while (i > 0); + } + +} diff --git a/ch07/SevenB.java b/ch07/SevenB.java new file mode 100644 index 0000000..99adc45 --- /dev/null +++ b/ch07/SevenB.java @@ -0,0 +1,48 @@ +public class SevenB +{ + public static void main(String[] args) + { + forUp(); + whileUp(); + doWhileUp(); + } + + private static void forUp() + { + System.out.println("Method forUp"); + System.out.println(); + + for(int i = 10; i <= 100; i+=10) + { + System.out.println("i = " + i); + } + } + + private static void whileUp() + { + System.out.println("Method whileUp"); + System.out.println(); + + int i = 10; + + while (i <= 100) + { + System.out.println("i = " + i); + i+=10; + } + } + + private static void doWhileUp() + { + System.out.println("Method doWhileUp"); + System.out.println(); + + int i = 10; + + do + { + System.out.println("i = " + i); + i+=10; + } while (i <= 100); + } +} diff --git a/ch07/SevenC.java b/ch07/SevenC.java new file mode 100644 index 0000000..0d78c1c --- /dev/null +++ b/ch07/SevenC.java @@ -0,0 +1,48 @@ +public class SevenC +{ + public static void main(String[] args) + { + forDownByEight(); + whileDownByEight(); + doWhileDownByEight(); + } + + private static void forDownByEight() + { + System.out.println("Method forDownByEight"); + System.out.println(); + + for(int i = 100; i >= -100; i-=8) + { + System.out.println("i = " + i); + } + } + + private static void whileDownByEight() + { + System.out.println("Method whileDownByEight"); + System.out.println(); + + int i = 100; + + while (i >= -100) + { + System.out.println("i = " + i); + i-=8; + } + } + + private static void doWhileDownByEight() + { + System.out.println("Method doWhileDownByEight"); + System.out.println(); + + int i = 100; + + do + { + System.out.println("i = " + i); + i-=8; + } while (i >= -100); + } +} diff --git a/ch07/SevenD.java b/ch07/SevenD.java new file mode 100644 index 0000000..7898ca6 --- /dev/null +++ b/ch07/SevenD.java @@ -0,0 +1,32 @@ +public class SevenD +{ + public static void main(String[] args) + { + countToInt(56); + countToInt(-25); + } + + private static void countToInt(int num) + { + int count = 1; + + if (num >= 1) + { + while (count < num) + { + System.out.println(count); + count++; + } + } + else if (num < 1) + { + while (count >= num) + { + System.out.println(count); + count--; + } + } + else + System.out.println(count); + } +} diff --git a/ch07/SevenE.java b/ch07/SevenE.java new file mode 100644 index 0000000..4dda5e4 --- /dev/null +++ b/ch07/SevenE.java @@ -0,0 +1,24 @@ +import java.util.Scanner; + +public class SevenE +{ + public static void main(String[] args) + { + loopUntilZero(); + } + + private static void loopUntilZero() + { + Scanner scan = new Scanner(System.in); + int num; + + System.out.println("Enter a number: "); + + do + { + num = scan.nextInt(); + } while (num != 0); + + System.out.println("Finally, you entered zero."); + } +} diff --git a/ch07/SevenF.java b/ch07/SevenF.java new file mode 100644 index 0000000..a0d3dfe --- /dev/null +++ b/ch07/SevenF.java @@ -0,0 +1,27 @@ +import java.util.Scanner; + +public class SevenF +{ + public static void main(String[] args) + { + getNumsAndSum(); + } + + private static void getNumsAndSum() + { + Scanner scan = new Scanner(System.in); + int num; + + int sum = 0; + + System.out.println("Enter some numbers: "); + + do + { + num = scan.nextInt(); + sum += num; + } while (sum < 1000); + + System.out.println("The total is: " + sum); + } +} diff --git a/ch07/While.java b/ch07/While.java new file mode 100644 index 0000000..2b9bb74 --- /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("While Countdown: " + num); + num -= 10; + } + } +} From d9c6a6ddaeef108ae56a3d35ab6dd7f7194671fa Mon Sep 17 00:00:00 2001 From: feralgoon Date: Mon, 23 Apr 2018 14:34:40 -0500 Subject: [PATCH 17/34] chapter seven exercise G --- ch07/SevenG.java | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 ch07/SevenG.java diff --git a/ch07/SevenG.java b/ch07/SevenG.java new file mode 100644 index 0000000..0c87ec6 --- /dev/null +++ b/ch07/SevenG.java @@ -0,0 +1,62 @@ +import java.sql.SQLOutput; + +public class SevenG +{ + public static void main(String[] args) + { + multTable(5); + System.out.println(); + multTable(59); + + } + + private static void multTable(int max) + { + int outerCount = 1; + int innerCount; + int headerCount = 1; + int spaceCount = 0; + String header = ""; + String spacer = ""; + String inLine = ""; + + while (headerCount <= max) + { + header += ("\t\t|\t\t" + headerCount); + headerCount++; + } + + while (spaceCount < header.length()) + { + if (header.charAt(spaceCount) == '\t') + spacer += "-----"; + spaceCount++; + } + + System.out.println(header); + System.out.println(spacer); + + while (outerCount <= max) + { + System.out.print(outerCount + "\t\t|\t\t"); + innerCount = 1; + + while (innerCount <= max) + { + if ((innerCount * outerCount) > 999) + { + inLine += ((innerCount * outerCount) + "\t|\t\t"); + } + else + { + inLine += ((innerCount * outerCount) + "\t\t|\t\t"); + } + innerCount++; + } + System.out.print(inLine); + inLine = ""; + System.out.println(); + outerCount++; + } + } +} From 10ad6a3d764263eaf6c71ca8d7b7c43b72564ec1 Mon Sep 17 00:00:00 2001 From: feralgoon Date: Mon, 23 Apr 2018 14:49:29 -0500 Subject: [PATCH 18/34] chapter seven exercise G --- ch07/SevenG.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ch07/SevenG.java b/ch07/SevenG.java index 0c87ec6..6db31c4 100644 --- a/ch07/SevenG.java +++ b/ch07/SevenG.java @@ -6,7 +6,7 @@ public static void main(String[] args) { multTable(5); System.out.println(); - multTable(59); + multTable(125); } @@ -29,11 +29,11 @@ private static void multTable(int max) while (spaceCount < header.length()) { if (header.charAt(spaceCount) == '\t') - spacer += "-----"; + spacer += "----"; spaceCount++; } - System.out.println(header); + System.out.println(header + "\t\t|"); System.out.println(spacer); while (outerCount <= max) From edce3e4b1b8e4c123044de5bb53f62b54e16998e Mon Sep 17 00:00:00 2001 From: feralgoon Date: Wed, 25 Apr 2018 20:02:00 -0500 Subject: [PATCH 19/34] Chapter 9 gold levels uploaded --- ch09/StringUtil.java | 323 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 323 insertions(+) create mode 100644 ch09/StringUtil.java diff --git a/ch09/StringUtil.java b/ch09/StringUtil.java new file mode 100644 index 0000000..4547bc5 --- /dev/null +++ b/ch09/StringUtil.java @@ -0,0 +1,323 @@ +import java.util.Collections; + +public class StringUtil +{ + public static void main(String[] args) + { + System.out.println("Welcome to StringUtil"); + + System.out.println("getFirstCharacter for 'Hello' returns " + getFirstCharacter("Hello")); + System.out.println("getFirstCharacter for 'Goodbye' returns " + getFirstCharacter("Goodbye")); + + System.out.println("getLastCharacter for 'Hello' returns " + getLastCharacter("Hello")); + System.out.println("getLastCharacter for 'Goodbye' returns " + getLastCharacter("Goodbye")); + + System.out.println("getFirstTwoCharacters for 'Hello' returns " + getFirstTwoCharacters("Hello")); + System.out.println("getFirstTwoCharacters for 'Goodbye' returns " + getFirstTwoCharacters("Goodbye")); + + System.out.println("getLastTwoCharacters for 'Hello' returns " + getLastTwoCharacters("Hello")); + System.out.println("getLastTwoCharacters for 'Goodbye' returns " + getLastTwoCharacters("Goodbye")); + + System.out.println("getAllButFirstThreeCharacters for 'Hello' returns " + getAllButFirstThreeCharacters("Hello")); + System.out.println("getAllButFirstThreeCharacters for 'Goodbye' returns " + getAllButFirstThreeCharacters("Goodbye")); + + printCharacters("Hello"); + printCharacters("Goodbye"); + + printPhoneNumber("234-666-8888"); + printPhoneNumber("5015550100"); + + System.out.println("findFirstE for 'Hello' is " + findFirstE("Hello")); + System.out.println("findFirstE for 'Goodbye' is " + findFirstE("Goodbye")); + + System.out.println("isFinn for 'Finn' returns " + isFinn("Finn")); + System.out.println("isFinn for 'Jake' returns " + isFinn("Jake")); + + System.out.println("Reverse of 'Finn' is " + reverse("Finn")); + + System.out.println("'Hello' is a palindrome: " + isPalindrome("Hello")); + System.out.println("'racecar' is a palindrome: " + isPalindrome("racecar")); + + System.out.println(); + + String[] words1 = {"Listen", "Apple", "Never", "Exclaim", "Red"}; + String[] words2 = {"Sample", "Perfect", "Test", "Test", "Boop"}; + String[] words3 = {"Listen", "Apple", "Never", "Exclaim", "Red", "Angry"}; + + System.out.println("Testing allLetters for words1: " + allLetters(words1)); + System.out.println("Testing allLetters for words2: " + allLetters(words2)); + System.out.println("Testing allLetters for words3: " + allLetters(words3)); + + System.out.println(); + + System.out.println("Testing allLettersNoDupes for words1: " + allLettersNoDupes(words1)); + System.out.println("Testing allLettersNoDupes for words2: " + allLettersNoDupes(words2)); + System.out.println("Testing allLettersNoDupes for words3: " + allLettersNoDupes(words3)); + + System.out.println(); + + String[] words4 = {"Spock", "Spock", "Kirk", "Kirk"}; + String[] words5 = {"Spock", "Spock", "Kirk", "Kirk", "Spock"}; + + System.out.println("Testing sameCount for words4: " + sameCount(words4)); + System.out.println("Testing sameCount for words5: " + sameCount(words5)); + + System.out.println(); + + String[] words6 = {"Zebra", "Ant", "Car", "Boat", "Alien", "Aardvark", "Giraffe", "Owl", "Earth"}; + + System.out.println("words6 initial values: "); + for (String s : words6) + { + System.out.println(s); + } + System.out.println("Sorting words6 alphabetically: "); + words6 = sort(words6); + for (String s : words6) + { + System.out.println(s); + } + + } + + private static String[] sort(String[] strings) + { + for (int i = 0; i < strings.length - 1; i++) + { + for (int j = i + 1; j < strings.length; j++) + { + if (strings[i].compareTo(strings[j]) > 0) + { + String temp = strings[i]; + strings[i] = strings[j]; + strings[j] = temp; + } + } + } + + return strings; + } + + private static boolean sameCount(String[] words) + { + boolean result = true; + int[] counts = new int[words.length]; + int countIndex = 0; + + for (int i = 0; i < counts.length; i++) + { + counts[i] = countOccurences(words, words[i]); + } + + int tempNum = counts[0]; + + for (int i = 0; i < counts.length; i++) + { + if (counts[i] != 0 && counts[i] != tempNum) + { + result = false; + } + } + + return result; + } + + private static int countOccurences(String[] strings, String value) + { + int count = 0; + + for (String s : strings) + { + if (s.equals(value)) + { + count++; + } + } + + return count; + } + + private static boolean allLettersNoDupes(String[] words) + { + boolean result = false; + + int countL = 0; + int countE = 0; + int countA = 0; + int countR = 0; + int countN = 0; + + + for (int i = 0; i < words.length; i++) + { + if (words[i].charAt(0) == 'L') + { + countL++; + } + if (words[i].charAt(0) == 'E') + { + countE++; + } + if (words[i].charAt(0) == 'A') + { + countA++; + } + if (words[i].charAt(0) == 'R') + { + countR++; + } + if (words[i].charAt(0) == 'N') + { + countN++; + } + } + + if (countL == 1 && countE == 1 && countA == 1 && + countR == 1 && countN == 1) + { + result = true; + } + return result; + } + + private static boolean allLetters(String[] words) + { + boolean result = false; + + boolean startsWithL = false; + boolean startsWithE = false; + boolean startsWithA = false; + boolean startsWithR = false; + boolean startsWithN = false; + + for (int i = 0; i < words.length; i++) + { + if (words[i].charAt(0) == 'L') + { + startsWithL = true; + } + if (words[i].charAt(0) == 'E') + { + startsWithE = true; + } + if (words[i].charAt(0) == 'A') + { + startsWithA = true; + } + if (words[i].charAt(0) == 'R') + { + startsWithR = true; + } + if (words[i].charAt(0) == 'N') + { + startsWithN = true; + } + } + + if (startsWithL && startsWithE && startsWithA + && startsWithR && startsWithN) + { + result = true; + } + return result; + } + + private static boolean isPalindrome(String s) + { + boolean result = false; + + if (s.equals(reverse(s))) + { + result = true; + } + + return result; + } + + private static String reverse(String s) + { + String result = ""; + + for (int i = 0; i < s.length(); i++) + { + result = result + s.charAt(s.length() - 1 - i); + } + return result; + } + + private static boolean isFinn(String s) + { + boolean result = false; + + if (s.equals("Finn")) + { + result = true; + } + + return result; + } + + private static int findFirstE(String s) + { + int result = s.indexOf('e'); + return result; + } + + private static void printPhoneNumber(String s) + { + if (s.length() == 10) + { + System.out.println("Area Code: " + s.substring(0, 3) + + "\tExchange: " + s.substring(3, 6) + + "\tLine Number: " + s.substring(6)); + } else + { + System.out.println("Area Code: " + s.substring(0, 3) + + "\tExchange: " + s.substring(4, 7) + + "\tLine Number: " + s.substring(8)); + } + } + + private static void printCharacters(String s) + { + for (int i = 0; i < s.length(); i++) + { + System.out.println(s.charAt(i) + ":" + i); + } + } + + private static String getAllButFirstThreeCharacters(String s) + { + String result = s.substring(3); + + return result; + } + + private static String getLastTwoCharacters(String s) + { + String result = s.substring(s.length() - 2); + + return result; + } + + private static String getFirstCharacter(String s) + { + String result = s.substring(0, 1); + + return result; + } + + private static String getLastCharacter(String s) + { + String result = s.substring(s.length() - 1); + + return result; + } + + private static String getFirstTwoCharacters(String s) + { + String result = s.substring(0, 2); + + return result; + } +} From f309dc3e54c867872e5172c223da95ea3c701d6c Mon Sep 17 00:00:00 2001 From: feralgoon Date: Thu, 26 Apr 2018 09:30:58 -0500 Subject: [PATCH 20/34] Vending machine still broken --- ch08/Array.java | 18 +++++ ch08/ArrayDemo.java | 145 ++++++++++++++++++++++++++++++++++++++ ch08/Pets.java | 33 +++++++++ ch08/VendingMaching.java | 114 ++++++++++++++++++++++++++++++ ch08/VendingSoftware.java | 130 ++++++++++++++++++++++++++++++++++ 5 files changed, 440 insertions(+) create mode 100644 ch08/Array.java create mode 100644 ch08/ArrayDemo.java create mode 100644 ch08/Pets.java create mode 100644 ch08/VendingMaching.java create mode 100644 ch08/VendingSoftware.java diff --git a/ch08/Array.java b/ch08/Array.java new file mode 100644 index 0000000..e9f9a6d --- /dev/null +++ b/ch08/Array.java @@ -0,0 +1,18 @@ +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]); + } +} diff --git a/ch08/ArrayDemo.java b/ch08/ArrayDemo.java new file mode 100644 index 0000000..364e434 --- /dev/null +++ b/ch08/ArrayDemo.java @@ -0,0 +1,145 @@ +public class ArrayDemo +{ + public static void main(String[] args) + { + int[] nums1 = {1,5,9}; + int[] nums2 = {5,7,13}; + int[] nums3 = {5,8,21,19,2}; + int[] nums4 = {78,23,9,34}; + + int[] nums5 = new int[10]; + + nums5[0] = 4; + nums5[3] = 2; + nums5[9] = 4; + + double[] dubs1 = {34.2,18.0,12.5,13.1}; + double[] dubs2 = {10.0,15.0,20.0}; + + String[] words = new String[10]; + + words[0] = "Hi"; + words[3] = "Hello"; + words[9] = "Bye"; + + System.out.println("Printing array"); + printArray(nums1); + + int numsOneSum = arrayTotal(nums1); + int numsTwoSum = arrayTotal(nums2); + + System.out.println("Finding sums"); + System.out.println(numsOneSum); + System.out.println(numsTwoSum); + + int maxArrayOne = arrayMax(nums1); + int maxArrayTwo = arrayMax(nums2); + int maxArrayThr = arrayMax(nums3); + int maxArrayFou = arrayMax(nums4); + + System.out.println("Finding max values"); + System.out.println(maxArrayOne); + System.out.println(maxArrayTwo); + System.out.println(maxArrayThr); + System.out.println(maxArrayFou); + + int maxIndexOne = arrayMaxIndex(nums1); + int maxIndexTwo = arrayMaxIndex(nums2); + int maxIndexThr = arrayMaxIndex(nums3); + int maxIndexFou = arrayMaxIndex(nums4); + + System.out.println("Finding index of max values"); + System.out.println(maxIndexOne); + System.out.println(maxIndexTwo); + System.out.println(maxIndexTwo); + System.out.println(maxIndexFou); + + double dubOneAvg = arrayAverage(dubs1); + double dubTwoAvg = arrayAverage(dubs2); + + System.out.println("Gettig averages"); + System.out.println(dubOneAvg); + System.out.println(dubTwoAvg); + + System.out.println("Printing array"); + printArray(nums5); + + System.out.println("Printing String Array"); + printArray(words); + + } + + private static void printArray(int[] nums) + { + for (int i : nums) + { + System.out.println(i); + } + } + + private static int arrayTotal(int[] nums) + { + int sum = 0; + + for (int i : nums) + { + sum += i; + } + + return sum; + } + + private static int arrayMax(int[] nums) + { + int max = 0; + + for (int i : nums) + { + max = Math.max(max, i); + } + + return max; + } + + private static int arrayMaxIndex(int[] nums) + { + int index = 0; + int max = nums[0]; + + for(int i = 0; i < nums.length; i++) + { + if (nums[i] > max) + { + index = i; + } + } + + return index; + } + + private static double arrayAverage(double[] nums) + { + double sum = 0.0; + double avg; + + int count = 0; + + for(double i : nums) + { + sum += i; + count++; + } + + avg = sum/count; + + return avg; + } + + private static void printArray(String[] words) + { + for (String word : words) + { + System.out.println(word); + } + } +} diff --git a/ch08/Pets.java b/ch08/Pets.java new file mode 100644 index 0000000..29b082b --- /dev/null +++ b/ch08/Pets.java @@ -0,0 +1,33 @@ +import java.util.Scanner; + +public class Pets +{ + public static void main(String[] args) + { + Scanner scan = new Scanner(System.in); + int numOfPets = 0; + + System.out.println("How many pets do you have? (Enter zero or higher)"); + + do + { + numOfPets = scan.nextInt(); + scan.nextLine(); + } while (numOfPets < 0); + + String[] petNames = new String[numOfPets]; + + System.out.println("What are their names?"); + + for (int i = 0; i < petNames.length; i++) + { + petNames[i] = scan.nextLine(); + } + + System.out.println("Pet names are: "); + for (String name : petNames) + { + System.out.println(name); + } + } +} diff --git a/ch08/VendingMaching.java b/ch08/VendingMaching.java new file mode 100644 index 0000000..1374ecb --- /dev/null +++ b/ch08/VendingMaching.java @@ -0,0 +1,114 @@ +import java.util.Scanner; + +public class VendingMaching +{ + public static void main(String[] args) + { + String[] items = {"Freeze Dried Sushi", "Spock's Brain Blast", + "Alien Asparagus"}; + int[] sales = new int[items.length]; + Scanner scan = new Scanner(System.in); + + + printMenu(items); + getSelection(items, sales, scan); + + } + + private static void printMenu(String[] names) + { + System.out.println("Please select from: "); + + for (int i = 0; i < names.length; i++) + { + System.out.println(i + ")\t" + names[i]); + } + System.out.print("Your selection: "); + } + + private static void printSales(String[] items, int[] sold, int selection, boolean array) + { + if (array) + { + System.out.println("You chose multiple items."); + } + else + { + sold[selection]++; + + System.out.println(); + System.out.println("Thank you for choosing " + items[selection]); + } + System.out.println("Sold so far: "); + for (int i = 0; i < items.length; i++) + { + System.out.println(sold[i] + " of " + items[i]); + } + } + + private static void printFinalSales(String[] items, int[] sold) + { + System.out.println(); + System.out.println("Final Sales Totals: "); + for (int i = 0; i < items.length; i++) + { + System.out.println(sold[i] + " of " + items[i]); + } + System.out.println("Goodbye!"); + } + + private static void getSelection(String[] items, int[] sales, Scanner scan) + { + int choice = scan.nextInt(); + scan.nextLine(); + + int[] choiceArray = convertIntToArray(choice); + + if (choiceArray.length >= 2 && choiceArray[0] != 9 && choiceArray[1] != 9) + { + for(int i : sales) + { + if (choiceArray[i] == 9 && choiceArray[i+1] == 9) + { + printFinalSales(items, sales); + } + else + { + sales[i]++; + } + } + printSales(items, sales, choice, true); + printMenu(items); + getSelection(items,sales,scan); + } + else + { + if (choice >= 0 && choice < items.length) + { + printSales(items, sales, choice, false); + System.out.println(); + printMenu(items); + getSelection(items, sales, scan); + } else if (choice == 99) + { + printFinalSales(items, sales); + } else + { + getSelection(items, sales, scan); + } + } + } + + private static int[] convertIntToArray(int num) + { + String numValue = num + ""; + int[] result = new int[numValue.length()]; + + for(int i = 0; i < numValue.length(); i++) + { + result[i] = numValue.charAt(i); + } + + return result; + } +} diff --git a/ch08/VendingSoftware.java b/ch08/VendingSoftware.java new file mode 100644 index 0000000..b8240e9 --- /dev/null +++ b/ch08/VendingSoftware.java @@ -0,0 +1,130 @@ +import java.util.Scanner; + +public class VendingSoftware +{ + public static void main(String[] args) + { + String[] items = {"Freeze Dried Sushi", "Spock's Brain Blast", + "Alien Asparagus"}; + int[] sales = new int[items.length]; + Scanner scan = new Scanner(System.in); + String choice; + boolean keepGoing = true; + + + do + { + printMenu(items); + choice = scan.nextLine(); + + int[] choices = convertToArray(choice); + if (choices.length == 1) + { + printSales(items, sales, choices[0]); + } + else + { + for (int i = 0; i < choices.length - 1; i++) + { + if (choices[i] == 0) + { + sales[0]++; + } + else if (choices[i] == 1) + { + sales[1]++; + } + else if (choices[i] == 2) + { + sales[2]++; + } + else if (choices[i] == 9 && choices[i + 1] == 9) + { + keepGoing = false; + } + } + if (choices[0] == 0) + { + sales[0]++; + } + else if (choices[0] == 1) + { + sales[1]++; + } + else if (choices[0] == 2) + { + sales[2]++; + } + } + + } while (keepGoing); + + } + + private static void printMenu(String[] names) + { + System.out.println("Please select from: "); + + for (int i = 0; i < names.length; i++) + { + System.out.println(i + ")\t" + names[i]); + } + System.out.print("Your selection: "); + } + + private static void printFinalSales(String[] items, int[] sold) + { + System.out.println(); + System.out.println("Final Sales Totals: "); + for (int i = 0; i < items.length; i++) + { + System.out.println(sold[i] + " of " + items[i]); + } + System.out.println("Goodbye!"); + } + + private static void printSales(String[] items, int[] sold, int selection) + { + sold[selection]++; + + System.out.println(); + if (selection == -1) + { + System.out.println("Thank you for choosing multiple items!"); + } + else + { + System.out.println("Thank you for choosing " + items[selection]); + } + System.out.println("Sold so far: "); + for (int i = 0; i < items.length; i++) + { + System.out.println(sold[i] + " of " + items[i]); + } + } + + private static int[] convertToArray(int num) + { + String numValue = num + ""; + int[] result = new int[numValue.length()]; + + for (int i = 0; i < numValue.length(); i++) + { + result[i] = numValue.charAt(i); + } + + return result; + } + + private static int[] convertToArray(String str) + { + int[] result = new int[str.length()]; + + for (int i = 0; i < str.length(); i++) + { + result[i] = str.charAt(i); + } + + return result; + } +} From 24d59fdf1e7a034aa1f6ac1da176d4ccdf463678 Mon Sep 17 00:00:00 2001 From: feralgoon Date: Thu, 26 Apr 2018 15:43:54 -0500 Subject: [PATCH 21/34] vending machine done through gold, finally. --- ch08/VendingSoftware.java | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/ch08/VendingSoftware.java b/ch08/VendingSoftware.java index b8240e9..e65c2d8 100644 --- a/ch08/VendingSoftware.java +++ b/ch08/VendingSoftware.java @@ -57,8 +57,15 @@ else if (choices[0] == 2) } } + if (keepGoing) + { + printSales(items, sales, -1); + } + } while (keepGoing); + printFinalSales(items, sales); + } private static void printMenu(String[] names) @@ -85,15 +92,18 @@ private static void printFinalSales(String[] items, int[] sold) private static void printSales(String[] items, int[] sold, int selection) { - sold[selection]++; System.out.println(); if (selection == -1) { System.out.println("Thank you for choosing multiple items!"); } + else if (selection != 0 && selection != 1 && selection != 2) + { + } else { + sold[selection]++; System.out.println("Thank you for choosing " + items[selection]); } System.out.println("Sold so far: "); @@ -103,26 +113,13 @@ private static void printSales(String[] items, int[] sold, int selection) } } - private static int[] convertToArray(int num) - { - String numValue = num + ""; - int[] result = new int[numValue.length()]; - - for (int i = 0; i < numValue.length(); i++) - { - result[i] = numValue.charAt(i); - } - - return result; - } - private static int[] convertToArray(String str) { int[] result = new int[str.length()]; for (int i = 0; i < str.length(); i++) { - result[i] = str.charAt(i); + result[i] = Character.getNumericValue(str.charAt(i)); } return result; From 93fc8b7611861e8751d402535dd460b7b1d5bdfa Mon Sep 17 00:00:00 2001 From: feralgoon Date: Fri, 27 Apr 2018 13:59:54 -0500 Subject: [PATCH 22/34] Fixed case statement for converting phone number to printable format --- ch09/StringUtil.java | 67 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/ch09/StringUtil.java b/ch09/StringUtil.java index 4547bc5..882005c 100644 --- a/ch09/StringUtil.java +++ b/ch09/StringUtil.java @@ -1,5 +1,3 @@ -import java.util.Collections; - public class StringUtil { public static void main(String[] args) @@ -26,6 +24,7 @@ public static void main(String[] args) printPhoneNumber("234-666-8888"); printPhoneNumber("5015550100"); + printPhoneNumber("555JKL21A1"); System.out.println("findFirstE for 'Hello' is " + findFirstE("Hello")); System.out.println("findFirstE for 'Goodbye' is " + findFirstE("Goodbye")); @@ -102,7 +101,6 @@ private static boolean sameCount(String[] words) { boolean result = true; int[] counts = new int[words.length]; - int countIndex = 0; for (int i = 0; i < counts.length; i++) { @@ -263,8 +261,67 @@ private static int findFirstE(String s) return result; } + private static String convertPhoneNumbers(String s) + { + String result = ""; + + for(int i = 0; i < s.length(); i++) + { + switch (s.charAt(i)) + { + case 'A' : case 'B' : case 'C' : + result += "2"; + break; + + case 'D' : case 'E' : case 'F' : + result += "3"; + break; + + case 'G' : case 'H' : case 'I' : + result += "4"; + break; + + case 'J' : case 'K' : case 'L' : + result += "5"; + break; + + case 'M' : case 'N' : case 'O' : + result += "6"; + break; + + case 'P' : case 'Q' : case 'R' : case 'S' : + result += "7"; + break; + + case 'T' : case 'U' : case 'V' : + result += "8"; + break; + + case 'W' : case 'X' : case 'Y' : case 'Z' : + result += "9"; + break; + + default : + if (s.charAt(i) != '-') + { + result += s.charAt(i); + } + break; + } + } + + return result; + } + private static void printPhoneNumber(String s) { + String number = convertPhoneNumbers(s); + + System.out.println("Area Code: " + number.substring(0, 3) + + "\tExchange: " + number.substring(3, 6) + + "\tLine Number: " + number.substring(6)); + +/* if (s.length() == 10) { System.out.println("Area Code: " + s.substring(0, 3) + @@ -275,7 +332,7 @@ private static void printPhoneNumber(String s) System.out.println("Area Code: " + s.substring(0, 3) + "\tExchange: " + s.substring(4, 7) + "\tLine Number: " + s.substring(8)); - } + }*/ } private static void printCharacters(String s) @@ -320,4 +377,4 @@ private static String getFirstTwoCharacters(String s) return result; } -} +} \ No newline at end of file From e810e611277503402b34ca302d1e1814849b5075 Mon Sep 17 00:00:00 2001 From: feralgoon Date: Fri, 27 Apr 2018 14:56:29 -0500 Subject: [PATCH 23/34] ch. 11 exercises --- ch11/.idea/misc.xml | 6 ++++++ ch11/.idea/modules.xml | 8 +++++++ ch11/.idea/vcs.xml | 6 ++++++ ch11/Date.java | 48 ++++++++++++++++++++++++++++++++++++++++++ ch11/DateTester.java | 13 ++++++++++++ ch11/Planet.java | 14 ++++++++++++ ch11/PlanetTester.java | 22 +++++++++++++++++++ ch11/Player.java | 26 +++++++++++++++++++++++ ch11/PlayerTester.java | 25 ++++++++++++++++++++++ ch11/Time2.java | 41 ++++++++++++++++++++++++++++++++++++ ch11/Time2Tester.java | 18 ++++++++++++++++ ch11/ch11.iml | 11 ++++++++++ 12 files changed, 238 insertions(+) create mode 100644 ch11/.idea/misc.xml create mode 100644 ch11/.idea/modules.xml create mode 100644 ch11/.idea/vcs.xml 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/PlayerTester.java create mode 100644 ch11/Time2.java create mode 100644 ch11/Time2Tester.java create mode 100644 ch11/ch11.iml diff --git a/ch11/.idea/misc.xml b/ch11/.idea/misc.xml new file mode 100644 index 0000000..e208459 --- /dev/null +++ b/ch11/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ch11/.idea/modules.xml b/ch11/.idea/modules.xml new file mode 100644 index 0000000..6a1ca0a --- /dev/null +++ b/ch11/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/ch11/.idea/vcs.xml b/ch11/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/ch11/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ch11/Date.java b/ch11/Date.java new file mode 100644 index 0000000..ac55962 --- /dev/null +++ b/ch11/Date.java @@ -0,0 +1,48 @@ +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() + { + String formattedDate = ""; + + formattedDate += year + "-"; + + if (month < 10) + { + formattedDate += "0" + month + "-"; + } + else + { + formattedDate += month + "-"; + } + + formattedDate += day; + + return formattedDate; + } +} diff --git a/ch11/DateTester.java b/ch11/DateTester.java new file mode 100644 index 0000000..c653696 --- /dev/null +++ b/ch11/DateTester.java @@ -0,0 +1,13 @@ +public class DateTester +{ + public static void main(String[] args) + { + Date currentDate = new Date(27, 04, 2018); + + System.out.println("Day:\t" + currentDate.getDay()); + System.out.println("Month:\t" + currentDate.getMonth()); + System.out.println("Year:\t" + 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..b4915da --- /dev/null +++ b/ch11/PlanetTester.java @@ -0,0 +1,22 @@ +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 dwarfPluto = new Planet("Pluto"); + + Planet[] planets = {mercury, venus, earth, mars, jupiter, saturn, uranus, neptune, dwarfPluto}; + + for(int i = 0; i < planets.length; i++) + { + System.out.println(planets[i].getName()); + } + } +} diff --git a/ch11/Player.java b/ch11/Player.java new file mode 100644 index 0000000..50b7b5d --- /dev/null +++ b/ch11/Player.java @@ -0,0 +1,26 @@ +public class Player +{ + private String name; + private int score; + + public Player(String name) + { + this.name = name; + this.score = 0; + } + + public void increaseScore() + { + this.score++; + } + + public void resetScore() + { + this.score = 0; + } + + public int getScore() + { + return score; + } +} diff --git a/ch11/PlayerTester.java b/ch11/PlayerTester.java new file mode 100644 index 0000000..38651a5 --- /dev/null +++ b/ch11/PlayerTester.java @@ -0,0 +1,25 @@ +public class PlayerTester +{ + public static void main(String[] args) + { + Player stan = new Player("Stan"); + + System.out.println("Score: " + stan.getScore()); + + stan.increaseScore(); + + System.out.println("Score: " + stan.getScore()); + + stan.increaseScore(); + + System.out.println("Score: " + stan.getScore()); + + stan.increaseScore(); + + System.out.println("Score: " + stan.getScore()); + + stan.resetScore(); + + System.out.println("Score: " + stan.getScore()); + } +} diff --git a/ch11/Time2.java b/ch11/Time2.java new file mode 100644 index 0000000..bdcd149 --- /dev/null +++ b/ch11/Time2.java @@ -0,0 +1,41 @@ +public class Time2 +{ + private int hour; + private int minute; + + public Time2(int hour, int minute) + { + this.hour = hour; + this.minute = minute; + } + + public Time2(int hour, int minute, boolean pm) + { + if (pm) + { + this.hour = hour + 12; + this.minute = minute; + } + else + { + this.hour = hour; + this.minute = minute; + } + } + + public String getMilitaryTime() + { + return hour + ":" + minute; + } + + public String getTime() + { + if (hour > 12) + { + return (hour - 12) + ":" + minute + " PM"; + } else + { + return hour + ":" + minute + " AM"; + } + } +} diff --git a/ch11/Time2Tester.java b/ch11/Time2Tester.java new file mode 100644 index 0000000..aca4259 --- /dev/null +++ b/ch11/Time2Tester.java @@ -0,0 +1,18 @@ +public class Time2Tester +{ + public static void main(String[] args) + { + Time2 timeTest1 = new Time2(14, 32); + Time2 timeTest2 = new Time2(9,45, false); + + System.out.println(timeTest1.getMilitaryTime()); + System.out.println(timeTest2.getMilitaryTime()); + + System.out.println(timeTest1.getTime()); + System.out.println(timeTest2.getTime()); + + System.out.println(timeTest2.getTime()); + System.out.println(timeTest2.getTime()); + + } +} diff --git a/ch11/ch11.iml b/ch11/ch11.iml new file mode 100644 index 0000000..b107a2d --- /dev/null +++ b/ch11/ch11.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file From 780e8220d8b78d74af5e90e1012db41ec155696e Mon Sep 17 00:00:00 2001 From: feralgoon Date: Mon, 30 Apr 2018 10:26:55 -0500 Subject: [PATCH 24/34] fixed formatted date to pad day with a 0 if less than 10 --- ch11/Date.java | 9 ++++++++- ch11/DateTester.java | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/ch11/Date.java b/ch11/Date.java index ac55962..cd58cf2 100644 --- a/ch11/Date.java +++ b/ch11/Date.java @@ -41,7 +41,14 @@ public String getFormattedDate() formattedDate += month + "-"; } - formattedDate += day; + if (day < 10) + { + formattedDate += "0" + day; + } + else + { + formattedDate += day; + } return formattedDate; } diff --git a/ch11/DateTester.java b/ch11/DateTester.java index c653696..03e6c3f 100644 --- a/ch11/DateTester.java +++ b/ch11/DateTester.java @@ -2,7 +2,7 @@ public class DateTester { public static void main(String[] args) { - Date currentDate = new Date(27, 04, 2018); + Date currentDate = new Date(5, 04, 2018); System.out.println("Day:\t" + currentDate.getDay()); System.out.println("Month:\t" + currentDate.getMonth()); From 20deb8c72f47da819ceb34af5adbab6370bd14dc Mon Sep 17 00:00:00 2001 From: feralgoon Date: Mon, 30 Apr 2018 12:55:25 -0500 Subject: [PATCH 25/34] Finish Line Game completed. --- FinishLine/src/com/feralgoon/Board.java | 75 +++++++++++++++++++++ FinishLine/src/com/feralgoon/Die.java | 25 +++++++ FinishLine/src/com/feralgoon/DieTester.java | 15 +++++ FinishLine/src/com/feralgoon/Main.java | 17 +++++ FinishLine/src/com/feralgoon/Peg.java | 23 +++++++ FinishLine/src/com/feralgoon/PegTester.java | 13 ++++ 6 files changed, 168 insertions(+) create mode 100644 FinishLine/src/com/feralgoon/Board.java create mode 100644 FinishLine/src/com/feralgoon/Die.java create mode 100644 FinishLine/src/com/feralgoon/DieTester.java create mode 100644 FinishLine/src/com/feralgoon/Main.java create mode 100644 FinishLine/src/com/feralgoon/Peg.java create mode 100644 FinishLine/src/com/feralgoon/PegTester.java diff --git a/FinishLine/src/com/feralgoon/Board.java b/FinishLine/src/com/feralgoon/Board.java new file mode 100644 index 0000000..3a027cf --- /dev/null +++ b/FinishLine/src/com/feralgoon/Board.java @@ -0,0 +1,75 @@ +package com.feralgoon; + +public class Board +{ + private Peg pegOne; + private Peg pegTwo; + private Die dieOne; + private Die dieTwo; + + public Board(Peg pegOne, Peg pegTwo, Die dieOne, Die dieTwo) + { + this.pegOne = pegOne; + this.pegTwo = pegTwo; + this.dieOne = dieOne; + this.dieTwo = dieTwo; + + showBoard(); + } + + public void playGame() + { + while (pegOne.getPosition() != 10 && pegTwo.getPosition() != 10) + { + showBoard(); + nextTurn(); + } + + endGame(); + } + + + private void nextTurn() + { + dieOne.roll(); + dieTwo.roll(); + + System.out.println(); + System.out.println("Turn starting for pegOne. "); + System.out.println("Dice for pegOne are " + dieOne.getValue() + " and " + dieTwo.getValue()); + + if (dieOne.getValue() == pegOne.getPosition() + 1 || dieTwo.getValue() == pegOne.getPosition() + 1 || + dieOne.getValue() + dieTwo.getValue() == pegOne.getPosition() + 1) + { + pegOne.move(); + } + + dieOne.roll(); + dieTwo.roll(); + + if (dieOne.getValue() == pegTwo.getPosition() + 1 || dieTwo.getValue() == pegTwo.getPosition() + 1 || + dieOne.getValue() + dieTwo.getValue() == pegTwo.getPosition() + 1) + { + System.out.println(); + System.out.println("Turn starting for pegTwo. "); + System.out.println("Dice for pegTwo are " + dieOne.getValue() + " and " + dieTwo.getValue()); + pegTwo.move(); + } + + + } + + private void showBoard() + { + System.out.println(); + System.out.println("Peg one is at position " + pegOne.getPosition()); + System.out.println("Peg two is at position " + pegTwo.getPosition()); + } + + private void endGame() + { + System.out.println(); + System.out.println("Game over!"); + showBoard(); + } +} diff --git a/FinishLine/src/com/feralgoon/Die.java b/FinishLine/src/com/feralgoon/Die.java new file mode 100644 index 0000000..1e4cc28 --- /dev/null +++ b/FinishLine/src/com/feralgoon/Die.java @@ -0,0 +1,25 @@ +package com.feralgoon; + +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; + } +} diff --git a/FinishLine/src/com/feralgoon/DieTester.java b/FinishLine/src/com/feralgoon/DieTester.java new file mode 100644 index 0000000..2cfdfe3 --- /dev/null +++ b/FinishLine/src/com/feralgoon/DieTester.java @@ -0,0 +1,15 @@ +package com.feralgoon; + +public class DieTester +{ + public static void main(String[] args) + { + Die die = new Die(); + + System.out.println(die.getValue()); + + die.roll(); + + System.out.println(die.getValue()); + } +} diff --git a/FinishLine/src/com/feralgoon/Main.java b/FinishLine/src/com/feralgoon/Main.java new file mode 100644 index 0000000..fe47d3f --- /dev/null +++ b/FinishLine/src/com/feralgoon/Main.java @@ -0,0 +1,17 @@ +package com.feralgoon; + +public class Main +{ + + public static void main(String[] args) + { + Peg pegOne = new Peg(); + Peg pegTwo = new Peg(); + Die dieOne = new Die(); + Die dieTwo = new Die(); + + Board finishLine = new Board(pegOne, pegTwo, dieOne, dieTwo); + + finishLine.playGame(); + } +} diff --git a/FinishLine/src/com/feralgoon/Peg.java b/FinishLine/src/com/feralgoon/Peg.java new file mode 100644 index 0000000..c85b361 --- /dev/null +++ b/FinishLine/src/com/feralgoon/Peg.java @@ -0,0 +1,23 @@ +package com.feralgoon; + +public class Peg +{ + int position; + + public Peg() + { + position = 1; + } + + public void move() + + { + position++; + } + + public int getPosition() + + { + return position; + } +} diff --git a/FinishLine/src/com/feralgoon/PegTester.java b/FinishLine/src/com/feralgoon/PegTester.java new file mode 100644 index 0000000..937e52c --- /dev/null +++ b/FinishLine/src/com/feralgoon/PegTester.java @@ -0,0 +1,13 @@ +package com.feralgoon; + +public class PegTester +{ + public static void main(String[] args) + { + Peg peg = new Peg(); + + System.out.println(peg.getPosition()); + peg.move(); + System.out.println(peg.getPosition()); + } +} From 54998064d01ac3834f834382d9bbb910a9d6d76e Mon Sep 17 00:00:00 2001 From: feralgoon Date: Mon, 30 Apr 2018 13:03:55 -0500 Subject: [PATCH 26/34] Adjusted Board so that if peg one wins, the turn for peg two is not executed. --- FinishLine/src/com/feralgoon/Board.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/FinishLine/src/com/feralgoon/Board.java b/FinishLine/src/com/feralgoon/Board.java index 3a027cf..a7321e1 100644 --- a/FinishLine/src/com/feralgoon/Board.java +++ b/FinishLine/src/com/feralgoon/Board.java @@ -35,8 +35,8 @@ private void nextTurn() dieTwo.roll(); System.out.println(); - System.out.println("Turn starting for pegOne. "); - System.out.println("Dice for pegOne are " + dieOne.getValue() + " and " + dieTwo.getValue()); + System.out.println("Turn starting for peg one. "); + System.out.println("Dice for peg one are " + dieOne.getValue() + " and " + dieTwo.getValue()); if (dieOne.getValue() == pegOne.getPosition() + 1 || dieTwo.getValue() == pegOne.getPosition() + 1 || dieOne.getValue() + dieTwo.getValue() == pegOne.getPosition() + 1) @@ -47,13 +47,17 @@ private void nextTurn() dieOne.roll(); dieTwo.roll(); - if (dieOne.getValue() == pegTwo.getPosition() + 1 || dieTwo.getValue() == pegTwo.getPosition() + 1 || - dieOne.getValue() + dieTwo.getValue() == pegTwo.getPosition() + 1) + if (pegOne.getPosition() != 10) { System.out.println(); - System.out.println("Turn starting for pegTwo. "); - System.out.println("Dice for pegTwo are " + dieOne.getValue() + " and " + dieTwo.getValue()); - pegTwo.move(); + System.out.println("Turn starting for peg two. "); + System.out.println("Dice for peg two are " + dieOne.getValue() + " and " + dieTwo.getValue()); + + if (dieOne.getValue() == pegTwo.getPosition() + 1 || dieTwo.getValue() == pegTwo.getPosition() + 1 || + dieOne.getValue() + dieTwo.getValue() == pegTwo.getPosition() + 1) + { + pegTwo.move(); + } } From 0455c6241ac3168863ebda6166141df3d24fad1e Mon Sep 17 00:00:00 2001 From: feralgoon Date: Mon, 30 Apr 2018 13:59:19 -0500 Subject: [PATCH 27/34] added reset methods for Peg and Board, looped the main method to play game 100 times --- FinishLine/src/com/feralgoon/Board.java | 46 ++++++++++++++------- FinishLine/src/com/feralgoon/Main.java | 10 +++-- FinishLine/src/com/feralgoon/Peg.java | 35 ++++++++++++++-- FinishLine/src/com/feralgoon/PegTester.java | 2 +- 4 files changed, 71 insertions(+), 22 deletions(-) diff --git a/FinishLine/src/com/feralgoon/Board.java b/FinishLine/src/com/feralgoon/Board.java index a7321e1..d89807e 100644 --- a/FinishLine/src/com/feralgoon/Board.java +++ b/FinishLine/src/com/feralgoon/Board.java @@ -13,13 +13,14 @@ public Board(Peg pegOne, Peg pegTwo, Die dieOne, Die dieTwo) this.pegTwo = pegTwo; this.dieOne = dieOne; this.dieTwo = dieTwo; - - showBoard(); } public void playGame() { - while (pegOne.getPosition() != 10 && pegTwo.getPosition() != 10) + System.out.println("Starting Game"); + System.out.println(); + + while (!pegOne.hasWon() && !pegTwo.hasWon()) { showBoard(); nextTurn(); @@ -35,11 +36,11 @@ private void nextTurn() dieTwo.roll(); System.out.println(); - System.out.println("Turn starting for peg one. "); - System.out.println("Dice for peg one are " + dieOne.getValue() + " and " + dieTwo.getValue()); + System.out.println("Turn starting for peg " + pegOne.getName()); + System.out.println("Dice for peg " + pegOne.getName() + " are " + dieOne.getValue() + " and " + dieTwo.getValue()); - if (dieOne.getValue() == pegOne.getPosition() + 1 || dieTwo.getValue() == pegOne.getPosition() + 1 || - dieOne.getValue() + dieTwo.getValue() == pegOne.getPosition() + 1) + if (dieOne.getValue() == pegOne.getNextPosition() || dieTwo.getValue() == pegOne.getNextPosition() || + dieOne.getValue() + dieTwo.getValue() == pegOne.getNextPosition()) { pegOne.move(); } @@ -47,14 +48,14 @@ private void nextTurn() dieOne.roll(); dieTwo.roll(); - if (pegOne.getPosition() != 10) + if (!pegOne.hasWon()) { System.out.println(); - System.out.println("Turn starting for peg two. "); - System.out.println("Dice for peg two are " + dieOne.getValue() + " and " + dieTwo.getValue()); + System.out.println("Turn starting for peg " + pegTwo.getName()); + System.out.println("Dice for peg " + pegTwo.getName() + " are " + dieOne.getValue() + " and " + dieTwo.getValue()); - if (dieOne.getValue() == pegTwo.getPosition() + 1 || dieTwo.getValue() == pegTwo.getPosition() + 1 || - dieOne.getValue() + dieTwo.getValue() == pegTwo.getPosition() + 1) + if (dieOne.getValue() == pegTwo.getNextPosition() || dieTwo.getValue() == pegTwo.getNextPosition() || + dieOne.getValue() + dieTwo.getValue() == pegTwo.getNextPosition()) { pegTwo.move(); } @@ -66,8 +67,8 @@ private void nextTurn() private void showBoard() { System.out.println(); - System.out.println("Peg one is at position " + pegOne.getPosition()); - System.out.println("Peg two is at position " + pegTwo.getPosition()); + System.out.println("Peg " + pegOne.getName() + " is at position " + pegOne.getPosition()); + System.out.println("Peg " + pegTwo.getName() + " is at position " + pegTwo.getPosition()); } private void endGame() @@ -75,5 +76,22 @@ private void endGame() System.out.println(); System.out.println("Game over!"); showBoard(); + System.out.println(); + + if (pegOne.hasWon()) + { + System.out.println(pegOne.getName() + " has won!"); + } + else + { + System.out.println(pegTwo.getName() + " has won!"); + + } + } + + public void reset() + { + pegOne.reset(); + pegTwo.reset(); } } diff --git a/FinishLine/src/com/feralgoon/Main.java b/FinishLine/src/com/feralgoon/Main.java index fe47d3f..1e2866f 100644 --- a/FinishLine/src/com/feralgoon/Main.java +++ b/FinishLine/src/com/feralgoon/Main.java @@ -5,13 +5,17 @@ public class Main public static void main(String[] args) { - Peg pegOne = new Peg(); - Peg pegTwo = new Peg(); + Peg pegOne = new Peg("Red"); + Peg pegTwo = new Peg("Blue"); Die dieOne = new Die(); Die dieTwo = new Die(); Board finishLine = new Board(pegOne, pegTwo, dieOne, dieTwo); - finishLine.playGame(); + for(int i = 0; i < 100; i++) + { + finishLine.playGame(); + finishLine.reset(); + } } } diff --git a/FinishLine/src/com/feralgoon/Peg.java b/FinishLine/src/com/feralgoon/Peg.java index c85b361..28d1f79 100644 --- a/FinishLine/src/com/feralgoon/Peg.java +++ b/FinishLine/src/com/feralgoon/Peg.java @@ -3,21 +3,48 @@ public class Peg { int position; + String name; - public Peg() + public Peg(String name) { - position = 1; + reset(); + this.name = name; } public void move() - { position++; } public int getPosition() - { return position; } + + public int getNextPosition() + { + return position + 1; + } + + public String getName() + { + return name; + } + + public boolean hasWon() + { + if (position == 10) + { + return true; + } + else + { + return false; + } + } + + public void reset() + { + position = 1; + } } diff --git a/FinishLine/src/com/feralgoon/PegTester.java b/FinishLine/src/com/feralgoon/PegTester.java index 937e52c..6bee213 100644 --- a/FinishLine/src/com/feralgoon/PegTester.java +++ b/FinishLine/src/com/feralgoon/PegTester.java @@ -4,7 +4,7 @@ public class PegTester { public static void main(String[] args) { - Peg peg = new Peg(); + Peg peg = new Peg("Red"); System.out.println(peg.getPosition()); peg.move(); From 458366d713b3fd21aa2566bd3828b0995aea99d1 Mon Sep 17 00:00:00 2001 From: feralgoon Date: Mon, 30 Apr 2018 15:57:53 -0500 Subject: [PATCH 28/34] Roundabout Game completed --- Roundabout/src/com/feralgoon/Board.java | 77 +++++++++++++++++++++++++ Roundabout/src/com/feralgoon/Die.java | 24 ++++++++ Roundabout/src/com/feralgoon/Main.java | 36 ++++++++++++ Roundabout/src/com/feralgoon/Peg.java | 50 ++++++++++++++++ 4 files changed, 187 insertions(+) create mode 100644 Roundabout/src/com/feralgoon/Board.java create mode 100644 Roundabout/src/com/feralgoon/Die.java create mode 100644 Roundabout/src/com/feralgoon/Main.java create mode 100644 Roundabout/src/com/feralgoon/Peg.java diff --git a/Roundabout/src/com/feralgoon/Board.java b/Roundabout/src/com/feralgoon/Board.java new file mode 100644 index 0000000..d4bccfd --- /dev/null +++ b/Roundabout/src/com/feralgoon/Board.java @@ -0,0 +1,77 @@ +package com.feralgoon; + +public class Board +{ + private Peg pegOne; + private Peg pegTwo; + private Die dieOne; + private Die dieTwo; + + public Board(Peg pegOne, Peg pegTwo, Die dieOne, Die dieTwo) + { + this.pegOne = pegOne; + this.pegTwo = pegTwo; + this.dieOne = dieOne; + this.dieTwo = dieTwo; + } + + public void startGame() + { + System.out.println("Welcome to Roundabout!"); + System.out.println("----------------------"); + showBoard(); + } + + public void reset() + { + pegOne.reset(); + pegTwo.reset(); + } + + public void nextTurn(Peg peg, Peg otherPeg) + { + dieOne.roll(); + dieTwo.roll(); + + System.out.println(); + System.out.println("Dice rolls for peg " + peg.getName() + " are " + + dieOne.getValue() + " and " + dieTwo.getValue() + "."); + + if (dieOne.getValue() == peg.getNextPosition() || + dieTwo.getValue() == peg.getNextPosition() || + dieOne.getValue() + dieTwo.getValue() == peg.getNextPosition()) + { + peg.move(); + } + + if (peg.getPosition() == otherPeg.getPosition()) + { + otherPeg.reset(); + } + + showBoard(); + } + + public void endGame() + { + System.out.println(); + System.out.println("Game over!"); + System.out.println(); + + if (pegOne.hasWon()) + { + System.out.println("Peg " + pegOne.getName() + " has won!"); + } + else + { + System.out.println("Peg " + pegTwo.getName() + " has won!"); + } + } + + private void showBoard() + { + System.out.println(); + System.out.println("Peg " + pegOne.getName() + " is at position " + pegOne.getPosition()); + System.out.println("Peg " + pegTwo.getName() + " is at position " + pegTwo.getPosition()); + } +} diff --git a/Roundabout/src/com/feralgoon/Die.java b/Roundabout/src/com/feralgoon/Die.java new file mode 100644 index 0000000..871c237 --- /dev/null +++ b/Roundabout/src/com/feralgoon/Die.java @@ -0,0 +1,24 @@ +package com.feralgoon; + +import java.util.Random; + +public class Die +{ + private int value; + private Random number = new Random(); + + public Die() + { + roll(); + } + + public int getValue() + { + return value; + } + + public void roll() + { + value = number.nextInt(6) + 1; + } +} diff --git a/Roundabout/src/com/feralgoon/Main.java b/Roundabout/src/com/feralgoon/Main.java new file mode 100644 index 0000000..05911f6 --- /dev/null +++ b/Roundabout/src/com/feralgoon/Main.java @@ -0,0 +1,36 @@ +package com.feralgoon; + +public class Main +{ + + public static void main(String[] args) + { + Peg pegOne = new Peg("Red"); + Peg pegTwo = new Peg("Blue"); + Die dieOne = new Die(); + Die dieTwo = new Die(); + + Board roundabout = new Board(pegOne, pegTwo, dieOne, dieTwo); + + roundabout.startGame(); + + while (true) + { + roundabout.nextTurn(pegOne, pegTwo); + + if (pegOne.hasWon()) + { + break; + } + + roundabout.nextTurn(pegTwo, pegOne); + + if (pegTwo.hasWon()) + { + break; + } + } + + roundabout.endGame(); + } +} diff --git a/Roundabout/src/com/feralgoon/Peg.java b/Roundabout/src/com/feralgoon/Peg.java new file mode 100644 index 0000000..952bf7c --- /dev/null +++ b/Roundabout/src/com/feralgoon/Peg.java @@ -0,0 +1,50 @@ +package com.feralgoon; + +public class Peg +{ + private int position; + private String name; + + public Peg(String name) + { + position = 0; + this.name = name; + } + + public void move() + { + position++; + } + + public void reset() + { + position = 0; + } + + public int getPosition() + { + return position; + } + + public int getNextPosition() + { + if (position == 0) + { + return 5; + } + else + { + return position + 1; + } + } + + public String getName() + { + return name; + } + + public boolean hasWon() + { + return position >= 12; + } +} From b0d3af69b178707f80911221de31fe5412745854 Mon Sep 17 00:00:00 2001 From: feralgoon Date: Mon, 30 Apr 2018 15:58:21 -0500 Subject: [PATCH 29/34] Basketball Game completed --- Basketball/src/com/feralgoon/Board.java | 113 ++++++++++++++++++++++++ Basketball/src/com/feralgoon/Die.java | 24 +++++ Basketball/src/com/feralgoon/Main.java | 42 +++++++++ Basketball/src/com/feralgoon/Peg.java | 43 +++++++++ 4 files changed, 222 insertions(+) create mode 100644 Basketball/src/com/feralgoon/Board.java create mode 100644 Basketball/src/com/feralgoon/Die.java create mode 100644 Basketball/src/com/feralgoon/Main.java create mode 100644 Basketball/src/com/feralgoon/Peg.java diff --git a/Basketball/src/com/feralgoon/Board.java b/Basketball/src/com/feralgoon/Board.java new file mode 100644 index 0000000..58d59ac --- /dev/null +++ b/Basketball/src/com/feralgoon/Board.java @@ -0,0 +1,113 @@ +package com.feralgoon; + +public class Board +{ + private Peg pegOne; + private Peg pegTwo; + private Die dieOne; + private Die dieTwo; + + public Board(Peg pegOne, Peg pegTwo, Die dieOne, Die dieTwo) + { + this.pegOne = pegOne; + this.pegTwo = pegTwo; + this.dieOne = dieOne; + this.dieTwo = dieTwo; + } + + public void startGame() + { + System.out.println("Welcome to Basketball!"); + System.out.println("----------------------"); + showBoard(); + } + + public void reset() + { + pegOne.reset(); + pegTwo.reset(); + } + + public void nextTurn(Peg peg) + { + dieOne.roll(); + dieTwo.roll(); + + peg.move(findPointValue(dieOne.getValue() + dieTwo.getValue())); + + showBoard(); + } + + public void endGame() + { + System.out.println(); + System.out.println("Game over!"); + System.out.println(); + + if (pegOne.hasWon()) + { + System.out.println("Peg " + pegOne.getName() + " has won!"); + } + else + { + System.out.println("Peg " + pegTwo.getName() + " has won!"); + } + } + + private void showBoard() + { + System.out.println(); + System.out.println("Peg " + pegOne.getName() + " has " + pegOne.getPosition() + " points."); + System.out.println("Peg " + pegTwo.getName() + " has " + pegTwo.getPosition() + " points."); + } + + private int findPointValue(int value) + { + int result; + + switch (value) + { + case 2: case 12: + System.out.println("Three point shot!"); + result = 3; + break; + case 3: + System.out.println("Walking violation!"); + result = 0; + break; + case 4: case 6: + System.out.println("Two point shot!"); + result = 2; + break; + case 5: + System.out.println("Foul Shot! One point!"); + result = 1; + break; + case 7: + System.out.println("Double Dribble!"); + result = 0; + break; + case 8: + System.out.println("Two foul shots! Two points!"); + result = 2; + break; + case 9: + System.out.println("Missed shot!"); + result = 0; + break; + case 10: + System.out.println("Three foul shots! Three Points!"); + result = 3; + break; + case 11: + System.out.println("Offensive Foul!"); + result = 0; + break; + default: + result = 0; + break; + } + + return result; + } +} diff --git a/Basketball/src/com/feralgoon/Die.java b/Basketball/src/com/feralgoon/Die.java new file mode 100644 index 0000000..871c237 --- /dev/null +++ b/Basketball/src/com/feralgoon/Die.java @@ -0,0 +1,24 @@ +package com.feralgoon; + +import java.util.Random; + +public class Die +{ + private int value; + private Random number = new Random(); + + public Die() + { + roll(); + } + + public int getValue() + { + return value; + } + + public void roll() + { + value = number.nextInt(6) + 1; + } +} diff --git a/Basketball/src/com/feralgoon/Main.java b/Basketball/src/com/feralgoon/Main.java new file mode 100644 index 0000000..ed9de3a --- /dev/null +++ b/Basketball/src/com/feralgoon/Main.java @@ -0,0 +1,42 @@ +package com.feralgoon; + +public class Main +{ + + public static void main(String[] args) + { + Peg pegOne = new Peg("Red"); + Peg pegTwo = new Peg("Blue"); + Die dieOne = new Die(); + Die dieTwo = new Die(); + + Board basketball = new Board(pegOne,pegTwo,dieOne,dieTwo); + + basketball.startGame(); + + while(true) + { + System.out.println(); + System.out.println("Peg " + pegOne.getName() + " turn!"); + + basketball.nextTurn(pegOne); + + if (pegOne.hasWon()) + { + break; + } + + System.out.println(); + System.out.println("Peg " + pegTwo.getName() + " turn!"); + + basketball.nextTurn((pegTwo)); + + if (pegTwo.hasWon()) + { + break; + } + } + + basketball.endGame(); + } +} diff --git a/Basketball/src/com/feralgoon/Peg.java b/Basketball/src/com/feralgoon/Peg.java new file mode 100644 index 0000000..5c264d1 --- /dev/null +++ b/Basketball/src/com/feralgoon/Peg.java @@ -0,0 +1,43 @@ +package com.feralgoon; + +public class Peg +{ + private int position; + private String name; + + public Peg(String name) + { + position = 0; + this.name = name; + } + + public void move(int value) + { + position += value; + } + + public void reset() + { + position = 0; + } + + public int getPosition() + { + return position; + } + + public int getNextPosition() + { + return position + 1; + } + + public String getName() + { + return name; + } + + public boolean hasWon() + { + return position >= 21; + } +} From 1a367a20f2aad6d8d32800666d3ba7ae8cea9c28 Mon Sep 17 00:00:00 2001 From: feralgoon Date: Thu, 3 May 2018 12:48:27 -0500 Subject: [PATCH 30/34] Enum Practice --- .../src/com/feralgoon/BrowserTest.java | 19 +++++++++++++++ .../src/com/feralgoon/BrowserType.java | 23 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 BrowserTest/src/com/feralgoon/BrowserTest.java create mode 100644 BrowserTest/src/com/feralgoon/BrowserType.java diff --git a/BrowserTest/src/com/feralgoon/BrowserTest.java b/BrowserTest/src/com/feralgoon/BrowserTest.java new file mode 100644 index 0000000..aafd725 --- /dev/null +++ b/BrowserTest/src/com/feralgoon/BrowserTest.java @@ -0,0 +1,19 @@ +package com.feralgoon; + +public class BrowserTest +{ + public static void main(String[] args) + { + BrowserTest browserTest = new BrowserTest(); + + browserTest.demo(); + } + + private void demo() + { + for(BrowserType i : BrowserType.values()) + { + System.out.println(i + ", by " + i.getCorporateOwner()); + } + } +} diff --git a/BrowserTest/src/com/feralgoon/BrowserType.java b/BrowserTest/src/com/feralgoon/BrowserType.java new file mode 100644 index 0000000..91e55fe --- /dev/null +++ b/BrowserTest/src/com/feralgoon/BrowserType.java @@ -0,0 +1,23 @@ +package com.feralgoon; + +public enum BrowserType +{ + CHROME("Google"), + OPERA("Opera Software"), + SAFARI("Apple"), + INTERNET_EXPLORER("Microsoft"), + EDGE("Microsoft"), + FIREFOX("Mozilla"); + + private String corporateOwner; + + BrowserType(String corporateOwner) + { + this.corporateOwner = corporateOwner; + } + + public String getCorporateOwner() + { + return corporateOwner; + } +} \ No newline at end of file From 4819205d4ff0b2f5e5ac6ac1826d1ffb8fbce94c Mon Sep 17 00:00:00 2001 From: feralgoon Date: Mon, 7 May 2018 14:18:32 -0500 Subject: [PATCH 31/34] Bingo game tracker complete --- Sets/src/com/feralgoon/BingoNumbers.java | 255 +++++++++++++++++++++++ Sets/src/com/feralgoon/SimpleSet.java | 31 +++ 2 files changed, 286 insertions(+) create mode 100644 Sets/src/com/feralgoon/BingoNumbers.java create mode 100644 Sets/src/com/feralgoon/SimpleSet.java diff --git a/Sets/src/com/feralgoon/BingoNumbers.java b/Sets/src/com/feralgoon/BingoNumbers.java new file mode 100644 index 0000000..5055c04 --- /dev/null +++ b/Sets/src/com/feralgoon/BingoNumbers.java @@ -0,0 +1,255 @@ +package com.feralgoon; + +import java.util.HashSet; +import java.util.Random; +import java.util.Scanner; +import java.util.Set; + +public class BingoNumbers +{ + private String[] choices; + Set bingoNums = new HashSet<>(75); + + private void run() + { + Scanner scan = new Scanner(System.in); + String choice; + + printHeader(); + + System.out.println(bingoNums); + + do + { + printOptions(); + choice = scan.nextLine(); + handleChoices(choice); + + } while (!choices[0].equalsIgnoreCase("Exit")); + + } + + private void handleChoices(String choice) + { + choices = choice.split(" "); + + try + { + if (choices[0].equalsIgnoreCase("Call")) + { + call(); + } + else if (choices[0].equalsIgnoreCase("Called")) + { + calledNums(); + } + else if (choices[0].equalsIgnoreCase("ToGo")) + { + toGo(); + } + else if (choices[0].equalsIgnoreCase("Verify")) + { + verifyNums(); + } + else if (choices[0].equalsIgnoreCase("Challenge")) + { + challenge(); + } + else if (choices[0].equalsIgnoreCase("Bingo")) + { + bingo(); + } + } catch (Exception e) + { + System.out.println("Invalid Entry."); + System.out.println(e.getLocalizedMessage()); + System.out.println(); + } + } + + private String appendLetter(int num) + { + String result = ""; + + if (num <= 15) + { + result += "B"; + } + else if (num <= 30) + { + result += "I"; + } + else if (num <= 45) + { + result += "N"; + } + else if (num <= 60) + { + result += "G"; + } + else if (num <= 75) + { + result += "O"; + } + + result += num; + return result; + } + + private void toGo() + { + Set numsToGo = new HashSet<>(75); + + for(int i = 1; i <= 75; i++) + { + numsToGo.add(i); + } + + numsToGo.removeAll(bingoNums); + + System.out.println("The following numbers remain: "); + for(int i : numsToGo) + { + System.out.println(appendLetter(i)); + } + } + + private void bingo() + { + if (bingoNums.size() < 5) + { + System.out.println("CHEATER!"); + } + else + { + System.out.println("Congratulations, you win an fruit cake!"); + } + bingoNums.clear(); + + } + + private void challenge() + { + int num = Integer.parseInt(choices[1]); + + if (num < 1 || num > 75) + { + throw new IllegalArgumentException("Number to challenge must be between 1 and 75."); + } + else if (bingoNums.remove(num)) + { + System.out.println(appendLetter(num) + " was removed from the called list."); + } + else + { + System.out.println(appendLetter(num) + " hasn't been called!"); + } + } + + private void verifyNums() + { + int num = Integer.parseInt(choices[1]); + System.out.println(); + + if (num < 1 || num > 75) + { + throw new IllegalArgumentException("Number to verify must be between 1 and 75."); + } + else if (bingoNums.contains(num)) + { + System.out.println(appendLetter(num) + " has been called."); + } + else + { + System.out.println(appendLetter(num) + " has NOT been called."); + } + + } + + private void calledNums() + { + System.out.println(); + if (bingoNums.size() == 0) + { + System.out.println("No numbers have been called."); + } + else + { + System.out.println("The following numbers have been called: "); + + for(int i : bingoNums) + { + System.out.println(appendLetter(i)); + } + } + } + + private void call() + { + System.out.println(); + + if (choices.length == 1) + { + Random randNum = new Random(); + int randomNumber = randNum.nextInt(74) + 1; + + while(bingoNums.contains(randomNumber)) + { + randomNumber = randNum.nextInt(74) + 1; + } + + bingoNums.add(randomNumber); + System.out.println("Called " + appendLetter(randomNumber)); + } + else + { + int num = Integer.parseInt(choices[1]); + + if (num < 1 || num > 75) + { + throw new IllegalArgumentException("Called number must be between 1 and 75."); + } + else if (bingoNums.add(num)) + { + System.out.println("Called " + appendLetter(num)); + } + else + { + System.out.println("Number already called!"); + } + } + } + + private void printOptions() + { + System.out.println(); + System.out.println("Choose from the following: "); + System.out.println(); + System.out.println("Call <1-75>"); + System.out.println("Called"); + System.out.println("ToGo"); + System.out.println("Verify <1-75>"); + System.out.println("Challenge <1-75>"); + System.out.println("Bingo"); + System.out.println("Exit"); + System.out.println(); + System.out.print("Enter Selection: ---> "); + } + + private void printHeader() + { + System.out.println(); + System.out.println("======================="); + System.out.println("|| Welcome to Bingo! ||"); + System.out.println("======================="); + System.out.println(); + } + + + public static void main(String[] args) + { + BingoNumbers bingoNumbers = new BingoNumbers(); + + bingoNumbers.run(); + } +} diff --git a/Sets/src/com/feralgoon/SimpleSet.java b/Sets/src/com/feralgoon/SimpleSet.java new file mode 100644 index 0000000..5fce650 --- /dev/null +++ b/Sets/src/com/feralgoon/SimpleSet.java @@ -0,0 +1,31 @@ +package com.feralgoon; + +import java.util.HashSet; +import java.util.Set; + +public class SimpleSet +{ + private void run() + { + Set mySet = new HashSet<>(); + + mySet.add(1); + mySet.add(2); + mySet.add(3); + mySet.add(4); + mySet.add(5); + + System.out.println(mySet); + + mySet.add(3); + + System.out.println(mySet); + } + + public static void main(String[] args) + { + SimpleSet simpleSet = new SimpleSet(); + + simpleSet.run(); + } +} From 82dbc0561e77a4728ea2496149e4b473670aeaa4 Mon Sep 17 00:00:00 2001 From: feralgoon Date: Mon, 7 May 2018 14:22:04 -0500 Subject: [PATCH 32/34] Changed bingo method to only clear called numbers if there were at least five numbers already called. --- Sets/src/com/feralgoon/BingoNumbers.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sets/src/com/feralgoon/BingoNumbers.java b/Sets/src/com/feralgoon/BingoNumbers.java index 5055c04..c849da7 100644 --- a/Sets/src/com/feralgoon/BingoNumbers.java +++ b/Sets/src/com/feralgoon/BingoNumbers.java @@ -123,8 +123,8 @@ private void bingo() else { System.out.println("Congratulations, you win an fruit cake!"); + bingoNums.clear(); } - bingoNums.clear(); } From a78e4270abfe125746836bdf7904f46f4186615f Mon Sep 17 00:00:00 2001 From: feralgoon Date: Tue, 8 May 2018 09:18:29 -0500 Subject: [PATCH 33/34] Minor updates to improve stability --- Sets/src/com/feralgoon/BingoNumbers.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Sets/src/com/feralgoon/BingoNumbers.java b/Sets/src/com/feralgoon/BingoNumbers.java index c849da7..a2f966d 100644 --- a/Sets/src/com/feralgoon/BingoNumbers.java +++ b/Sets/src/com/feralgoon/BingoNumbers.java @@ -8,7 +8,7 @@ public class BingoNumbers { private String[] choices; - Set bingoNums = new HashSet<>(75); + private Set bingoNums = new HashSet<>(75); private void run() { @@ -193,13 +193,21 @@ private void call() Random randNum = new Random(); int randomNumber = randNum.nextInt(74) + 1; - while(bingoNums.contains(randomNumber)) + if (bingoNums.size() == 74) { - randomNumber = randNum.nextInt(74) + 1; + System.out.println("All numbers have been called!"); } + else + { + while(bingoNums.contains(randomNumber)) + { + randomNumber = randNum.nextInt(74) + 1; - bingoNums.add(randomNumber); - System.out.println("Called " + appendLetter(randomNumber)); + } + + bingoNums.add(randomNumber); + System.out.println("Called " + appendLetter(randomNumber)); + } } else { From 57bc26507d8c4e60966564a0bb8e39d49eaed4d2 Mon Sep 17 00:00:00 2001 From: feralgoon Date: Tue, 8 May 2018 12:53:08 -0500 Subject: [PATCH 34/34] Queues and Stacks exercises. --- .../src/com/feralgoon/EmergencyTracker.java | 116 ++++++++++++ .../src/com/feralgoon/SimpleQueue.java | 38 ++++ .../src/com/feralgoon/SimpleStack.java | 35 ++++ .../src/com/feralgoon/TaskHelper.java | 170 ++++++++++++++++++ 4 files changed, 359 insertions(+) create mode 100644 QueuesAndStacks/src/com/feralgoon/EmergencyTracker.java create mode 100644 QueuesAndStacks/src/com/feralgoon/SimpleQueue.java create mode 100644 QueuesAndStacks/src/com/feralgoon/SimpleStack.java create mode 100644 QueuesAndStacks/src/com/feralgoon/TaskHelper.java diff --git a/QueuesAndStacks/src/com/feralgoon/EmergencyTracker.java b/QueuesAndStacks/src/com/feralgoon/EmergencyTracker.java new file mode 100644 index 0000000..0b12eeb --- /dev/null +++ b/QueuesAndStacks/src/com/feralgoon/EmergencyTracker.java @@ -0,0 +1,116 @@ +package com.feralgoon; + +import java.util.Scanner; +import java.util.Stack; + +public class EmergencyTracker +{ + private Stack emergencies = new Stack<>(); + + public void run() + { + Scanner scan = new Scanner(System.in); + String choice; + + printHeader(); + + do + { + printOptions(); + + System.out.print("Enter command ---> "); + choice = scan.nextLine(); + handleChoice(choice); + + }while (!choice.equalsIgnoreCase("EXIT")); + + } + + private void handleChoice(String choice) + { + String[] splitChoice = choice.split(" "); + + if (splitChoice[0].equalsIgnoreCase("Add")) + { + add(splitChoice); + } + else if (splitChoice[0].equalsIgnoreCase("Peek")) + { + peek(); + } + else if (splitChoice[0].equalsIgnoreCase("Remove")) + { + remove(); + } + else if (splitChoice[0].equalsIgnoreCase("HowMany")) + { + howMany(); + } + else if (splitChoice[0].equalsIgnoreCase("Panic")) + { + panic(); + } + } + + private void panic() + { + System.out.println("Packing bag..."); + System.out.println("Clearing emergency list..."); + System.out.println("Ready to flee country!"); + } + + private void howMany() + { + System.out.println("There are " + emergencies.size() + " emergencies left."); + } + + private void remove() + { + String removed = emergencies.pop(); + System.out.println("Removed " + removed + " from the list."); + System.out.println("Next task is: " + emergencies.peek()); + } + + private void peek() + { + System.out.println(emergencies.peek()); + } + + private void add(String[] taskList) + { + if (taskList.length < 2) + { + System.out.println("Error: No emergency specified."); + } + else + { + this.emergencies.add(taskList[1]); + } + } + + private void printOptions() + { + System.out.println(); + System.out.println("Options: "); + System.out.println(); + System.out.println("Add "); + System.out.println("Peek"); + System.out.println("Remove"); + System.out.println("HowMany"); + System.out.println("Panic"); + System.out.println("Exit"); + System.out.println(); + } + + private void printHeader() + { + System.out.println("===================="); + System.out.println("|| Emergency List ||"); + System.out.println("===================="); + } + public static void main(String[] args) + { + EmergencyTracker emergencyTracker = new EmergencyTracker(); + emergencyTracker.run(); + } +} diff --git a/QueuesAndStacks/src/com/feralgoon/SimpleQueue.java b/QueuesAndStacks/src/com/feralgoon/SimpleQueue.java new file mode 100644 index 0000000..d27cc63 --- /dev/null +++ b/QueuesAndStacks/src/com/feralgoon/SimpleQueue.java @@ -0,0 +1,38 @@ +package com.feralgoon; + +import java.util.LinkedList; +import java.util.Queue; + +public class SimpleQueue +{ + public 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); + + String removed = myQueue.peek(); + System.out.println(removed); + + System.out.println(myQueue); + + System.out.println(myQueue.peek()); + System.out.println(myQueue.peek()); + System.out.println(myQueue.peek()); + System.out.println(myQueue.peek()); + + myQueue.peek(); + } + + public static void main(String[] args) + { + SimpleQueue simpleQueue = new SimpleQueue(); + simpleQueue.run(); + } +} diff --git a/QueuesAndStacks/src/com/feralgoon/SimpleStack.java b/QueuesAndStacks/src/com/feralgoon/SimpleStack.java new file mode 100644 index 0000000..5a00857 --- /dev/null +++ b/QueuesAndStacks/src/com/feralgoon/SimpleStack.java @@ -0,0 +1,35 @@ +package com.feralgoon; + +import java.util.LinkedList; +import java.util.Stack; + +public class SimpleStack +{ + public void run() + { + Stack myStack = new Stack<>(); + + myStack.push("One"); + myStack.push("Two"); + myStack.push("Three"); + myStack.push("Four"); + myStack.push("Five"); + + System.out.println(myStack); + + System.out.println(myStack.peek()); + System.out.println(myStack); + + System.out.println(myStack.peek()); + System.out.println(myStack.peek()); + System.out.println(myStack.peek()); + System.out.println(myStack.peek()); + + } + + public static void main(String[] args) + { + SimpleStack simpleStack = new SimpleStack(); + simpleStack.run(); + } +} diff --git a/QueuesAndStacks/src/com/feralgoon/TaskHelper.java b/QueuesAndStacks/src/com/feralgoon/TaskHelper.java new file mode 100644 index 0000000..4cb67e8 --- /dev/null +++ b/QueuesAndStacks/src/com/feralgoon/TaskHelper.java @@ -0,0 +1,170 @@ +package com.feralgoon; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Scanner; + +public class TaskHelper +{ + private Queue taskList = new LinkedList<>(); + + public void run() + { + Scanner scan = new Scanner(System.in); + String choice; + + printHeader(); + + do + { + printOptions(); + + System.out.print("Enter command ---> "); + choice = scan.nextLine(); + handleChoice(choice); + + } while (!choice.equalsIgnoreCase("EXIT")); + + } + + private void handleChoice(String choice) + { + String[] splitChoice = choice.split(" "); + + if (splitChoice[0].equalsIgnoreCase("Add")) + { + add(splitChoice); + } + else if (splitChoice[0].equalsIgnoreCase("Peek")) + { + peek(splitChoice); + } + else if (splitChoice[0].equalsIgnoreCase("Remove")) + { + remove(splitChoice); + } + else if (splitChoice[0].equalsIgnoreCase("HowMany")) + { + howMany(); + } + else if (splitChoice[0].equalsIgnoreCase("Flee")) + { + flee(); + } + } + + private void flee() + { + System.out.println("Packing bag..."); + System.out.println("Clearing task list..."); + System.out.println("Ready to flee country!"); + } + + private void howMany() + { + System.out.println("There are " + taskList.size() + " tasks left."); + } + + private void remove(String[] choices) + { + if (choices.length == 1) + { + String removed = taskList.remove(); + System.out.println("Removed " + removed + " from the list."); + } + else + { + int num = getNum(choices[1]); + + for (int i = 0; i < num; i++) + { + System.out.println("Removed " + taskList.remove() + " from the list."); + if (taskList.size() == 0) + { + System.out.println("List empty!"); + return; + } + } + } + System.out.println("Next task is: " + taskList.peek()); + } + + private int getNum(String s) + { + int result = 0; + try + { + result = Integer.parseInt(s); + } catch (Exception e) + { + System.out.println("Invalid number of tasks."); + } + return result; + } + + private void peek(String[] choices) + { + + if (choices.length == 1) + { + System.out.println(taskList.peek()); + } + else + { + int num = getNum(choices[1]); + Iterator itr = taskList.iterator(); + int index = 1; + System.out.println("Viewing " + num + " tasks."); + while (itr.hasNext() && index <= num) + { + System.out.println(itr.next()); + index++; + } + + } + } + + private void add(String[] taskList) + { + if (taskList.length < 2) + { + System.out.println("Error: No task(s) Specified"); + } + else + { + for (int i = 1; i < taskList.length; i++) + { + this.taskList.add(taskList[i]); + System.out.println("Added " + taskList[i] + " to list"); + } + } + } + + private void printOptions() + { + System.out.println(); + System.out.println("Options: "); + System.out.println(); + System.out.println("Add "); + System.out.println("Peek"); + System.out.println("Remove"); + System.out.println("HowMany"); + System.out.println("Flee"); + System.out.println("Exit"); + System.out.println(); + } + + private void printHeader() + { + System.out.println("==============="); + System.out.println("|| Task List ||"); + System.out.println("==============="); + } + + public static void main(String[] args) + { + TaskHelper taskHelper = new TaskHelper(); + taskHelper.run(); + } +}