-
-
Notifications
You must be signed in to change notification settings - Fork 5
added ch13 practice problem (recursive merge sort) + progress on ch12 pokemon project #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 18 commits
e2ce36e
519d122
c8b5e49
410957c
eec1212
b21828e
92d9f10
58f87b9
456589a
117fcd3
7c20e93
1c39543
685ad6d
cf4cc79
da5b72a
90d76dd
7d7ac9f
dfe8259
0ccfe1d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package com.codefortomorrow.advanced.chapter13.examples; | ||
|
|
||
| public class BinarySearch { | ||
|
|
||
| // Java implementation of recursive Binary Search | ||
| // Returns index of x if it is present in arr[l.. | ||
| // r], else return -1 | ||
| public static int binarySearch(int arr[], int l, int r, int x) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
| if (r >= l) { | ||
| int mid = l + (r - l) / 2; | ||
|
|
||
| // If the element is present at the | ||
| // middle itself | ||
| if (arr[mid] == x) return mid; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
|
|
||
| // If element is smaller than mid, then | ||
| // it can only be present in left subarray | ||
| if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
|
|
||
| // Else the element can only be present | ||
| // in right subarray | ||
| return binarySearch(arr, mid + 1, r, x); | ||
| } | ||
|
|
||
| // We reach here when element is not present | ||
| // in array | ||
| return -1; | ||
| } | ||
|
|
||
| // Driver method to test above | ||
| public static void main(String args[]) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
| int arr[] = { 2, 3, 4, 10, 40 }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
| int n = arr.length; | ||
| int x = 10; | ||
| int result = binarySearch(arr, 0, n - 1, x); | ||
| if (result == -1) System.out.println( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
| "Element not present" | ||
| ); else System.out.println("Element found at index " + result); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
| } | ||
| /* This code is contributed by Rajat Mishra | ||
| https://www.geeksforgeeks.org/binary-search/ */ | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package com.codefortomorrow.advanced.chapter13.examples; | ||
|
|
||
| public class Fibonacci { | ||
|
|
||
| public static int fibonacciRecursion(int nthNumber) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
| //use recursion | ||
| if (nthNumber == 0) { | ||
| return 0; | ||
| } else if (nthNumber == 1) { | ||
| return 1; | ||
| } | ||
| return ( | ||
| fibonacciRecursion(nthNumber - 1) + | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
| fibonacciRecursion(nthNumber - 2) | ||
| ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
| } | ||
|
|
||
| public static int fibonacciLoop(int nthNumber) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
| //use loop | ||
| int previouspreviousNumber, previousNumber = 0, currentNumber = 1; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
| for (int i = 1; i < nthNumber; i++) { | ||
| previouspreviousNumber = previousNumber; | ||
| previousNumber = currentNumber; | ||
| currentNumber = previouspreviousNumber + previousNumber; | ||
| } | ||
| return currentNumber; | ||
| } | ||
| // Credit: https://dev.to/khalilsaboor/fibonacci-recursion-vs-iteration--474l | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package com.codefortomorrow.advanced.chapter13.practice; | ||
|
|
||
| public class RecurMergeSort { | ||
|
|
||
| public static void recurMergeSort(Comparable[] array, int start, int end) {} | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package com.codefortomorrow.advanced.chapter13.solutions; | ||
|
|
||
| public class RecurMergeSort { | ||
|
|
||
| public static void recurMergeSort(Comparable[] array, int start, int end) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
| if (start >= end) { | ||
| return; | ||
| } | ||
| int middle = (start + end) / 2; | ||
| recurMergeSort(array, start, middle); | ||
| recurMergeSort(array, middle + 1, end); | ||
| int i = start; | ||
| int j = middle + 1; | ||
| Comparable[] sortedArray = new Comparable[end - start + 1]; | ||
| int k = 0; | ||
| while (i <= middle && j <= end) { | ||
| if (array[i].compareTo(array[j]) > 0) { | ||
| sortedArray[k] = array[i]; | ||
| i++; | ||
| } else { | ||
| sortedArray[k] = array[j]; | ||
| j++; | ||
| } | ||
| k++; | ||
| } | ||
| while (i <= middle) { | ||
| sortedArray[k] = array[i]; | ||
| i++; | ||
| k++; | ||
| } | ||
| while (j <= end) { | ||
| sortedArray[k] = array[j]; | ||
| j++; | ||
| k++; | ||
| } | ||
| k = 0; | ||
| for (int l = start; l <= end; l++) { | ||
| array[l] = sortedArray[k]; | ||
| k++; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package com.codefortomorrow.advanced.chapter14.examples; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.FileReader; | ||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Source: https://beginnersbook.com/2014/01/how-to-read-file-in-java-using-bufferedreader/ | ||
| */ | ||
| public class BufferedReaderExample { | ||
|
|
||
| public static void main(String[] args) { | ||
| BufferedReader br = null; | ||
| BufferedReader br2 = null; | ||
|
|
||
| try { | ||
| br = new BufferedReader(new FileReader("B:\\myfile.txt")); | ||
|
|
||
| //One way of reading the file | ||
| System.out.println("Reading the file using readLine() method:"); | ||
| String contentLine = br.readLine(); | ||
| while (contentLine != null) { | ||
| System.out.println(contentLine); | ||
| contentLine = br.readLine(); | ||
| } | ||
|
|
||
| br2 = new BufferedReader(new FileReader("B:\\myfile2.txt")); | ||
|
|
||
| //Second way of reading the file | ||
| System.out.println("Reading the file using read() method:"); | ||
| int num = 0; | ||
| char ch; | ||
| while ((num = br2.read()) != -1) { | ||
| ch = (char) num; | ||
| System.out.print(ch); | ||
| } | ||
| } catch (IOException ioe) { | ||
| ioe.printStackTrace(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| package com.codefortomorrow.advanced.chapter14.practice.account; | ||
|
|
||
| public class InvalidAgeException {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| package com.codefortomorrow.advanced.chapter14.practice.account; | ||
|
|
||
| public class InvalidPasswordException {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| package com.codefortomorrow.advanced.chapter14.practice.account; | ||
|
|
||
| public class InvalidUsernameException {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package com.codefortomorrow.advanced.chapter14.practice.account; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| Scanner sc = new Scanner(System.in); | ||
|
|
||
| System.out.println("Welcome to Account Creation!"); | ||
| while (true) { | ||
| System.out.print("Enter username (4 to 10 characters): "); | ||
| String username = sc.next(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
| System.out.print("Enter age: "); | ||
| int age = sc.nextInt(); | ||
| System.out.print("Enter password (4 to 10 characters): "); | ||
| String password = sc.next(); | ||
| System.out.print("Confirm password (4 to 10 characters): "); | ||
| String confirmPassword = sc.next(); | ||
|
|
||
| // TODO: try and catch to handle exceptions | ||
| createAccount(username, password, age, confirmPassword); | ||
| System.out.println("Account created successfully!"); | ||
| } | ||
| } | ||
|
|
||
| public static void createAccount( | ||
| String username, | ||
| String password, | ||
| int age, | ||
| String confirmPassword | ||
| ) | ||
| throws InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
| // TODO: complete | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| package com.codefortomorrow.advanced.chapter14.practice.account; | ||
|
|
||
| public class PasswordMismatchException {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| package com.codefortomorrow.advanced.chapter14.solutions.account; | ||
|
|
||
| public class InvalidAgeException extends Exception {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| package com.codefortomorrow.advanced.chapter14.solutions.account; | ||
|
|
||
| public class InvalidPasswordException extends Exception {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| package com.codefortomorrow.advanced.chapter14.solutions.account; | ||
|
|
||
| public class InvalidUsernameException extends Exception {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package com.codefortomorrow.advanced.chapter14.solutions.account; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| Scanner sc = new Scanner(System.in); | ||
|
|
||
| System.out.println("Welcome to Account Creation!"); | ||
| while (true) { | ||
| System.out.print("Enter username (4 to 10 characters): "); | ||
| String username = sc.next(); | ||
| System.out.print("Enter age: "); | ||
| int age = sc.nextInt(); | ||
| System.out.print("Enter password (4 to 10 characters): "); | ||
| String password = sc.next(); | ||
| System.out.print("Confirm password (4 to 10 characters): "); | ||
| String confirmPassword = sc.next(); | ||
|
|
||
| try { | ||
| createAccount(username, password, age, confirmPassword); | ||
| System.out.println("Account created successfully!"); | ||
| break; | ||
| } catch (InvalidUsernameException e) { | ||
| System.out.println("Invalid username."); | ||
| } catch (InvalidPasswordException e) { | ||
| System.out.println("Invalid password."); | ||
| } catch (InvalidAgeException e) { | ||
| System.out.println("Must be 18 or older to create an account."); | ||
| } catch (PasswordMismatchException e) { | ||
| System.out.println("Passwords don't match!"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static void createAccount( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
| String username, | ||
| String password, | ||
| int age, | ||
| String confirmPassword | ||
| ) | ||
| throws InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
| if (username.length() < 4 || username.length() > 10) { | ||
| throw new InvalidUsernameException(); | ||
| } | ||
| if (password.length() < 4 || password.length() > 10) { | ||
| throw new InvalidPasswordException(); | ||
| } | ||
| if (age < 18) { | ||
| throw new InvalidAgeException(); | ||
| } | ||
| if (!password.equals(confirmPassword)) { | ||
| throw new PasswordMismatchException(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| package com.codefortomorrow.advanced.chapter14.solutions.account; | ||
|
|
||
| public class PasswordMismatchException extends Exception {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package com.codefortomorrow.intermediate.chapter12.practice.pokemon; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class Battle { | ||
| private Scanner sc = new Scanner(System.in); | ||
| private Pokemon pokemonOne; | ||
| private Pokemon pokemonTwo; | ||
| private int turn; | ||
|
|
||
| public Battle(Pokemon pokemonOne, Pokemon pokemonTwo) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
| this.pokemonOne = pokemonOne; | ||
| this.pokemonTwo = pokemonTwo; | ||
| turn = 1; | ||
| } | ||
|
|
||
| public void runBattle() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
| while (true) { | ||
| if (turn == 1) { | ||
| executeTurn(pokemonOne, pokemonTwo); | ||
| } else { | ||
| executeTurn(pokemonTwo, pokemonOne); | ||
| } | ||
| System.out.println(pokemonOne); | ||
| System.out.println(pokemonTwo); | ||
| if ( | ||
| pokemonOne.getCurrentHP() <= 0 || pokemonTwo.getCurrentHP() <= 0 | ||
| ) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void executeTurn(Pokemon pokemon, Pokemon other) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
| System.out.println("It's " + pokemon.getName() + "'s move!"); | ||
|
|
||
| Move[] pokemonMoves = pokemon.getMoveList(); | ||
| for (int i = 1; i <= pokemonMoves.length; i++) { | ||
| System.out.println(i + ": " + pokemonMoves[i - 1].getName()); | ||
| } | ||
| System.out.print("Move #: "); | ||
| int move = sc.nextInt(); | ||
| pokemon.attack(other, move - 1); | ||
| System.out.println(); | ||
|
|
||
| if (turn == 1) { | ||
| turn = 0; | ||
| } else { | ||
| turn = 1; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[reviewdog] reported by reviewdog 🐶
Missing a Javadoc comment.