Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {

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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[reviewdog] reported by reviewdog 🐶
Array brackets at illegal position.

if (r >= l) {
int mid = l + (r - l) / 2;

// If the element is present at the
// middle itself
if (arr[mid] == x) return mid;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[reviewdog] reported by reviewdog 🐶
'if' construct must use '{}'s.


// 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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[reviewdog] reported by reviewdog 🐶
'if' construct must use '{}'s.


// 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[]) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[reviewdog] reported by reviewdog 🐶
Array brackets at illegal position.

int arr[] = { 2, 3, 4, 10, 40 };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[reviewdog] reported by reviewdog 🐶
Array brackets at illegal position.

int n = arr.length;
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
if (result == -1) System.out.println(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[reviewdog] reported by reviewdog 🐶
'if' construct must use '{}'s.

"Element not present"
); else System.out.println("Element found at index " + result);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[reviewdog] reported by reviewdog 🐶
'else' construct must use '{}'s.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[reviewdog] reported by reviewdog 🐶
Only one statement per line allowed.

}
/* This code is contributed by Rajat Mishra
https://www.geeksforgeeks.org/binary-search/ */

}
29 changes: 29 additions & 0 deletions src/com/codefortomorrow/advanced/chapter13/examples/Fibonacci.java
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) {

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.

//use recursion
if (nthNumber == 0) {
return 0;
} else if (nthNumber == 1) {
return 1;
}
return (
fibonacciRecursion(nthNumber - 1) +

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[reviewdog] reported by reviewdog 🐶
'+' should be on a new line.

fibonacciRecursion(nthNumber - 2)
);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[reviewdog] reported by reviewdog 🐶
'method def' child has incorrect indentation level 8, expected level should be 12.

}

public static int fibonacciLoop(int nthNumber) {

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.

//use loop
int previouspreviousNumber, previousNumber = 0, currentNumber = 1;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[reviewdog] reported by reviewdog 🐶
Each variable declaration must be in its own statement.

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) {

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.

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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[reviewdog] reported by reviewdog 🐶
Distance between variable 'username' declaration and its first usage is 4, but allowed 3. Consider making that variable final if you still need to store its value in advance (before method calls that might have side effects on the original value).

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 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[reviewdog] reported by reviewdog 🐶
Line is longer than 100 characters (found 115).

// 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(

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.

String username,
String password,
int age,
String confirmPassword
)
throws InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[reviewdog] reported by reviewdog 🐶
Line is longer than 100 characters (found 115).

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) {

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.

this.pokemonOne = pokemonOne;
this.pokemonTwo = pokemonTwo;
turn = 1;
}

public void runBattle() {

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.

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) {

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.

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;
}
}
}
Loading