├── Animals ├── Animal.java ├── Cat.java └── Dog.java ├── ArrayPractice.java ├── BinarySearchTree ├── BinarySearchTree.java ├── EmptyBST.java ├── NonEmptyBST.java ├── Testers.java └── Tree.java ├── CalendarPractice └── CalendarPractice.java ├── Car.java ├── CoinToss.java ├── DatabasePractice └── User.java ├── DictionaryPractice.java ├── ExceptionsPractice.java ├── Generics.java ├── GuessTheNumber.java ├── HangmanApplication ├── Hangman.java ├── HangmanApplication.java └── dictionary.txt ├── HotChocolate ├── HotChocolate.java ├── TemperatureException.java ├── TooColdException.java └── TooHotException.java ├── LibraryCatalogue ├── Book.java └── LibraryCatalogue.java ├── LinkedList ├── LinkedListUS.java └── Node.java ├── LoopPractice.java ├── MadLibs.java ├── MultipleLanguages.txt ├── Person ├── HairColor.java └── Person.java ├── QueuePractice.java ├── QuickStart.java ├── README.md ├── RecursionPractice.java ├── RunTimePractice.java ├── StarWarsInterfacePractice ├── Character.java ├── Enemy.java ├── Hero.java └── StarWarsInterfacePractice.java └── TicTacToeApplication ├── AI.java ├── TicTacToe.java └── TicTacToeApplication.java /Animals/Animal.java: -------------------------------------------------------------------------------- 1 | package Animals; 2 | 3 | // Animal is now an abstract class 4 | public abstract class Animal { 5 | 6 | private int age; // VS private int age; 7 | 8 | public Animal(int age) { 9 | this.age = age; 10 | System.out.println("An animal has been created!"); 11 | } 12 | 13 | // Abstract Method 14 | public abstract void eat(); 15 | 16 | // Non-Abstract Method 17 | public void sleep() { 18 | System.out.println("An animal is sleeping."); 19 | } 20 | 21 | // Getters 22 | public int getAge() { 23 | return age; 24 | } 25 | 26 | public static void main(String[] args) { 27 | Dog d = new Dog(); 28 | Cat c = new Cat(); 29 | d.eat(); 30 | c.eat(); 31 | d.sleep(); 32 | c.sleep(); 33 | 34 | // Casting 35 | Object dog = new Dog(); 36 | Dog realDog = (Dog) dog; 37 | realDog.eat(); 38 | realDog.ruff(); 39 | 40 | // More Casting 41 | Object str = "est"; 42 | // On the right side of the equals sign you put the datatype in parenthesis 43 | String realS = (String) str; 44 | realS.getBytes(); 45 | 46 | // What happens when... 47 | Dog doggy = new Dog(); 48 | if (d instanceof Animal) { 49 | Animal animal = (Animal) doggy; 50 | animal.sleep(); 51 | } 52 | doggy.sleep(); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /Animals/Cat.java: -------------------------------------------------------------------------------- 1 | package Animals; 2 | 3 | public class Cat extends Animal { 4 | 5 | public Cat() { 6 | super(7); 7 | System.out.println("A cat has been created."); 8 | } 9 | 10 | public void eat() { 11 | System.out.println("A cat is eating."); 12 | } 13 | 14 | // Override the Sleep method in Animal 15 | public void sleep() { 16 | System.out.println("A cat is sleeping."); 17 | } 18 | // Method 19 | public void meow() { 20 | System.out.println("A cat meows!"); 21 | } 22 | 23 | // Method 24 | public void prance() { 25 | System.out.println("A cat is prancing."); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Animals/Dog.java: -------------------------------------------------------------------------------- 1 | package Animals; 2 | 3 | public class Dog extends Animal { 4 | 5 | // Constructor 6 | public Dog() { 7 | super(15); //References Animal 8 | System.out.println("A dog has been created."); 9 | } 10 | 11 | // This is necessary becuase the Animal Class is abstract 12 | public void eat(){ 13 | System.out.println("A dog is eating"); 14 | } 15 | 16 | // Override the Sleep method in Animal 17 | public void sleep() { 18 | System.out.println("A dog is sleeping."); 19 | } 20 | 21 | // Method 22 | public void ruff() { 23 | System.out.println("The dog says ruff"); 24 | } 25 | 26 | //Method 27 | public void run() { 28 | System.out.println("A dog is running"); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /ArrayPractice.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class ArrayPractice { 4 | 5 | // "" can be anything (int, String, Boolean) 6 | public static void printArray(E[] array) { 7 | for (E element : array) { 8 | System.out.println(element + " "); 9 | } 10 | System.out.println(); 11 | } 12 | public static void main(String[] args) { 13 | // Location ID: 0,1,2,3,4 14 | // Example Array [0,5,2,6,7] 15 | 16 | // First way to declare arrays 17 | Integer[] intArray1; 18 | 19 | //Second way to declare arrays 20 | Integer[] intArray2 = new Integer[4]; 21 | 22 | //Third way to declare arrays 23 | Integer[] intArray3 = {5,0,2,6,7}; 24 | 25 | String[] shoppingList = {"bananas", "apples", "celery"}; 26 | 27 | // Setting variables in an array 28 | intArray2[0] = 10; // Set the first number in array 2 to the number 10. 29 | intArray2[1] = 20; 30 | intArray2[2] = 30; 31 | intArray2[3] = 40; 32 | 33 | //Print out arrays 34 | System.out.println(Arrays.toString(intArray2)); 35 | System.out.println(Arrays.toString(intArray3)); 36 | System.out.println(); 37 | 38 | // Custom print out arrays 39 | printArray(intArray2); 40 | printArray(intArray3); 41 | System.out.println(); 42 | 43 | // Retrieve objects 44 | System.out.println(intArray2[3]); 45 | System.out.println(); 46 | 47 | //Given Functions 48 | Arrays.sort(intArray3); // Sorting Arrays 49 | printArray(intArray3); 50 | 51 | System.out.println("Special For Loop:"); 52 | // Special for loop: foreach 53 | for (String s : shoppingList) { 54 | System.out.println(s); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /BinarySearchTree/BinarySearchTree.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTree; 2 | 3 | public class BinarySearchTree { 4 | public static void main(String[] args) throws Exception{ 5 | 6 | EmptyBST e = new EmptyBST(); 7 | NonEmptyBST n = new NonEmptyBST(5); 8 | Testers.checkIsEmpty(e); 9 | Testers.checkIsEmpty(n); 10 | Testers.checkAddMemberCardinality(e, 5); 11 | Testers.checkAddMemberCardinality(n, 5); 12 | Testers.checkAddMemberCardinality(n, 6); 13 | 14 | int tests = 1000; 15 | for (int i = 0; i < tests; i++) { 16 | Tree t; 17 | if (i % 10 == 0) { 18 | t = Testers.rndTree(0); 19 | } else { 20 | t = Testers.rndTree(10); 21 | } 22 | Testers.checkAddMemberCardinality(t, i); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /BinarySearchTree/EmptyBST.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTree; 2 | 3 | public class EmptyBST implements Tree{ 4 | 5 | public EmptyBST() { 6 | 7 | } 8 | 9 | public boolean isEmpty() { 10 | return true; 11 | } 12 | 13 | public int cardinality() { 14 | return 0; 15 | } 16 | 17 | public boolean member(D elt) { 18 | return false; 19 | } 20 | 21 | public NonEmptyBST add(D elt) { 22 | return new NonEmptyBST(elt); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /BinarySearchTree/NonEmptyBST.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTree; 2 | 3 | public class NonEmptyBST implements Tree { 4 | 5 | // Fields and input areas for the Non Empty BST 6 | D data; 7 | Tree left; 8 | Tree right; 9 | 10 | // "elt" is short for element 11 | public NonEmptyBST(D elt) { 12 | data = elt; 13 | left = new EmptyBST(); 14 | right = new EmptyBST(); 15 | } 16 | 17 | public NonEmptyBST(D elt, Tree leftTree, Tree rightTree) { 18 | data = elt; 19 | left = leftTree; 20 | right = rightTree; 21 | } 22 | 23 | public boolean isEmpty() { 24 | return false; 25 | } 26 | 27 | public int cardinality() { 28 | return 1 + left.cardinality() + right.cardinality(); 29 | } 30 | 31 | public boolean member(D elt) { 32 | if (data == elt) { 33 | return true; 34 | } else { 35 | if (elt.compareTo(data) < 0) { 36 | return left.member(elt); 37 | } else { 38 | return right.member(elt); 39 | } 40 | } 41 | } 42 | 43 | public NonEmptyBST add(D elt) { 44 | if (data == elt) { 45 | return this; 46 | } else { 47 | if (elt.compareTo(data) < 0) { 48 | return new NonEmptyBST(data, left.add(elt), right); 49 | } else { 50 | return new NonEmptyBST(data, left, right.add(elt)); 51 | } 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /BinarySearchTree/Testers.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTree; 2 | 3 | import java.util.Random; 4 | 5 | public class Testers { 6 | 7 | // Random Integers 8 | public static int rndInt(int min, int max) { 9 | Random rand = new Random(); 10 | return rand.nextInt((max - min) + 1) + min; 11 | // Min = 5 and Max = 15 12 | // Example: 15-5 = 10 + 1 == 11 --> 0 to 10 13 | // + 5 to whatever the random number is 14 | } 15 | 16 | // Random Binary Search Trees 17 | public static Tree rndTree(int count) { 18 | if (count == 0) { 19 | return new EmptyBST(); 20 | } else { 21 | return rndTree(count - 1).add(rndInt(0,50)); 22 | } 23 | } 24 | 25 | // x + (x*2) = x + x *2 { This is an example of a test. We know these are equal, so if for some reason they were not we have a problem.} 26 | 27 | public static void checkIsEmpty(Tree t) throws Exception{ 28 | // if the tree t is an instance of EmptyBST then t.isEmpty should return True 29 | // if the tree t is an instance of NonEmptyBST then t.isEmpty should return False 30 | if (t instanceof EmptyBST) { 31 | if (!t.isEmpty()) { 32 | throw new Exception("All is not good. The tree is an EmptyBST and it is non-empty."); 33 | } else if (t instanceof NonEmptyBST) { 34 | if (t.isEmpty()) { 35 | throw new Exception("All is not good, the tree is a NonEmptyBST and it is empty"); 36 | } 37 | } 38 | } 39 | } 40 | 41 | public static void checkAddMemberCardinality(Tree t, int x) throws Exception { 42 | int nT = (t.add(x)).cardinality(); 43 | // 1. Either something was added and the cardinality increased by one. 44 | if (nT == (t.cardinality() + 1)) { 45 | if (t.member(x)) { 46 | throw new Exception("The cardinality increased by 1, but the thing that was added was already a member of the tree"); 47 | } 48 | } // 2. OR the thing that was added was already there and not really added so the cardinality stayed the same. 49 | else if (nT == t.cardinality()) { 50 | if (!t.member(x)) { 51 | throw new Exception("The cardinality didn't increase by 1, but we added a new thing"); 52 | } else { 53 | throw new Exception("Something is wrong with our program."); 54 | } 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /BinarySearchTree/Tree.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTree; 2 | 3 | // Allowing for generics by allowing D to extend Comparable 4 | public interface Tree { 5 | 6 | public boolean isEmpty(); 7 | public int cardinality(); 8 | public boolean member(D element); 9 | public NonEmptyBST add(D elt); 10 | } 11 | -------------------------------------------------------------------------------- /CalendarPractice/CalendarPractice.java: -------------------------------------------------------------------------------- 1 | package CalendarPractice; 2 | 3 | import java.util.Calendar; 4 | import java.util.Date; 5 | import java.text.SimpleDateFormat; 6 | 7 | public class CalendarPractice { 8 | 9 | public static void main(String[] args) { 10 | Calendar cal = Calendar.getInstance(); 11 | cal.add(Calendar.DATE, 125); 12 | System.out.println(cal.getTime()); 13 | SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd"); 14 | String formatted = format1.format(cal.getTime()); 15 | System.out.println(formatted); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Car.java: -------------------------------------------------------------------------------- 1 | public class Car { 2 | // primitive data types are integers, booleans and characters (char) 3 | int maxSpeed = 250; // integers must be whole numbers 4 | int minSpeed = 0; // a single equals sign means assignment. a double equals sign means "equal to" 5 | 6 | double weight = 4079.25; //doubles can hold decimals. floats can also hold decimals but are smaller than doubles 7 | 8 | boolean isTheCarOn = false; 9 | char condition = 'A'; // characters can only be a single character. characters have single quotes 10 | String nameOfCar = "Grover"; // strings have double quotes. 11 | 12 | double maxFuel = 16; 13 | double currentFuel = 8; 14 | double mpg = 26.4; 15 | 16 | int numberOfPeopleInCar = 1; 17 | int maxNumberOfPeopleInCar = 6; 18 | // everything above, all the variables, are instance variables 19 | 20 | public Car() { 21 | 22 | } 23 | 24 | public Car(int customMaxSpeed, double customWeight, boolean customIsTheCarOn) { 25 | // customMinSpeed is the parameter 26 | maxSpeed = customMaxSpeed; 27 | weight = customWeight; 28 | isTheCarOn = customIsTheCarOn; 29 | 30 | } 31 | 32 | // Getters And Setters 33 | 34 | public int getMaxSpeed() { 35 | return this.maxSpeed; 36 | } 37 | 38 | public void setMaxSpeed(int newMaxSpeed) { 39 | this.maxSpeed = newMaxSpeed; 40 | } 41 | 42 | public int getMinSpeed() { 43 | return this.minSpeed; 44 | } 45 | 46 | public double getWeight() { 47 | return this.weight; 48 | } 49 | 50 | public boolean isTheCarOn() { 51 | return this.isTheCarOn; 52 | } 53 | 54 | public void printVariables() { 55 | // "public" is the scope of the function 56 | // "void" is a return type. This is void because it does not return anything 57 | System.out.println("The Maxmimum speed is " + maxSpeed); // "println" prints to a single line 58 | System.out.println("The Minimum speed is " + minSpeed); 59 | System.out.println("The vehicle wieght is " + weight); 60 | System.out.println("Is the car on? : " + isTheCarOn); 61 | System.out.println("Grade of car condition: " + condition); 62 | System.out.println("Name Of Car: " + nameOfCar); 63 | System.out.println("Number Of People In Car: " + numberOfPeopleInCar); 64 | } 65 | 66 | public void upgradeMaxSpeed() { 67 | setMaxSpeed(getMaxSpeed() + 10); 68 | } 69 | 70 | public void getIn() { 71 | if (numberOfPeopleInCar < maxNumberOfPeopleInCar) { 72 | numberOfPeopleInCar++; 73 | System.out.println("Someone Got In"); 74 | } else { 75 | System.out.println("The Car Is Full! " + numberOfPeopleInCar + " = " + maxNumberOfPeopleInCar); 76 | } 77 | } 78 | 79 | public void getOut() { 80 | // if ther are people in the car 81 | if (numberOfPeopleInCar > 0) { 82 | // tell someone to get out 83 | numberOfPeopleInCar--; 84 | } else { 85 | // there aren't any people in the car 86 | System.out.println("Nobody is in the car!"); 87 | } 88 | } 89 | 90 | public double howManyMilesToEmpty() { 91 | return currentFuel * mpg; 92 | } 93 | 94 | public double maxMilesPerFillUp() { 95 | return maxFuel * mpg; 96 | } 97 | 98 | public void turnTheCarOn() { 99 | // if the car is not on 100 | if (isTheCarOn == false) { 101 | // turn the car on 102 | // isTheCarOn automatically is set to false based on the class parameters 103 | isTheCarOn = true; 104 | } else { 105 | // otherwise print out the fact it is on 106 | System.out.println("The car is already on"); 107 | } 108 | } 109 | public static void main(String[] args) { 110 | Car grover = new Car(); 111 | grover.getOut(); 112 | grover.getOut(); 113 | grover.getIn(); 114 | grover.getIn(); 115 | grover.getIn(); 116 | grover.getIn(); 117 | grover.getIn(); 118 | grover.getIn(); 119 | grover.getIn(); 120 | grover.turnTheCarOn(); 121 | grover.turnTheCarOn(); 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /CoinToss.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | public class CoinToss { 4 | 5 | public String tossACoin() { 6 | Random rand = new Random(); 7 | int toss = Math.abs(rand.nextInt()) % 2; 8 | if (toss == 0) { 9 | return "HEADS"; 10 | } else { 11 | return "TAILS"; 12 | } 13 | } 14 | public static void main(String[] args) { 15 | CoinToss game = new CoinToss(); 16 | System.out.println(game.tossACoin()); 17 | System.out.println(game.tossACoin()); 18 | System.out.println(game.tossACoin()); 19 | System.out.println(game.tossACoin()); 20 | System.out.println(game.tossACoin()); 21 | System.out.println(game.tossACoin()); 22 | System.out.println(game.tossACoin()); 23 | } 24 | } -------------------------------------------------------------------------------- /DatabasePractice/User.java: -------------------------------------------------------------------------------- 1 | package DatabasePractice; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | public class User { 7 | 8 | String username; 9 | String password; 10 | int age; 11 | Set orderIDs; 12 | 13 | public User(String customUserName, String customPassword, int customAge, Set orderIDs) { 14 | this.username = customUserName; 15 | this.password = customPassword; 16 | this.age = customAge; 17 | this.orderIDs = orderIDs; 18 | } 19 | 20 | public static void main(String[] args) { 21 | Set a = new HashSet(); 22 | a.add(1212); 23 | User rando = new User("rando", "rando1", 25, a); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /DictionaryPractice.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.Map; 3 | 4 | public class DictionaryPractice { 5 | 6 | public static void main(String[] args) { 7 | //English to Spanish dictionary 8 | Map englSpanDictionary = new HashMap(); 9 | //Putting things inside of the dictionary 10 | englSpanDictionary.put("Monday","Lunes"); 11 | englSpanDictionary.put("Tuesday", "Martes"); 12 | englSpanDictionary.put("Wednesday", "Miercoles"); 13 | englSpanDictionary.put("Thursday", "Jueves"); 14 | englSpanDictionary.put("Friday","Viernes"); 15 | englSpanDictionary.put("Saturday", "Sabado"); 16 | englSpanDictionary.put("Sunday", "Domingo"); 17 | //Retrieve things from the dictionary 18 | System.out.println(englSpanDictionary.get("Monday")); 19 | System.out.println(englSpanDictionary.get("Tuesday")); 20 | System.out.println(englSpanDictionary.get("Wednesday")); 21 | System.out.println(englSpanDictionary.get("Thursday")); 22 | System.out.println(englSpanDictionary.get("Friday")); 23 | //Print out all keys 24 | System.out.println(englSpanDictionary.keySet()); 25 | // Print out all values 26 | System.out.println(englSpanDictionary.values()); 27 | //Print out the size of the dictionary 28 | System.out.println(englSpanDictionary.size()); 29 | 30 | System.out.println(); 31 | System.out.println(); 32 | 33 | //Shopping List 34 | Map shoppingList = new HashMap(); 35 | //Putting things inside of the dictionary 36 | shoppingList.put("Ham", true); 37 | shoppingList.put("Bread", true); 38 | shoppingList.put("Sprite", false); 39 | shoppingList.put("Eggs", Boolean.FALSE); 40 | //Retrieve Items 41 | System.out.println(shoppingList.get("Ham")); 42 | System.out.println(shoppingList.get("Sprite")); 43 | //Remove Items 44 | shoppingList.remove("Eggs"); 45 | //Key-Value Pairs Print Out 46 | System.out.println(shoppingList.toString()); 47 | //Replace Values 48 | shoppingList.replace("Bread", true, false); 49 | //Clear Dictionary 50 | shoppingList.clear(); 51 | System.out.println(shoppingList.toString()); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /ExceptionsPractice.java: -------------------------------------------------------------------------------- 1 | public class ExceptionsPractice { 2 | 3 | public static void main(String[] args) { 4 | // Try-Catch Block 5 | 6 | //Try Clause "Do this until we get an exception" 7 | try { 8 | int[] c = new int[6]; 9 | System.out.println("Element 6 at index 5 = " + c[5]); 10 | } 11 | //Catch Clause "Do this is we get (type_of_error) in the try" 12 | catch (ArrayIndexOutOfBoundsException e) { 13 | System.out.println("Exception thrown" + e); 14 | } 15 | //Finally Clause "Do this no matter what" 16 | finally { 17 | System.out.println("Finally clause"); 18 | } 19 | 20 | System.out.println("Finally Finished Try-Catch"); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Generics.java: -------------------------------------------------------------------------------- 1 | public class Generics{ 2 | 3 | // "T" is some type of data that extends "Comparable" 4 | public static > T findMax(T a, T b) { 5 | int n = a.compareTo(b); 6 | if (n < 0) { 7 | return b; 8 | } else { 9 | return a; 10 | } 11 | } 12 | public static void main(String[] args) { 13 | System.out.println(findMax(2, 3)); 14 | System.out.println(findMax(3, 3)); 15 | System.out.println(findMax(3, 2)); 16 | 17 | System.out.println(findMax(2.0, 3.0)); 18 | System.out.println(findMax('a', 'b')); 19 | System.out.println(findMax("hello", "there")); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /GuessTheNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | import java.util.Scanner; 3 | 4 | public class GuessTheNumber { 5 | 6 | int theNUMBER; // the number that the user is guessing 7 | int max; // the max number the computer could come up with 8 | Scanner scanner = new Scanner(System.in); 9 | 10 | // Constructor 11 | public GuessTheNumber() { 12 | Random rand = new Random(); 13 | max = 100; 14 | theNUMBER = Math.abs(rand.nextInt()) % (max + 1); 15 | } 16 | 17 | // Method 18 | public void play() { 19 | while (true) { 20 | int move = scanner.nextInt(); 21 | if (move > theNUMBER) { 22 | System.out.println("Your number is too big"); 23 | } 24 | if (move < theNUMBER) { 25 | System.out.println("Your number is too small"); 26 | } else { 27 | System.out.println("YOU WON!!!"); 28 | } 29 | } 30 | } 31 | 32 | public static void howBigIsMyNumber(int x) { 33 | if (x >= 0 && x <= 10) { 34 | System.out.println("Our number is pretty small"); 35 | } else if (x >= 11 && x <= 100) { 36 | System.out.println("Our number is pretty big"); 37 | } else { 38 | System.out.println("Our number is out of range"); 39 | } 40 | } 41 | 42 | // Method 43 | public static void main(String[] args) { 44 | GuessTheNumber guessGame = new GuessTheNumber(); 45 | System.out.println( 46 | "Welcome to my game. Try and guess my impossible number." 47 | + " It is between 0 and " + guessGame.max + " inclusive." 48 | + " Just type a number to get started."); 49 | guessGame.play(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /HangmanApplication/Hangman.java: -------------------------------------------------------------------------------- 1 | package HangmanApplication; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.File; 5 | import java.io.FileReader; 6 | import java.io.IOException; 7 | import java.util.ArrayList; 8 | import java.util.Random; 9 | 10 | public class Hangman { 11 | 12 | String mysteryWord; 13 | StringBuilder currentGuess; 14 | ArrayList previousGuesses = new ArrayList<>(); 15 | 16 | int maxTries = 6; 17 | int currentTry = 0; 18 | 19 | ArrayList dictionary = new ArrayList<>(); 20 | private static FileReader fileReader; 21 | private static BufferedReader bufferedFileReader; 22 | 23 | public Hangman() throws IOException { 24 | initializeStreams(); 25 | mysteryWord = pickWord(); 26 | currentGuess = initializeCurrentGuess(); 27 | } 28 | 29 | public void initializeStreams() throws IOException { 30 | try { 31 | File inFile = new File("dictionary.txt"); 32 | fileReader = new FileReader(inFile); 33 | bufferedFileReader = new BufferedReader(fileReader); 34 | String currentLine = bufferedFileReader.readLine(); 35 | while (currentLine != null) { 36 | dictionary.add(currentLine); 37 | currentLine = bufferedFileReader.readLine(); 38 | } 39 | bufferedFileReader.close(); 40 | fileReader.close(); 41 | } catch(IOException e) { 42 | System.out.println("Could not init streams"); 43 | } 44 | } 45 | 46 | public String pickWord() { 47 | Random rand = new Random(); 48 | int wordIndex = Math.abs(rand.nextInt()) % dictionary.size(); 49 | return dictionary.get(wordIndex); 50 | } 51 | 52 | public StringBuilder initializeCurrentGuess() { 53 | StringBuilder current = new StringBuilder(); 54 | for (int i = 0; i < mysteryWord.length() * 2; i++) { 55 | if (i % 2 == 0) { 56 | current.append("_"); 57 | } else { 58 | current.append(" "); 59 | } 60 | } 61 | return current; 62 | } 63 | 64 | public String getFormalCurrentGuess() { 65 | return "Current Guess: " + currentGuess.toString(); 66 | } 67 | 68 | public boolean gameOver() { 69 | if (didWeWin()) { 70 | System.out.println(); 71 | System.out.println("Congrats! You won! You guessed the right word!"); 72 | return true; 73 | } else if (didWeLose()) { 74 | System.out.println(); 75 | System.out.println("Sorry, you lost. You spent all of your 6 tries. " 76 | + "The word was " + mysteryWord + "."); 77 | return true; 78 | } 79 | return false; 80 | } 81 | 82 | public boolean didWeLose() { 83 | return currentTry >= maxTries; 84 | } 85 | 86 | public boolean didWeWin() { 87 | String guess = getCondensedCurrentGuess(); 88 | return guess.equals(mysteryWord); 89 | } 90 | 91 | public String getCondensedCurrentGuess() { 92 | String guess = currentGuess.toString(); 93 | return guess.replace(" ", ""); 94 | } 95 | 96 | 97 | public boolean isGuessedAlready(char guess) { 98 | return previousGuesses.contains(guess); 99 | } 100 | 101 | public boolean playGuess(char guess) { 102 | boolean isItAGoodGuess = false; 103 | previousGuesses.add(guess); 104 | for (int i = 0; i < mysteryWord.length(); i++) { 105 | if (mysteryWord.charAt(i) == guess) { 106 | currentGuess.setCharAt(i * 2, guess); 107 | isItAGoodGuess = true; 108 | } 109 | } 110 | 111 | if (!isItAGoodGuess) { 112 | currentTry++; 113 | } 114 | 115 | return isItAGoodGuess; 116 | } 117 | 118 | public String drawPicture() { 119 | switch(currentTry) { 120 | case 0: return noPersonDraw(); 121 | case 1: return addHeadDraw(); 122 | case 2: return addBodyDraw(); 123 | case 3: return addOneArmDraw(); 124 | case 4: return addSecondArmDraw(); 125 | case 5: return addFirstLegDraw(); 126 | default: return fullPersonDraw(); 127 | } 128 | } 129 | 130 | private String fullPersonDraw() { 131 | return " - - - - -\n"+ 132 | "| |\n"+ 133 | "| O\n"+ 134 | "| / | \\ \n"+ 135 | "| |\n"+ 136 | "| / \\ \n"+ 137 | "|\n"+ 138 | "|\n"; 139 | } 140 | 141 | private String addFirstLegDraw() { 142 | return " - - - - -\n"+ 143 | "| |\n"+ 144 | "| O\n"+ 145 | "| / | \\ \n"+ 146 | "| |\n"+ 147 | "| / \n"+ 148 | "|\n"+ 149 | "|\n"; 150 | } 151 | 152 | private String addSecondArmDraw() { 153 | return " - - - - -\n"+ 154 | "| |\n"+ 155 | "| O\n"+ 156 | "| / | \\ \n"+ 157 | "| |\n"+ 158 | "| \n"+ 159 | "|\n" + 160 | "|\n"; 161 | } 162 | 163 | private String addOneArmDraw() { 164 | return " - - - - -\n"+ 165 | "| |\n"+ 166 | "| O\n"+ 167 | "| / |\n"+ 168 | "| |\n"+ 169 | "| \n"+ 170 | "|\n"+ 171 | "|\n"; 172 | } 173 | 174 | private String addBodyDraw() { 175 | return " - - - - -\n"+ 176 | "| |\n"+ 177 | "| O\n"+ 178 | "| |\n"+ 179 | "| |\n"+ 180 | "| \n"+ 181 | "|\n"+ 182 | "|\n"; 183 | } 184 | 185 | private String addHeadDraw() { 186 | return " - - - - -\n"+ 187 | "| |\n"+ 188 | "| O\n"+ 189 | "| \n"+ 190 | "| \n"+ 191 | "| \n"+ 192 | "|\n"+ 193 | "|\n"; 194 | } 195 | 196 | private String noPersonDraw() { 197 | return " - - - - -\n"+ 198 | "| |\n"+ 199 | "| \n"+ 200 | "| \n"+ 201 | "| \n"+ 202 | "| \n"+ 203 | "|\n"+ 204 | "|\n"; 205 | } 206 | } 207 | -------------------------------------------------------------------------------- /HangmanApplication/HangmanApplication.java: -------------------------------------------------------------------------------- 1 | package HangmanApplication; 2 | 3 | import java.io.IOException; 4 | import java.util.Scanner; 5 | 6 | public class HangmanApplication { 7 | 8 | /** 9 | * @param args the command line arguments 10 | */ 11 | public static void main(String[] args) throws IOException { 12 | // How do we play the game 13 | Scanner sc = new Scanner(System.in); 14 | System.out.println("Welcome to hangman! I will pick a word and you will " 15 | + "try to guess it character " 16 | + "by character." 17 | + "If you guess wrong 6 times, then I win.\nIf you can guess it before then, you win. " 18 | + "Are you ready? I hope so because I am."); 19 | System.out.println(); 20 | System.out.println("I have picked my word. Below is a picture, and below " 21 | + "that is your current guess, which starts off as nothing. \nEvery time you " 22 | + "guess incorrectly. I add a body part to the picture. When there is a full" 23 | + " person, you lose."); 24 | 25 | // Allows for multiple games 26 | boolean doYouWantToPlay = true; 27 | while (doYouWantToPlay) { 28 | // Setting up the game 29 | System.out.println(); 30 | System.out.println("Alright! Let's play!"); 31 | Hangman game = new Hangman(); 32 | do { 33 | // Draw the things... 34 | System.out.println(); 35 | System.out.println(game.drawPicture()); 36 | System.out.println(); 37 | System.out.println(game.getFormalCurrentGuess()); 38 | System.out.print(game.mysteryWord); 39 | System.out.println(); 40 | 41 | // Get the guess 42 | System.out.println("Enter a character that you think is in the word"); 43 | char guess = (sc.next().toLowerCase()).charAt(0); 44 | System.out.println(); 45 | 46 | // Check if the character is guessed already 47 | while (game.isGuessedAlready(guess)) { 48 | System.out.println("Try again! You've already guessed that character."); 49 | guess = (sc.next().toLowerCase()).charAt(0); 50 | } 51 | 52 | // Play the guess 53 | if (game.playGuess(guess)) { 54 | System.out.println("Great guess! That character is in the word!"); 55 | } else { 56 | System.out.println("Unfortunately that character isn't in the word"); 57 | } 58 | 59 | } 60 | while (!game.gameOver()); // Keep playing until the game is over 61 | 62 | // Play again or no? 63 | System.out.println(); 64 | System.out.println("Do you want to play another game? Enter Y if you do."); 65 | Character response = (sc.next().toUpperCase()).charAt(0); 66 | doYouWantToPlay = (response == 'Y'); 67 | 68 | } 69 | } 70 | 71 | } -------------------------------------------------------------------------------- /HotChocolate/HotChocolate.java: -------------------------------------------------------------------------------- 1 | package HotChocolate; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | public class HotChocolate { 6 | 7 | // creating maximum temparature for too hot 8 | public static final double tooHot = 185; 9 | 10 | // creating minimum tempature for too cold 11 | public static final double tooCold = 160; 12 | 13 | // methods must throw an error or else the error will show up as a syntax issue 14 | // within code 15 | public static void drinkHotChocolate(double cocoaTemp) throws TooColdException, TooHotException { 16 | if (cocoaTemp >= tooHot) { 17 | throw new TooHotException(); 18 | } else if (cocoaTemp <= tooCold) { 19 | throw new TooColdException(); 20 | } 21 | } 22 | 23 | // main method 24 | public static void main(String[] args) throws InterruptedException { 25 | // Sets current temp using the double "currentCocoaTemp" 26 | double currentCocoaTemp = 150; 27 | 28 | boolean wrongTemp = true; 29 | while (wrongTemp) { 30 | // Try-Catch loop where we try to drink the hot chocolate using 31 | // "drinkHotChocolate" 32 | try { 33 | drinkHotChocolate(currentCocoaTemp); 34 | System.out.println("That cocoa was good"); 35 | wrongTemp = false; 36 | } 37 | // if it is too hot we send an error that states it is too hot 38 | catch (TooHotException e1) { 39 | System.out.println("That's Too Hot!"); 40 | currentCocoaTemp = currentCocoaTemp - 5; 41 | } 42 | // if it is too cold we send an error that states it is too cold 43 | catch (TooColdException e2) { 44 | System.out.println("That's Too Cold!"); 45 | currentCocoaTemp = currentCocoaTemp + 5; 46 | } 47 | TimeUnit.SECONDS.sleep(2); 48 | } 49 | System.out.println("And it's gone"); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /HotChocolate/TemperatureException.java: -------------------------------------------------------------------------------- 1 | package HotChocolate; 2 | 3 | public class TemperatureException extends Exception { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /HotChocolate/TooColdException.java: -------------------------------------------------------------------------------- 1 | package HotChocolate; 2 | 3 | public class TooColdException extends TemperatureException { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /HotChocolate/TooHotException.java: -------------------------------------------------------------------------------- 1 | package HotChocolate; 2 | 3 | public class TooHotException extends TemperatureException { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /LibraryCatalogue/Book.java: -------------------------------------------------------------------------------- 1 | package LibraryCatalogue; 2 | 3 | public class Book { 4 | 5 | // Defining characteristics of the book 6 | // Properties, Fields, Global Variables 7 | String title; 8 | int pageCount; 9 | int ISBN; 10 | boolean isCheckedOut; // whether or not the book is checked out 11 | int dayCheckedOut = -1; 12 | 13 | // Constructor. How do we make our book? 14 | public Book(String bookTitle, int bookPageCount, int bookISBN) { 15 | this.title = bookTitle; 16 | this.pageCount = bookPageCount; 17 | this.ISBN = bookISBN; 18 | isCheckedOut = false; 19 | } 20 | 21 | // Getters --> Instance Methods. Getting value of certain properties within the instance 22 | public String getTitle() { 23 | return this.title; 24 | } 25 | 26 | public int getPageCount() { 27 | return this.pageCount; 28 | } 29 | 30 | public int getISBN() { 31 | return this.ISBN; 32 | } 33 | 34 | public boolean getIsCheckedOut() { 35 | return this.isCheckedOut; 36 | } 37 | 38 | public int getDayCheckedOut() { 39 | return this.dayCheckedOut; 40 | } 41 | 42 | // Setters 43 | public void setIsCheckedOut(boolean newIsCheckedOut, int currentDayCheckedOut) { 44 | this.isCheckedOut = newIsCheckedOut; 45 | setDayCheckedOut(currentDayCheckedOut); 46 | } 47 | 48 | private void setDayCheckedOut(int day) { 49 | this.dayCheckedOut = day; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /LibraryCatalogue/LibraryCatalogue.java: -------------------------------------------------------------------------------- 1 | package LibraryCatalogue; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class LibraryCatalogue { 7 | 8 | // Creating a dictionary 9 | Map bookCollection = new HashMap(); 10 | 11 | // Properties, Fields, Global Variables 12 | int currentDay = 0; 13 | int lengthOfCheckoutPeriod = 7; 14 | double initialLateFee = 0.50; 15 | double feePerLateDay = 1.00; 16 | 17 | // Constructor 18 | public LibraryCatalogue(Map collection) { 19 | this.bookCollection = collection; 20 | } 21 | 22 | public LibraryCatalogue(Map collection, int lengthOfCheckoutPeriod, double initialLateFee, double feePerLateDay) { 23 | this.bookCollection = collection; 24 | this.lengthOfCheckoutPeriod = lengthOfCheckoutPeriod; 25 | this.initialLateFee = initialLateFee; 26 | this.feePerLateDay = feePerLateDay; 27 | } 28 | 29 | // Getters 30 | public int getCurrentDay() { 31 | return this.currentDay; 32 | } 33 | 34 | public Map getBookCollection() { 35 | return this.bookCollection; 36 | } 37 | 38 | public Book getBook(String bookTitle) { 39 | return getBookCollection().get(bookTitle); 40 | } 41 | 42 | public int getLengthOfCheckoutPeriod() { 43 | return this.lengthOfCheckoutPeriod; 44 | } 45 | 46 | public double getInitialLateFee() { 47 | return this.initialLateFee; 48 | } 49 | 50 | public double getFeePerLateDay() { 51 | return this.feePerLateDay; 52 | } 53 | 54 | // Setters 55 | public void nextDay() { 56 | currentDay ++; 57 | } 58 | 59 | public void setDay(int day) { 60 | currentDay = day; 61 | } 62 | 63 | // Instance Methods. Do not have STATIC in front of them 64 | // Checking out a book 65 | public void checkOutBook(String title) { 66 | Book book = getBook(title); 67 | if (book.getIsCheckedOut()) { 68 | sorryBookAlreadyCheckedOut(book); 69 | } else { 70 | book.setIsCheckedOut(true, currentDay); 71 | System.out.println("You just checked out " + title + ". It is due on day " + (getCurrentDay() + getLengthOfCheckoutPeriod())+ "."); 72 | } 73 | } 74 | 75 | // Returning A Book 76 | public void returnBook(String title) { 77 | Book book = getBook(title); 78 | int daysLate = currentDay - (book.getDayCheckedOut() + getLengthOfCheckoutPeriod()); 79 | if (daysLate > 0) { 80 | System.out.println("You owe the library $" + (getInitialLateFee() + daysLate * getFeePerLateDay()) + " because your book is " + daysLate + " days overdue."); 81 | } else { 82 | System.out.println("Book Returned. Thank you"); 83 | } 84 | } 85 | 86 | // Book is not available at the time 87 | public void sorryBookAlreadyCheckedOut(Book book) { 88 | System.out.println("Sorry, " + book.getTitle() + " is already checked out. " + "It should be back on day " + (book.getDayCheckedOut() + getLengthOfCheckoutPeriod()) + "."); 89 | } 90 | 91 | // Main Method 92 | public static void main(String[] args) { 93 | Map bookCollection = new HashMap(); 94 | Book Aaron = new Book("Aarons Book", 1234, 11111111); 95 | bookCollection.put("Aarons Book", Aaron); 96 | LibraryCatalogue lib = new LibraryCatalogue(bookCollection); 97 | lib.checkOutBook("Aarons Book"); 98 | lib.nextDay(); 99 | lib.nextDay(); 100 | lib.checkOutBook("Aarons Book"); 101 | lib.setDay(17); 102 | lib.returnBook("Aarons Book"); 103 | lib.checkOutBook("Aarons Book"); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /LinkedList/LinkedListUS.java: -------------------------------------------------------------------------------- 1 | package LinkedList; 2 | 3 | import java.util.LinkedList; 4 | 5 | /* 6 | Things neededed for this project: 7 | 1. Ability to add nodes 8 | 2. Ability to remove nodes 9 | 3. How many nodes are in this sequence? 10 | 4. Is this sequence empty? 11 | 5. Retrieve Nodes (Data in nodes) 12 | */ 13 | 14 | public class LinkedListUS { 15 | // Properties 16 | Node head; 17 | int count; 18 | 19 | // Constructors 20 | // public LinkedList() { 21 | // head = null; 22 | // count = 0; 23 | // } 24 | 25 | public LinkedListUS(Node newHead) { 26 | head = newHead; 27 | count = 1; 28 | } 29 | 30 | // Methods 31 | 32 | // add 33 | public void add(D newData) { 34 | Node temp = new Node(newData); 35 | Node current = head; 36 | while (current.getNext() != null) { 37 | current = current.getNext(); 38 | } 39 | current.setNext(temp); 40 | count++; 41 | } 42 | 43 | //get 44 | public D get(int index) { 45 | //if (index <= 0) { 46 | // return -1; 47 | //} 48 | Node current = head; 49 | for (int i = 1; i < index; i++) { 50 | current = current.getNext(); 51 | } 52 | return current.getData(); 53 | } 54 | 55 | // size 56 | public int size() { 57 | return count; 58 | } 59 | 60 | // isEmpty 61 | public boolean isEmpty() { 62 | return head == null; 63 | } 64 | 65 | // remove 66 | public void remove() { 67 | // remove from the back of the list 68 | Node current = head; 69 | while (current.getNext().getNext() != null) { 70 | current = current.getNext(); 71 | } 72 | current.setNext(null); 73 | count--; 74 | } 75 | public static void main(String[] args) { 76 | LinkedList linkedlist = new LinkedList(); 77 | linkedlist.add("Aaron"); 78 | System.out.println(linkedlist); 79 | linkedlist.add("Montina"); 80 | System.out.println(linkedlist); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /LinkedList/Node.java: -------------------------------------------------------------------------------- 1 | package LinkedList; 2 | 3 | public class Node { 4 | // Properties 5 | Node next; 6 | D data; 7 | 8 | // Methods 9 | 10 | // Constructors 11 | public Node(D newData) { 12 | data = newData; 13 | next = null; 14 | } 15 | 16 | public Node(D newData, Node newNext) { 17 | data = newData; 18 | next = newNext; 19 | } 20 | 21 | //Getters and Setters 22 | 23 | //Getters 24 | public D getData() { 25 | return data; 26 | } 27 | 28 | public Node getNext() { 29 | return next; 30 | } 31 | 32 | //Setters 33 | public void setData(D newData) { 34 | data = newData; 35 | } 36 | 37 | public void setNext(Node newNode) { 38 | next = newNode; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /LoopPractice.java: -------------------------------------------------------------------------------- 1 | public class LoopPractice { 2 | 3 | //Function for While Loop 4 | public static void practiceWhileLoop() { 5 | // variable 6 | int x = 0; 7 | 8 | while(x < 5) { 9 | System.out.println("The value of x is " + x); 10 | x++; 11 | } 12 | System.out.println("This is the end of the While Loop."); 13 | } 14 | 15 | //Function for Do While Loop 16 | public static void practiceDoWhileLoop() { 17 | int x = 0; 18 | do { 19 | System.out.println("The value of x is " + x); 20 | x++; 21 | } while (x < 10); 22 | System.out.println("This is the end of the Do While Loop."); 23 | } 24 | 25 | public static void practiceForLoop() { 26 | for (int x = 0; x < 10; x++) { 27 | for (int y = 0; y < 10; y++) { 28 | System.out.println("("+x+","+y+")"); 29 | } 30 | } 31 | System.out.println("This is the end of the For Loop."); 32 | } 33 | 34 | public static void main(String[] args) { 35 | practiceWhileLoop(); 36 | practiceDoWhileLoop(); 37 | practiceForLoop(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /MadLibs.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | import java.util.Scanner; 3 | 4 | public class MadLibs { 5 | 6 | // Characteristics of the MadLib story. Instance variables 7 | Scanner scanner = new Scanner(System.in); 8 | String story; 9 | String name; 10 | String adjective1; 11 | String adjective2; 12 | String noun1; 13 | String noun2; 14 | String noun3; 15 | String adverb; 16 | String randomNums; 17 | Random rand = new Random(); 18 | // End instance variables 19 | 20 | // Getters 21 | public String getStory() { 22 | return story; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public String getAdjective1() { 30 | return adjective1; 31 | } 32 | 33 | public String getAdjective2() { 34 | return adjective2; 35 | } 36 | 37 | public String getNoun1() { 38 | return noun1; 39 | } 40 | 41 | public String getNoun2() { 42 | return noun2; 43 | } 44 | 45 | public String getNoun3() { 46 | return noun3; 47 | } 48 | 49 | public String getAdverb() { 50 | return adverb; 51 | } 52 | 53 | public String getRandomNums() { 54 | return randomNums; 55 | } 56 | // End Getters 57 | 58 | // Setters 59 | public void setStory(String newStory) { 60 | this.story = newStory; 61 | } 62 | 63 | public void setName(String newName) { 64 | this.name = newName; 65 | } 66 | 67 | public void setAdjective1(String newAdjective1) { 68 | this.adjective1 = newAdjective1; 69 | } 70 | 71 | public void setAdjective2(String newAdjective2) { 72 | this.adjective2 = newAdjective2; 73 | } 74 | 75 | public void setNoun1(String newNoun1) { 76 | this.noun1 = newNoun1; 77 | } 78 | 79 | public void setNoun2(String newNoun2) { 80 | this.noun2 = newNoun2; 81 | } 82 | 83 | public void setNoun3(String newNoun3) { 84 | this.noun3 = newNoun3; 85 | } 86 | 87 | public void setAdverb(String newAdverb) { 88 | this.adverb = newAdverb; 89 | } 90 | 91 | public void setRandomNums() { 92 | int num = Math.abs(rand.nextInt()) % 100; // Get a random number 93 | int index = 0; // Set the index 94 | int[] numberHolder = new int[3]; // Create an array 95 | while(index < numberHolder.length) { 96 | // While the index is lower than the length of the array keep going through 97 | numberHolder[index] = num + index; 98 | index++; 99 | } 100 | 101 | randomNums = "not " + numberHolder[0] + ", not " + numberHolder[1] + ", but " + numberHolder[2]; 102 | } 103 | // End Setters 104 | 105 | // Print Instructions for the player 106 | public void printInstructions() { 107 | System.out.println("Welcome to the MadLibs game. If you type in " 108 | + "words, we'll give you a story. Start by typing a name."); 109 | } 110 | 111 | // Allowing users to enter information using the Scanner. 112 | public void enterName() { 113 | setName(scanner.nextLine()); // nextLine will take the next line that a user enters 114 | } 115 | 116 | public void enterAdjective1() { 117 | System.out.println("Give me an adjective: "); 118 | setAdjective1(scanner.nextLine()); 119 | } 120 | 121 | public void enterAdjective2() { 122 | System.out.println("Give me another adjective: "); 123 | setAdjective2(scanner.nextLine()); 124 | } 125 | 126 | public void enterNoun1() { 127 | System.out.println("Give me a noun: "); 128 | setNoun1(scanner.nextLine()); 129 | } 130 | 131 | public void enterNoun2() { 132 | System.out.println("Give me another noun: "); 133 | setNoun2(scanner.nextLine()); 134 | } 135 | 136 | public void enterNoun3() { 137 | System.out.println("Give me the last noun: "); 138 | setNoun3(scanner.nextLine()); 139 | } 140 | 141 | public void enterAdverb() { 142 | System.out.println("Give me an adverb: "); 143 | setAdverb(scanner.nextLine()); 144 | } 145 | 146 | // Building the frame of the story 147 | public void putTogetherTheStory() { 148 | String story; 149 | int num = Math.abs(rand.nextInt()) % 2; 150 | if (num == 0) { 151 | story = "Jessie and her best friend " + getName() + " went to Disney Word today! " 152 | + "They saw a " + getNoun1() + " in a show at the Magic Kingdom " 153 | + "and ate a " + getAdjective1() + "feast for dinner. The next day I ran " 154 | + getAdverb() + " to meet Mickey Mouse in his " + getNoun2() + 155 | " and then that night I gazed at the " + getRandomNums() + " " + getAdjective2() + 156 | " fireworks shooting from the " + getNoun3() + "."; 157 | } else { 158 | story = "Amanda and her frenemy " + getName() + " went to the zoo last summer. " 159 | + "They saw a huge " + getNoun1() + " and a tiny little " + getNoun2() + ". " 160 | + "That night they decided to climb " + getAdverb() + " into the " + getNoun3() 161 | + " to get a closer look. The Zoo was " + getAdjective1() + " at night, but they didn't care.. until " 162 | + getRandomNums() + " " + getAdjective2() + " apes yelled in their faces, making Amanda and " 163 | + getName() + " sprint all the way back home."; 164 | } 165 | setStory(story); 166 | } 167 | 168 | public void play() { 169 | enterName(); 170 | enterNoun1(); 171 | enterAdjective1(); 172 | enterAdjective2(); 173 | enterNoun2(); 174 | enterAdverb(); 175 | enterNoun3(); 176 | setRandomNums(); 177 | putTogetherTheStory(); 178 | System.out.println(getStory()); 179 | } 180 | 181 | public static void main(String[] args) { 182 | MadLibs game = new MadLibs(); 183 | game.printInstructions(); 184 | game.play(); 185 | } 186 | 187 | } -------------------------------------------------------------------------------- /MultipleLanguages.txt: -------------------------------------------------------------------------------- 1 | // Variables 2 | Java | String s = "Hello World"; 3 | Python | s = "Hello World" 4 | C++ | string s = "Hello World; 5 | 6 | // Operations 7 | Java | int x = 4 + 4; 8 | Scheme/Racket/Perl | (define x (+ 4 4)) // functional programming language (Lisp) 9 | 10 | // Control-Flow Statements 11 | Java | 12 | int counter = 5; 13 | while (counter > 0) { 14 | System.out.println(counter); 15 | counter--; 16 | } 17 | 18 | Swift (iOS Development) | 19 | var counter = 5 20 | while counter > 0 { 21 | println(counter) 22 | counter-- 23 | } 24 | 25 | // Functions 26 | Java | 27 | public static String sayHello(String name) { 28 | String say = "Hello " + name; 29 | return say; 30 | } 31 | 32 | Ruby | 33 | def sayHello(name) 34 | say = "hello " + name 35 | return var 36 | end -------------------------------------------------------------------------------- /Person/HairColor.java: -------------------------------------------------------------------------------- 1 | package Person; 2 | 3 | public enum HairColor { 4 | // Constants must be in full caps 5 | BLONDE, BROWN, BLACK, RED, ORANGE, PINK, BLUE, GREEN, PURPLE, RAINBOW, OTHER 6 | } 7 | -------------------------------------------------------------------------------- /Person/Person.java: -------------------------------------------------------------------------------- 1 | package Person; 2 | 3 | // Must import the enum in order for things to work 4 | import static Person.HairColor.*; 5 | 6 | public class Person { 7 | 8 | // Hair color must actual be a color and not something like "bubbles." Introducing Enums 9 | HairColor hairColor = BLACK; 10 | 11 | // Constructor that creates a new person 12 | public Person() { 13 | 14 | } 15 | 16 | public static void main(String[] args) { 17 | // Testing aliasing with Peter Parker as Spiderman 18 | Person peterParker = new Person(); 19 | Person spiderMan = peterParker; 20 | 21 | // Changing the default black hair to pink hair. Will Spiderman's hair change too? 22 | peterParker.hairColor = PINK; 23 | 24 | System.out.println("Hair Color of Peter Parker: " + peterParker.hairColor); 25 | System.out.println("Hair Color of Spiderman: " + spiderMan.hairColor); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /QueuePractice.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | import java.util.Stack; 3 | 4 | public class QueuePractice { 5 | //Creating a new LinkedList 6 | LinkedList queue = new LinkedList(); 7 | 8 | // Making a queue instance 9 | public QueuePractice() { 10 | queue = new LinkedList(); 11 | } 12 | 13 | // Is our queue empty 14 | public boolean isEmpty() { 15 | return queue.isEmpty(); 16 | } 17 | 18 | // What is the size of our queue 19 | public int size() { 20 | return queue.size(); 21 | } 22 | 23 | // Enqueueing an item 24 | public void enqueue(D n) { 25 | queue.addLast(n); 26 | } 27 | 28 | // Dequeueing an item 29 | public D dequeue() { 30 | return queue.remove(0); 31 | } 32 | 33 | // Peek at the first item 34 | public D peek() { 35 | return queue.get(0); 36 | } 37 | 38 | public static void main(String[] args) { 39 | // Creating A Stack. Only strings can be in this stack, but other data types can be added to stacks with proper notation/syntax 40 | Stack StackPractice = new Stack<>(); 41 | StackPractice.push("there"); 42 | StackPractice.push("hi"); 43 | System.out.println(StackPractice.pop() + " "); 44 | System.out.println("Peek:" + StackPractice.peek()); 45 | System.out.println(StackPractice.pop() + "! "); 46 | System.out.println("Size:" + StackPractice.size()); 47 | 48 | // Queues in the Main Method 49 | QueuePractice numberQueue = new QueuePractice(); 50 | numberQueue.enqueue(5); 51 | numberQueue.enqueue(7); 52 | numberQueue.enqueue(6); 53 | System.out.println("First out: " + numberQueue.dequeue()); 54 | System.out.println("Peek at second item: " + numberQueue.peek()); 55 | System.out.println("Second out: " + numberQueue.dequeue()); 56 | System.out.println("Third out: " + numberQueue.dequeue()); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /QuickStart.java: -------------------------------------------------------------------------------- 1 | class QuickStart { 2 | public static void main(String[] args) { 3 | System.out.println("Learning Java Coding"); 4 | } 5 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 30-Days-Of-Java 2 | *Code and lessons from "30 Days Of Code" led by @BlondieBytes* 3 | 4 | ### Completed By: Aaron Galloway 5 | 6 | --- 7 | 8 | My introduction to 30 Days of Code originally came from HackerRank. After finding the series on YouTube I decided to complete the series on YouTube independent of HackerRank. This code is far from perfect as I am still learning. Even with the excellent instruction from @BlondieBytes, I found that improvising or find errors on my own was a little challenging. If you find something that is out of order or could be done better, please let me know. 9 | 10 | --- 11 | ## Items covered in the tutorial: 12 | 1. Overview of Object Oriented Programming, Classes, and Data Structures 13 | 2. Data Types 14 | 3. Variables and Arithmetic 15 | 4. If Statements 16 | 5. Boolean Operators and Classes vs. Instances 17 | 6. Loops 18 | 7. Making A Mad Lib with Arrays 19 | 8. More Arrays 20 | 9. Dictionaries and Hash Maps 21 | 10. Recursion 22 | 11. Binary 23 | 12. Creating a Library Catalogue From Scratch 24 | 13. Inheritance 25 | 14. Abstract Classes and Casting 26 | 15. Imports, Packages, and Scope 27 | 16. Linked Lists 28 | 17. Exceptions Part 1 29 | 18. Exceptions Part 2 30 | 19. Queues and Stacks 31 | 20. Interfaces 32 | 21. Pointers, Aliasing, Garbage Collection, and Java Virtual Machine 33 | 22. Generics 34 | 23. Heaps and Binary Search Trees 35 | 24. Tic Tac Toe Game From Scratch (*Formatting Issue*) 36 | 25. Hangman Game From Scratch (*Formatting Issue*) 37 | 26. Running Time And Complexity 38 | 27. Unit Testing Part 1 39 | 28. Unit Testing Part 2 40 | 29. Introduction To Databases 41 | 30. Coding In Other Languages 42 | -------------------------------------------------------------------------------- /RecursionPractice.java: -------------------------------------------------------------------------------- 1 | public class RecursionPractice { 2 | 3 | // Compilation of functions example: f(f(f(a))) 4 | //f(a) = 5 + a 5 | //f(20) = 5 + 20 = 25 6 | //f(f(f(20))) = f(f(25)) 7 | //f(25) = 5 + 25 = 30 8 | //f(f(25)) = f(30) 9 | //f(30) = 30 + 5 = 35 10 | //end state is 35 11 | 12 | // Summation example: 5 + 4 + 3 + 2 + 1 13 | // Summation example: 3 + 2 + 1 14 | 15 | public static int Summation(int n) { 16 | // Base Case: We are at the end 17 | if (n <= 0) { 18 | return 0; // additive identity property 19 | } 20 | // Recursive Case: Keep going 21 | else { 22 | // Example using the number 3 as a start 23 | // Step 1: 3 + Summation(2) | Recursive 24 | // Step 2: 3 + 2 + Summation(1) | Recursive 25 | // Step 3: 3 + 2 + 1 + Summation(0) | Recursive 26 | // Step 4: 3 + 2 + 1 + 0 | Base 27 | return n + Summation(n-1); 28 | } 29 | } 30 | 31 | // Example Factorial: 5! 32 | // 5 * 4 * 3 * 2 * 1 33 | public static int Factorial(int n) { 34 | // Base Case: 35 | if (n <= 1) { 36 | return 1; // multiplicative identity 37 | } 38 | // Recurisve Case 39 | else { 40 | // Example using 5 as a start 41 | // Step 1: 5 * Factorial(4) | Recursive 42 | // Step 2: 5 * 4 * Factorial(3) | Recursive 43 | // Step 3: 5 * 4 * 3 * Factorial(2) | Recursive 44 | // Step 4: 5 * 4 * 3 * 2 * Factorial(1) | Recursive 45 | // Step 5: 5 * 4 * 3 * 2 * 1 | Base 46 | return n * Factorial(n-1); 47 | } 48 | } 49 | 50 | // Example Exponential Case: 5^3 51 | // % * 5 * 5 52 | public static int Exponentiation(int n, int p) { 53 | // Base Case: 54 | if (p <= 0) { 55 | return 1; // multiplicative identity 56 | } 57 | // Recursive Case 58 | else { 59 | // Example using (5, 5) to start 60 | // Step 1: 5 * Exponentiation(5, 4) | Recursive 61 | // Step 2: 5 * 5 * Exponentiation(5, 3) | Recursive 62 | // Step 3: 5 * 5 * 5 * Exponentiation(5, 2) | Recursive 63 | // Step 4: 5 * 5 * 5 * 5 * Exponentiation(5, 1) | Recursive 64 | // Step 5: 5 * 5 * 5 * 5 * 5 * Exponentiation(5, 0) | Recursive 65 | // Step 6: 5 * 5 * 5 * 5 * 5 * 1 | Base 66 | return n * Exponentiation(n, p-1); 67 | } 68 | } 69 | public static void main(String[] args) { 70 | System.out.println("Summation " + Summation(5)); 71 | System.out.println("Factorial " + Factorial(5)); 72 | System.out.println("Exponential " + Exponentiation(5, 5)); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /RunTimePractice.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | 3 | public class RunTimePractice { 4 | 5 | public static int findNumberOfRepetitions(String s, char c) { 6 | int sum = 0; 7 | for (int i = 0; i < s.length(); i++) { 8 | if (s.charAt(i) == c) { 9 | sum++; 10 | } 11 | } 12 | return sum; 13 | } 14 | // Brute force solution 15 | public static int[] findNumberOfRepetitionsv1(String s, char[] c) { 16 | int[] sums = new int[c.length]; // 1 time 17 | for (int i = 0 ; i < s.length(); i++) { // 1 time, length of string + 1, set number of times 18 | for (int j = 0; j < c.length; j++) { 19 | if (s.charAt(i) == c[j]) { 20 | sums[j] = sums[j] + 1; 21 | } 22 | } 23 | } 24 | return sums; 25 | } 26 | 27 | public static int[] findNumberOfRepetitionsv2(String s, char[] c) { 28 | // Optimal time: 0(n+m) 29 | int[] sums = new int[c.length]; // 1 time 30 | HashMap map = new HashMap<>(); 31 | for (int i = 0; i < s.length(); i++) { 32 | if (!map.containsKey(s.charAt(i))) { 33 | map.put(s.charAt(i), 1); 34 | } else { 35 | int sum = map.get(s.charAt(i)); 36 | map.put(s.charAt(i), sum+1); 37 | } 38 | } 39 | 40 | for (int j = 0; j < c.length; j++) { 41 | int sum; 42 | if (!map.containsKey(c[j])) { 43 | sums[j] = 0; 44 | } else { 45 | sums[j] = map.get(c[j]); 46 | } 47 | } 48 | 49 | return sums; 50 | } 51 | public static void main(String[] args) { 52 | long startTime = System.currentTimeMillis(); 53 | System.out.println(findNumberOfRepetitions("abcadasgiashdgsiangpiangwpaoewapoyweapnpghoinapoieapiyapiaepwiygnhepwaiynhapiygnhjewpaojepaojygpaoejyapoejypoyjahgpoaj", 'a')); 54 | long endTime = System.currentTimeMillis(); 55 | long duration = endTime - startTime; 56 | System.out.println("Test: " + duration + "ms"); 57 | 58 | char[] a = {'a', 'b'}; 59 | 60 | startTime = System.currentTimeMillis(); 61 | System.out.println(findNumberOfRepetitionsv1("abcadasgiashdgsiangpiangwpaoewapoyweapnpghoinapoieapiyapiaepwiygnhepwaiynhapiygnhjewpaojepaojygpaoejyapoejypoyjahgpoaj", a)); 62 | endTime = System.currentTimeMillis(); 63 | duration = endTime - startTime; 64 | System.out.println("Test: " + duration + "ms"); 65 | 66 | startTime = System.currentTimeMillis(); 67 | System.out.println(findNumberOfRepetitionsv2("abcadasgiashdgsiangpiangwpaoewapoyweapnpghoinapoieapiyapiaepwiygnhepwaiynhapiygnhjewpaojepaojygpaoejyapoejypoyjahgpoaj", a)); 68 | endTime = System.currentTimeMillis(); 69 | duration = endTime - startTime; 70 | System.out.println("Test: " + duration + "ms"); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /StarWarsInterfacePractice/Character.java: -------------------------------------------------------------------------------- 1 | package StarWarsInterfacePractice; 2 | 3 | public interface Character { 4 | public String base = "character"; 5 | public void attack(); 6 | public void heal(); 7 | public String getWeapon(); 8 | } 9 | -------------------------------------------------------------------------------- /StarWarsInterfacePractice/Enemy.java: -------------------------------------------------------------------------------- 1 | package StarWarsInterfacePractice; 2 | 3 | public class Enemy implements Character { 4 | 5 | public String weapon = "lightsaber"; 6 | 7 | public Enemy() { 8 | 9 | } 10 | 11 | public String getWeapon() { 12 | return weapon; 13 | } 14 | 15 | public void attack() { 16 | System.out.println("The enemy attacks YOU!"); 17 | } 18 | 19 | public void heal() { 20 | System.out.println("The enemy heals himself"); 21 | } 22 | 23 | public void weaponDraw() { 24 | System.out.println("draw out weapon"); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /StarWarsInterfacePractice/Hero.java: -------------------------------------------------------------------------------- 1 | package StarWarsInterfacePractice; 2 | 3 | public class Hero implements Character { 4 | public String weapon = "The Force"; 5 | 6 | public String getWeapon() { 7 | return weapon; 8 | } 9 | 10 | public void attack() { 11 | System.out.println("The hero attacks the enemy"); 12 | } 13 | 14 | public void heal() { 15 | System.out.println("The hero heals you"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /StarWarsInterfacePractice/StarWarsInterfacePractice.java: -------------------------------------------------------------------------------- 1 | package StarWarsInterfacePractice; 2 | 3 | import java.util.Random; 4 | 5 | public class StarWarsInterfacePractice { 6 | 7 | public static Character summonCharacter() { 8 | 9 | // Create a new random integer 10 | Random rand = new Random(); 11 | 12 | // If the random integer is even (divisible by 2) then return an enemy to the game 13 | if (Math.abs(rand.nextInt()) % 2 == 0) { 14 | return new Enemy(); 15 | } else { 16 | // If the random integer is odd then return a hero to the game 17 | return new Hero(); 18 | } 19 | } 20 | 21 | public static void main(String[] args) { 22 | // Creating enemies and heroes 23 | Enemy darthVader = new Enemy(); 24 | Hero ObiWanKenobi = new Hero(); 25 | 26 | // Creating the actions of the enemies and heroes 27 | darthVader.attack(); 28 | ObiWanKenobi.attack(); 29 | darthVader.heal(); 30 | ObiWanKenobi.heal(); 31 | 32 | // Listing the weapons used in the fight 33 | System.out.println("Enemy Weapon: " + darthVader.getWeapon()); 34 | System.out.println("Hero Weapon: " + ObiWanKenobi.getWeapon()); 35 | 36 | // Creating a random spy 37 | Character spy = summonCharacter(); 38 | spy.attack(); 39 | spy.heal(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /TicTacToeApplication/AI.java: -------------------------------------------------------------------------------- 1 | package TicTacToeApplication; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Random; 5 | 6 | class AI { 7 | 8 | public int pickSpot(TicTacToe game) { 9 | ArrayList choices = new ArrayList(); 10 | for (int i = 0; i < 9; i++) { 11 | // If the slot is not taken, add it as a choice 12 | if (game.board[i] == '-') { 13 | choices.add(i + 1); 14 | } 15 | } 16 | Random rand = new Random(); 17 | int choice = choices.get(Math.abs(rand.nextInt() % choices.size())); 18 | return choice; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /TicTacToeApplication/TicTacToe.java: -------------------------------------------------------------------------------- 1 | package TicTacToeApplication; 2 | 3 | public class TicTacToe { 4 | 5 | // Picture Of Game With Index: 6 | // For Storage: 7 | // 0 | 1 | 2 8 | // ----------- 9 | // 3 | 4 | 5 10 | // ----------- 11 | // 6 | 7 | 8 12 | // What The User Thinks 13 | // 1 | 2 | 3 14 | // ----------- 15 | // 4 | 5 | 6 16 | // ----------- 17 | // 7 | 8 | 9 18 | 19 | // UI Picture Of Game 20 | // INIT: 21 | // | - | - | - 22 | // ------------- 23 | // | - | - | - 24 | // ------------- 25 | // | - | - | - 26 | // GAMEPLAY: 27 | // | 0 | - | 0 28 | // ------------ 29 | // | - | X | - 30 | // ------------ 31 | // | - | - | X 32 | 33 | // SETTING UP THE GAME: 34 | // Creating the board 35 | protected char[] board; 36 | 37 | // Allowing the user to show their move 38 | protected char userMarker; 39 | 40 | // Allowing the ai to show their move 41 | protected char aiMarker; 42 | 43 | // Declaring the winner 44 | protected char winner; 45 | 46 | // Declaring the current marker 47 | protected char currentMarker; 48 | 49 | // Creating a constructor 50 | public TicTacToe(char playerToken, char aiMarker) { 51 | this.userMarker = playerToken; 52 | this.aiMarker = aiMarker; 53 | this.winner = '-'; 54 | this.board = setBoard(); 55 | this.currentMarker = userMarker; 56 | } 57 | 58 | // Creating the board 59 | public static char[] setBoard() { 60 | char[] board = new char[9]; 61 | for (int i=0; i < board.length; i++) { 62 | board[i] = '-'; 63 | } 64 | return board; 65 | } 66 | 67 | // Playing turns 68 | public boolean playTurn(int spot) { 69 | boolean isValid = withinRange(spot) && !isSpotTaken(spot); 70 | if (isValid) { 71 | board[spot - 1] = currentMarker; 72 | // single line "if" statement 73 | currentMarker = (currentMarker == userMarker) ? aiMarker : userMarker; 74 | } 75 | return isValid; 76 | } 77 | 78 | // Check if the spot is within range 79 | public boolean withinRange(int number) { 80 | return number > 0 && number < board.length + 1; 81 | } 82 | 83 | // Check if the spot is taken 84 | public boolean isSpotTaken(int number) { 85 | return board[number-1] != '-'; 86 | } 87 | 88 | // Print out the board 89 | public void printBoard() { 90 | System.out.println(); 91 | for (int i = 0; i < board.length; i++) { 92 | if (i % 3 == 0 && i != 0) { 93 | System.out.println(); 94 | System.out.println("----------------"); 95 | } 96 | System.out.println(" | " + board[i]); 97 | } 98 | System.out.println(); 99 | } 100 | 101 | public void printIndexBoard() { 102 | System.out.println(); 103 | for (int i = 0; i < board.length; i++) { 104 | if (i % 3 == 0 && i != 0) { 105 | System.out.println(); 106 | System.out.println("----------------"); 107 | } 108 | System.out.println(" | " + (i + 1)); 109 | } 110 | System.out.println(); 111 | } 112 | 113 | public TicTacToe() { 114 | } 115 | 116 | // Checking to see if there are three items in a row to determine a winner 117 | public boolean isThereAWinner() { 118 | boolean diagonalsAndMiddles = (rightDi() || leftDi() || middleRow() || secondCol()) && board[4] != '-'; 119 | boolean topAndFirst = (topRow() || firstCol()) && board[0] != '-'; 120 | boolean bottomAndThird = (bottomRow() || thirdCol()) && board[8] != '-'; 121 | 122 | // Checking for winning combinations 123 | if (diagonalsAndMiddles) { 124 | this.winner = board[4]; 125 | } else if (topAndFirst) { 126 | this.winner = board[0]; 127 | } else if (bottomAndThird) { 128 | this.winner = board[8]; 129 | } 130 | return diagonalsAndMiddles || topAndFirst || bottomAndThird; 131 | } 132 | 133 | public boolean topRow() { 134 | return board[0] == board[1] && board[1] == board[2]; 135 | } 136 | 137 | public boolean middleRow() { 138 | return board[3] == board[4] && board[4] == board[5]; 139 | } 140 | 141 | public boolean bottomRow() { 142 | return board[6] == board[7] && board[7] == board[8]; 143 | } 144 | 145 | public boolean firstCol() { 146 | return board[0] == board[3] && board[3] == board[6]; 147 | } 148 | 149 | public boolean secondCol() { 150 | return board[1] == board[4] && board[4] == board[7]; 151 | } 152 | 153 | public boolean thirdCol() { 154 | return board[2] == board[5] && board[5] == board[8]; 155 | } 156 | 157 | public boolean rightDi() { 158 | return board[0] == board[4] && board[4] == board[8]; 159 | } 160 | 161 | public boolean leftDi() { 162 | return board[2] == board[4] && board[4] == board[6]; 163 | } 164 | 165 | public boolean isTheBoardFilled() { 166 | for (int i=0; i < board.length; i++) { 167 | if (board[i] == '-') { 168 | return false; 169 | } 170 | } 171 | return true; 172 | } 173 | 174 | public String gameOver() { 175 | boolean didSomeoneWin = isThereAWinner(); 176 | if (didSomeoneWin) { 177 | return "We have a winner! The winner is: " + this.winner; 178 | } else if (isTheBoardFilled()) { 179 | return "Draw. Game Over."; 180 | } else { 181 | return "notOver"; 182 | } 183 | } 184 | } 185 | -------------------------------------------------------------------------------- /TicTacToeApplication/TicTacToeApplication.java: -------------------------------------------------------------------------------- 1 | package TicTacToeApplication; 2 | 3 | import java.util.Scanner; 4 | 5 | public class TicTacToeApplication { 6 | 7 | public static void main(String[] args) { 8 | // Getting input 9 | Scanner sc = new Scanner(System.in); 10 | 11 | //Allow for continuous games 12 | boolean doYouWantToPlay = true; 13 | 14 | // Setting up tokens and Artificial Intelligence (AI) 15 | while (doYouWantToPlay) { 16 | System.out.println("Welcome to Tic Tac Toe! You are about to go against the master of Tic Tac Toe. Are you ready? I hope so! \n" + 17 | "But first you must pick the character you want to be and which character I will be"); 18 | 19 | System.out.println(); 20 | System.out.println("Enter a single character that will represent you on the board"); 21 | char playerToken = sc.next().charAt(0); 22 | System.out.println("Enter a single character that will represent your opponent on the board"); 23 | char opponentToken = sc.next().charAt(0); 24 | TicTacToe game = new TicTacToe(playerToken, opponentToken); 25 | AI ai = new AI(); 26 | 27 | // Setting Up The Game 28 | System.out.println(); 29 | System.out.println("Now we can start the game. To play, enter a number and your token shall be put in its place.\n"+ 30 | "The numbers go from 1-9, left to right."); 31 | // TicTacToe.printIndexBoard(); 32 | System.out.println(); 33 | 34 | // Let's Play 35 | while (game.gameOver().equals("notOver")) { 36 | if (game.currentMarker == game.userMarker) { 37 | // USER TURN 38 | System.out.println("It's your turn. Enter a spot for your token"); 39 | int spot = sc.nextInt(); 40 | while(!game.playTurn(spot)) { 41 | System.out.println("Try again. " + spot + " is invalid. This spot is already taken or it is out of range"); 42 | spot = sc.nextInt(); 43 | } 44 | System.out.println("You Picked " + spot); 45 | } else { 46 | // AI TURN 47 | System.out.println("It's my turn"); 48 | // Pick Spot 49 | int aiSpot = ai.pickSpot(game); 50 | game.playTurn(aiSpot); 51 | System.out.println("I picked " + aiSpot + "!"); 52 | } 53 | // Print a new board 54 | System.out.println(); 55 | game.printBoard(); 56 | } 57 | System.out.println(game.gameOver()); 58 | System.out.println(); 59 | 60 | // Set up a new game, or not, depending on the response 61 | System.out.println("do you want to play again? Enter Y if you do. Enter anything else if you do not."); 62 | char response = sc.next().charAt(0); 63 | doYouWantToPlay = (response == 'Y'); 64 | System.out.println(); 65 | System.out.println(); 66 | } 67 | } 68 | } 69 | --------------------------------------------------------------------------------