From e2ce36e5d0255e7ec688bb59b4430a110eadaf30 Mon Sep 17 00:00:00 2001 From: Achintya Rajan <39139575+ARajan1084@users.noreply.github.com> Date: Sun, 5 Jul 2020 22:24:14 -0700 Subject: [PATCH 01/15] Progress on new OOP lab --- .../chapter12/practice/pokemon/Move.java | 43 ++++++++++ .../chapter12/practice/pokemon/Pokemon.java | 84 +++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Move.java create mode 100644 src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Move.java b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Move.java new file mode 100644 index 0000000..d9e11f2 --- /dev/null +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Move.java @@ -0,0 +1,43 @@ +package com.codefortomorrow.intermediate.chapter12.practice.pokemon; + +/** + * represents a Pokemon's move + */ +public class Move { + private String name; + private boolean poisoning; + private boolean sleeping; + private boolean paralyzing; + private boolean burning; + private boolean freezing; + private int baseDamage; + + /** + * Constructs a Move object. This has been done for you. + * @param name move's name + * @param poisoning whether the move poisons + * @param sleeping whether the move sleeps + * @param paralyzing whether the move paralyzes + * @param burning whether the move burns + * @param freezing whether the move freezes + * @param baseDamage base damage of the move + */ + public Move(String name, boolean poisoning, boolean sleeping, boolean paralyzing, boolean burning, boolean freezing, int baseDamage) { + this.name = name; + this.poisoning = poisoning; + this.sleeping = sleeping; + this.paralyzing = paralyzing; + this.burning = burning; + this.freezing = freezing; + this.baseDamage = baseDamage; + } + + /** + * calculates the move's damage by multiplying the base damage of the move by the given level + * @param level the Pokemon's level + * @return the move's total damage + */ + public int calculateDamage(int level) { + + } +} diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java new file mode 100644 index 0000000..a58efa6 --- /dev/null +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java @@ -0,0 +1,84 @@ +package com.codefortomorrow.intermediate.chapter12.practice.pokemon; + +/** + * represents a Pokemon object + */ +public class Pokemon { + private Move[] moveList; + private int maxHP; + private int currentHP; + private int XP; + private String species; + private String type; + private String name; + private int level; + private boolean poisoned; + private boolean asleep; + private boolean paralyzed; + private boolean burned; + private boolean frozen; + + /** + * Complete this constructor. The move list should be initialized to an empty array of size 4. + * Both Max HP and Current HP should be set to 40. Name should be set to species. Level should be + * set to 1. XP should be set to 0. All effects should be set to false + * @param species the species of the Pokemon + * @param type type of the Pokemon + */ + public Pokemon(String species, String type) { + + } + + /** + * Complete this constructor. See the above constructor for default values. + * @param species the species of the Pokemon + * @param type of the Pokemon + * @param name the name/nickname of the Pokemon + */ + public Pokemon(String species, String type, String name) { + + } + + /** + * Complete this constructor. See the first constructor for default values. + */ + public Pokemon(Move[] moveList, int maxHP, int XP, String species, String type, String name, int level) { + + } + + /** + * Places given move in the moveList at the given index. If a move was previously present, it + * is replaced, and returned + * @param index the index to place the move + * @param move the move to learn + * @return old move or null if not applicable + */ + public Move learnMove(int index, Move move) { + + } + + /** + * attacks enemy Pokemon with the move at the given moveIndex + * @param enemy enemy Pokemon + * @param moveIndex index of the move to make + */ + public void attack(Pokemon enemy, int moveIndex) { + + } + + /** + * Subtracts damage from currentHP + * @param damage damage to take + */ + public void takeDamage(int damage) { + + } + + /** + * Adds given HP to current HP + * @param hp given hp to heal + */ + public void heal(int hp) { + + } +} From 519d122145545a8d827e11e701cd2944a2e9aa06 Mon Sep 17 00:00:00 2001 From: Achintya Rajan <39139575+ARajan1084@users.noreply.github.com> Date: Wed, 8 Jul 2020 12:44:05 -0700 Subject: [PATCH 02/15] Progress on Pokemon project for Ch 12 --- .../chapter12/practice/pokemon/Battle.java | 29 +++++++++++++++++++ .../chapter12/practice/pokemon/Pokemon.java | 11 ++++++- 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java new file mode 100644 index 0000000..9b5ffd0 --- /dev/null +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java @@ -0,0 +1,29 @@ +package com.codefortomorrow.intermediate.chapter12.practice.pokemon; + +import java.util.Scanner; + +public class Battle { + private Pokemon pokemonOne; + private Pokemon pokemonTwo; + private int turn; + + public Battle (Pokemon pokemonOne, Pokemon pokemonTwo) { + this.pokemonOne = pokemonOne; + this.pokemonTwo = pokemonTwo; + turn = 1; + } + + public void runBattle() { + Scanner sc = new Scanner(System.in); + System.out.println("It's " + pokemonOne.getName() + "'s move!"); + + Move[] pokemonOneMoves = pokemonOne.getMoveList(); + for (int i = 1; i <= pokemonOneMoves.length; i++) { + System.out.println(i + ": " + pokemonOneMoves[i]); + } + System.out.println("Move #: "); + + int move = sc.nextInt(); + pokemonOne.attack(pokemonTwo, move); + } +} \ No newline at end of file diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java index a58efa6..6897958 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java @@ -17,11 +17,12 @@ public class Pokemon { private boolean paralyzed; private boolean burned; private boolean frozen; + private boolean fainted; /** * Complete this constructor. The move list should be initialized to an empty array of size 4. * Both Max HP and Current HP should be set to 40. Name should be set to species. Level should be - * set to 1. XP should be set to 0. All effects should be set to false + * set to 1. XP should be set to 0. All effects should be set to false, including fainted. * @param species the species of the Pokemon * @param type type of the Pokemon */ @@ -81,4 +82,12 @@ public void takeDamage(int damage) { public void heal(int hp) { } + + public String getName() { + + } + + public Move[] getMoveList() { + + } } From c8b5e49660535e5af904dbfffbf8ebd3bd413491 Mon Sep 17 00:00:00 2001 From: Achintya Rajan <39139575+ARajan1084@users.noreply.github.com> Date: Thu, 9 Jul 2020 13:24:51 -0700 Subject: [PATCH 03/15] more progress on Pokemon project --- .../chapter12/practice/pokemon/Battle.java | 30 ++++++++++++++++--- .../chapter12/practice/pokemon/Move.java | 4 +++ .../chapter12/practice/pokemon/Pokemon.java | 14 +++++++-- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java index 9b5ffd0..787dcf9 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java @@ -3,6 +3,7 @@ import java.util.Scanner; public class Battle { + private Scanner sc = new Scanner(System.in); private Pokemon pokemonOne; private Pokemon pokemonTwo; private int turn; @@ -14,16 +15,37 @@ public Battle (Pokemon pokemonOne, Pokemon pokemonTwo) { } public void runBattle() { - Scanner sc = new Scanner(System.in); - System.out.println("It's " + pokemonOne.getName() + "'s move!"); + 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) { + System.out.println("It's " + pokemon.getName() + "'s move!"); - Move[] pokemonOneMoves = pokemonOne.getMoveList(); + Move[] pokemonOneMoves = pokemon.getMoveList(); for (int i = 1; i <= pokemonOneMoves.length; i++) { System.out.println(i + ": " + pokemonOneMoves[i]); } System.out.println("Move #: "); int move = sc.nextInt(); - pokemonOne.attack(pokemonTwo, move); + pokemon.attack(other, move); + + + if (turn == 1) { + turn = 0; + } else { + turn = 1; + } } } \ No newline at end of file diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Move.java b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Move.java index d9e11f2..9ba8790 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Move.java +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Move.java @@ -38,6 +38,10 @@ public Move(String name, boolean poisoning, boolean sleeping, boolean paralyzing * @return the move's total damage */ public int calculateDamage(int level) { + return 0; //TODO: Fix + } + public String getName() { + return name; } } diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java index 6897958..8c827a3 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java @@ -55,7 +55,7 @@ public Pokemon(Move[] moveList, int maxHP, int XP, String species, String type, * @return old move or null if not applicable */ public Move learnMove(int index, Move move) { - + return null; // TODO: Fix } /** @@ -64,7 +64,9 @@ public Move learnMove(int index, Move move) { * @param moveIndex index of the move to make */ public void attack(Pokemon enemy, int moveIndex) { - + // Type your code here + System.out.println(name + " used " + moveList[moveIndex].getName() + " on " + enemy.getName()); + System.out.println(enemy.getName() + " took " + " damage."); // TODO: Fix } /** @@ -84,10 +86,18 @@ public void heal(int hp) { } public String getName() { + return ""; //TODO: Fix + } + public int getCurrentHP() { + return 0; // TODO: Fix } public Move[] getMoveList() { + return null; // TODO: Fix + } + public String toString() { + return name + "\n" + "HP: " + currentHP + " / " + maxHP; } } From 410957c1a9ca634ed9742205215c352566a3b48b Mon Sep 17 00:00:00 2001 From: Achintya Rajan <39139575+ARajan1084@users.noreply.github.com> Date: Mon, 14 Sep 2020 19:59:14 -0700 Subject: [PATCH 04/15] Update .gitignore --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 41a4481..9cf125d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,7 @@ # ignore files c4t-java.iml *.xml -.DS_Store \ No newline at end of file +.DS_Store +*.class +*.zip +*.txt From eec12122bcc9a61c3cc62655b98a9e788874140e Mon Sep 17 00:00:00 2001 From: Achintya Rajan <39139575+ARajan1084@users.noreply.github.com> Date: Mon, 14 Sep 2020 20:02:09 -0700 Subject: [PATCH 05/15] Some more progress on Pokemon project --- .../chapter12/practice/pokemon/Battle.java | 13 +- .../chapter12/practice/pokemon/Main.java | 21 +++ .../chapter12/practice/pokemon/Pokemon.java | 6 +- .../chapter12/solutions/pokemon/Battle.java | 50 +++++++ .../chapter12/solutions/pokemon/Main.java | 21 +++ .../chapter12/solutions/pokemon/Move.java | 47 +++++++ .../chapter12/solutions/pokemon/Pokemon.java | 125 ++++++++++++++++++ 7 files changed, 273 insertions(+), 10 deletions(-) create mode 100644 src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Main.java create mode 100644 src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Battle.java create mode 100644 src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Main.java create mode 100644 src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Move.java create mode 100644 src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Pokemon.java diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java index 787dcf9..120bb87 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java @@ -32,15 +32,14 @@ public void runBattle() { public void executeTurn(Pokemon pokemon, Pokemon other) { System.out.println("It's " + pokemon.getName() + "'s move!"); - Move[] pokemonOneMoves = pokemon.getMoveList(); - for (int i = 1; i <= pokemonOneMoves.length; i++) { - System.out.println(i + ": " + pokemonOneMoves[i]); + Move[] pokemonMoves = pokemon.getMoveList(); + for (int i = 1; i <= pokemonMoves.length; i++) { + System.out.println(i + ": " + pokemonMoves[i-1].getName()); } - System.out.println("Move #: "); - + System.out.print("Move #: "); int move = sc.nextInt(); - pokemon.attack(other, move); - + pokemon.attack(other, move-1); + System.out.println(); if (turn == 1) { turn = 0; diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Main.java b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Main.java new file mode 100644 index 0000000..21d876d --- /dev/null +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Main.java @@ -0,0 +1,21 @@ +package com.codefortomorrow.intermediate.chapter12.practice.pokemon; + +public class Main { + public static void main(String[]args) { + Move a1 = new Move("Water Gun", false, false, false, false, false, 15); + Move a2 = new Move("Tackle", false, false, false, false, false, 10); + Move a3 = new Move("Bubble", false, false, false, false, false, 16); + Move a4 = new Move("Water Splash", false, false, false, false, false, 6); + Move[] moveListA = {a1, a2, a3, a4}; + Pokemon a = new Pokemon(moveListA, 40, 0, "Squirtle", "Water", "Squirtle", 1); + + Move b1 = new Move("Thunder Shock", false, false, false, false, false, 25); + Move b2 = new Move("Slam", false, false, false, false, false, 16); + Move b3 = new Move("Quick Attack", false, false, false, false, false, 10); + Move b4 = new Move("Thunder", false, false, false, false, false, 6); + Move[] moveListB = {b1, b2, b3, b4}; + Pokemon b = new Pokemon(moveListB, 30, 0, "Pikachu", "Electric", "Pikachu", 1); + Battle battle = new Battle(a, b); + battle.runBattle(); + } +} diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java index 8c827a3..bb16e03 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java @@ -70,11 +70,11 @@ public void attack(Pokemon enemy, int moveIndex) { } /** - * Subtracts damage from currentHP + * Subtracts damage from currentHP. Returns true if Pokemon fainted * @param damage damage to take */ - public void takeDamage(int damage) { - + public boolean takeDamage(int damage) { + return false; //TODO: Fix } /** diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Battle.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Battle.java new file mode 100644 index 0000000..4e2b2ca --- /dev/null +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Battle.java @@ -0,0 +1,50 @@ +package com.codefortomorrow.intermediate.chapter12.solutions.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) { + this.pokemonOne = pokemonOne; + this.pokemonTwo = pokemonTwo; + turn = 1; + } + + public void runBattle() { + 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) { + 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; + } + } +} \ No newline at end of file diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Main.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Main.java new file mode 100644 index 0000000..2009c03 --- /dev/null +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Main.java @@ -0,0 +1,21 @@ +package com.codefortomorrow.intermediate.chapter12.solutions.pokemon; + +public class Main { + public static void main(String[]args) { + Move a1 = new Move("Water Gun", false, false, false, false, false, 15); + Move a2 = new Move("Tackle", false, false, false, false, false, 10); + Move a3 = new Move("Bubble", false, false, false, false, false, 16); + Move a4 = new Move("Water Splash", false, false, false, false, false, 6); + Move[] moveListA = {a1, a2, a3, a4}; + Pokemon a = new Pokemon(moveListA, 40, 0, "Squirtle", "Water", "Squirtle", 1); + + Move b1 = new Move("Thunder Shock", false, false, false, false, false, 25); + Move b2 = new Move("Slam", false, false, false, false, false, 16); + Move b3 = new Move("Quick Attack", false, false, false, false, false, 10); + Move b4 = new Move("Thunder", false, false, false, false, false, 6); + Move[] moveListB = {b1, b2, b3, b4}; + Pokemon b = new Pokemon(moveListB, 30, 0, "Pikachu", "Electric", "Pikachu", 1); + Battle battle = new Battle(a, b); + battle.runBattle(); + } +} diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Move.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Move.java new file mode 100644 index 0000000..063b3d2 --- /dev/null +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Move.java @@ -0,0 +1,47 @@ +package com.codefortomorrow.intermediate.chapter12.solutions.pokemon; + +/** + * represents a Pokemon's move + */ +public class Move { + private String name; + private boolean poisoning; + private boolean sleeping; + private boolean paralyzing; + private boolean burning; + private boolean freezing; + private int baseDamage; + + /** + * Constructs a Move object. This has been done for you. + * @param name move's name + * @param poisoning whether the move poisons + * @param sleeping whether the move sleeps + * @param paralyzing whether the move paralyzes + * @param burning whether the move burns + * @param freezing whether the move freezes + * @param baseDamage base damage of the move + */ + public Move(String name, boolean poisoning, boolean sleeping, boolean paralyzing, boolean burning, boolean freezing, int baseDamage) { + this.name = name; + this.poisoning = poisoning; + this.sleeping = sleeping; + this.paralyzing = paralyzing; + this.burning = burning; + this.freezing = freezing; + this.baseDamage = baseDamage; + } + + /** + * calculates the move's damage by multiplying the base damage of the move by the given level + * @param level the Pokemon's level + * @return the move's total damage + */ + public int calculateDamage(int level) { + return level * baseDamage; + } + + public String getName() { + return name; + } +} diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Pokemon.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Pokemon.java new file mode 100644 index 0000000..e2309b2 --- /dev/null +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Pokemon.java @@ -0,0 +1,125 @@ +package com.codefortomorrow.intermediate.chapter12.solutions.pokemon; + +/** + * represents a Pokemon object + */ +public class Pokemon { + private Move[] moveList; + private int maxHP; + private int currentHP; + private int XP; + private String species; + private String type; + private String name; + private int level; + private boolean poisoned; + private boolean asleep; + private boolean paralyzed; + private boolean burned; + private boolean frozen; + private boolean fainted; + + /** + * Complete this constructor. The move list should be initialized to an empty array of size 4. + * Both Max HP and Current HP should be set to 40. Name should be set to species. Level should be + * set to 1. XP should be set to 0. All effects should be set to false, including fainted. + * @param species the species of the Pokemon + * @param type type of the Pokemon + */ + public Pokemon(String species, String type) { + this.species = species; + this.type = type; + } + + /** + * Complete this constructor. See the above constructor for default values. + * @param species the species of the Pokemon + * @param type of the Pokemon + * @param name the name/nickname of the Pokemon + */ + public Pokemon(String species, String type, String name) { + this.species = species; + this.type = type; + this.name = name; + } + + /** + * Complete this constructor. See the first constructor for default values. + */ + public Pokemon(Move[] moveList, int maxHP, int XP, String species, String type, String name, int level) { + this.moveList = moveList; + this.maxHP = maxHP; + this.XP = XP; + this.species = species; + this.type = type; + this.name = name; + this.level = level; + currentHP = maxHP; + } + + /** + * Places given move in the moveList at the given index. If a move was previously present, it + * is replaced, and returned + * @param index the index to place the move + * @param move the move to learn + * @return old move or null if not applicable + */ + public Move learnMove(int index, Move move) { + Move oldMove = moveList[index]; + moveList[index] = move; + return oldMove; + } + + /** + * attacks enemy Pokemon with the move at the given moveIndex + * @param enemy enemy Pokemon + * @param moveIndex index of the move to make + */ + public void attack(Pokemon enemy, int moveIndex) { + int damage = moveList[moveIndex].calculateDamage(level); + enemy.takeDamage(damage); + System.out.println(name + " used " + moveList[moveIndex].getName() + " on " + enemy.getName()); + System.out.println(enemy.getName() + " took " + damage + " damage."); + } + + /** + * Subtracts damage from currentHP. Returns true if Pokemon fainted + * @param damage damage to take + */ + public boolean takeDamage(int damage) { + currentHP -= damage; + if (currentHP < 0) { + currentHP = 0; + return true; + } + return false; + } + + /** + * Adds given HP to current HP + * @param hp given hp to heal + */ + public void heal(int hp) { + if (currentHP + hp > maxHP) { + currentHP = maxHP; + return; + } + currentHP += hp; + } + + public String getName() { + return name; + } + + public int getCurrentHP() { + return currentHP; + } + + public Move[] getMoveList() { + return moveList; + } + + public String toString() { + return name + "\n" + "HP: " + currentHP + " / " + maxHP; + } +} From 92d9f1034a2a86e45ed0855f67c924d03e9f0e43 Mon Sep 17 00:00:00 2001 From: Achintya Rajan <39139575+ARajan1084@users.noreply.github.com> Date: Mon, 14 Sep 2020 20:07:11 -0700 Subject: [PATCH 06/15] added recursive merge sort practice problem --- .../chapter13/practice/RecurMergeSort.java | 7 ++++ .../chapter13/solutions/RecurMergeSort.java | 41 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/com/codefortomorrow/advanced/chapter13/practice/RecurMergeSort.java create mode 100644 src/com/codefortomorrow/advanced/chapter13/solutions/RecurMergeSort.java diff --git a/src/com/codefortomorrow/advanced/chapter13/practice/RecurMergeSort.java b/src/com/codefortomorrow/advanced/chapter13/practice/RecurMergeSort.java new file mode 100644 index 0000000..01ea20a --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter13/practice/RecurMergeSort.java @@ -0,0 +1,7 @@ +package com.codefortomorrow.advanced.chapter13.practice; + +public class RecurMergeSort { + public static void recurMergeSort(Comparable[] array, int start, int end) { + + } +} diff --git a/src/com/codefortomorrow/advanced/chapter13/solutions/RecurMergeSort.java b/src/com/codefortomorrow/advanced/chapter13/solutions/RecurMergeSort.java new file mode 100644 index 0000000..ef62ae4 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter13/solutions/RecurMergeSort.java @@ -0,0 +1,41 @@ +package com.codefortomorrow.advanced.chapter13.solutions; + +public class RecurMergeSort { + public static void recurMergeSort(Comparable[] array, int start, int end) { + 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++; + } + } +} From 58f87b94e368844d226aca39ce5d193fad70e5a9 Mon Sep 17 00:00:00 2001 From: ARajan1084 Date: Sat, 26 Sep 2020 21:18:40 +0000 Subject: [PATCH 07/15] Bot: Prettified Java code! --- .../chapter13/practice/RecurMergeSort.java | 3 +- .../chapter13/solutions/RecurMergeSort.java | 1 + .../chapter12/practice/pokemon/Battle.java | 14 +++-- .../chapter12/practice/pokemon/Main.java | 57 ++++++++++++++++--- .../chapter12/practice/pokemon/Move.java | 10 +++- .../chapter12/practice/pokemon/Pokemon.java | 32 ++++++----- .../chapter12/solutions/pokemon/Battle.java | 14 +++-- .../chapter12/solutions/pokemon/Main.java | 57 ++++++++++++++++--- .../chapter12/solutions/pokemon/Move.java | 10 +++- .../chapter12/solutions/pokemon/Pokemon.java | 18 +++++- 10 files changed, 169 insertions(+), 47 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter13/practice/RecurMergeSort.java b/src/com/codefortomorrow/advanced/chapter13/practice/RecurMergeSort.java index 01ea20a..9708f35 100644 --- a/src/com/codefortomorrow/advanced/chapter13/practice/RecurMergeSort.java +++ b/src/com/codefortomorrow/advanced/chapter13/practice/RecurMergeSort.java @@ -1,7 +1,6 @@ package com.codefortomorrow.advanced.chapter13.practice; public class RecurMergeSort { - public static void recurMergeSort(Comparable[] array, int start, int end) { - } + public static void recurMergeSort(Comparable[] array, int start, int end) {} } diff --git a/src/com/codefortomorrow/advanced/chapter13/solutions/RecurMergeSort.java b/src/com/codefortomorrow/advanced/chapter13/solutions/RecurMergeSort.java index ef62ae4..da7c9c9 100644 --- a/src/com/codefortomorrow/advanced/chapter13/solutions/RecurMergeSort.java +++ b/src/com/codefortomorrow/advanced/chapter13/solutions/RecurMergeSort.java @@ -1,6 +1,7 @@ package com.codefortomorrow.advanced.chapter13.solutions; public class RecurMergeSort { + public static void recurMergeSort(Comparable[] array, int start, int end) { if (start >= end) { return; diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java index 120bb87..961e20b 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java @@ -8,14 +8,14 @@ public class Battle { private Pokemon pokemonTwo; private int turn; - public Battle (Pokemon pokemonOne, Pokemon pokemonTwo) { + public Battle(Pokemon pokemonOne, Pokemon pokemonTwo) { this.pokemonOne = pokemonOne; this.pokemonTwo = pokemonTwo; turn = 1; } public void runBattle() { - while(true) { + while (true) { if (turn == 1) { executeTurn(pokemonOne, pokemonTwo); } else { @@ -23,7 +23,9 @@ public void runBattle() { } System.out.println(pokemonOne); System.out.println(pokemonTwo); - if (pokemonOne.getCurrentHP() <= 0 || pokemonTwo.getCurrentHP() <= 0) { + if ( + pokemonOne.getCurrentHP() <= 0 || pokemonTwo.getCurrentHP() <= 0 + ) { break; } } @@ -34,11 +36,11 @@ public void executeTurn(Pokemon pokemon, Pokemon other) { Move[] pokemonMoves = pokemon.getMoveList(); for (int i = 1; i <= pokemonMoves.length; i++) { - System.out.println(i + ": " + pokemonMoves[i-1].getName()); + System.out.println(i + ": " + pokemonMoves[i - 1].getName()); } System.out.print("Move #: "); int move = sc.nextInt(); - pokemon.attack(other, move-1); + pokemon.attack(other, move - 1); System.out.println(); if (turn == 1) { @@ -47,4 +49,4 @@ public void executeTurn(Pokemon pokemon, Pokemon other) { turn = 1; } } -} \ No newline at end of file +} diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Main.java b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Main.java index 21d876d..185a151 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Main.java +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Main.java @@ -1,20 +1,61 @@ package com.codefortomorrow.intermediate.chapter12.practice.pokemon; public class Main { - public static void main(String[]args) { + + public static void main(String[] args) { Move a1 = new Move("Water Gun", false, false, false, false, false, 15); Move a2 = new Move("Tackle", false, false, false, false, false, 10); Move a3 = new Move("Bubble", false, false, false, false, false, 16); - Move a4 = new Move("Water Splash", false, false, false, false, false, 6); - Move[] moveListA = {a1, a2, a3, a4}; - Pokemon a = new Pokemon(moveListA, 40, 0, "Squirtle", "Water", "Squirtle", 1); + Move a4 = new Move( + "Water Splash", + false, + false, + false, + false, + false, + 6 + ); + Move[] moveListA = { a1, a2, a3, a4 }; + Pokemon a = new Pokemon( + moveListA, + 40, + 0, + "Squirtle", + "Water", + "Squirtle", + 1 + ); - Move b1 = new Move("Thunder Shock", false, false, false, false, false, 25); + Move b1 = new Move( + "Thunder Shock", + false, + false, + false, + false, + false, + 25 + ); Move b2 = new Move("Slam", false, false, false, false, false, 16); - Move b3 = new Move("Quick Attack", false, false, false, false, false, 10); + Move b3 = new Move( + "Quick Attack", + false, + false, + false, + false, + false, + 10 + ); Move b4 = new Move("Thunder", false, false, false, false, false, 6); - Move[] moveListB = {b1, b2, b3, b4}; - Pokemon b = new Pokemon(moveListB, 30, 0, "Pikachu", "Electric", "Pikachu", 1); + Move[] moveListB = { b1, b2, b3, b4 }; + Pokemon b = new Pokemon( + moveListB, + 30, + 0, + "Pikachu", + "Electric", + "Pikachu", + 1 + ); Battle battle = new Battle(a, b); battle.runBattle(); } diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Move.java b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Move.java index 9ba8790..7e2559f 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Move.java +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Move.java @@ -22,7 +22,15 @@ public class Move { * @param freezing whether the move freezes * @param baseDamage base damage of the move */ - public Move(String name, boolean poisoning, boolean sleeping, boolean paralyzing, boolean burning, boolean freezing, int baseDamage) { + public Move( + String name, + boolean poisoning, + boolean sleeping, + boolean paralyzing, + boolean burning, + boolean freezing, + int baseDamage + ) { this.name = name; this.poisoning = poisoning; this.sleeping = sleeping; diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java index bb16e03..277ffd6 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java @@ -26,9 +26,7 @@ public class Pokemon { * @param species the species of the Pokemon * @param type type of the Pokemon */ - public Pokemon(String species, String type) { - - } + public Pokemon(String species, String type) {} /** * Complete this constructor. See the above constructor for default values. @@ -36,16 +34,20 @@ public Pokemon(String species, String type) { * @param type of the Pokemon * @param name the name/nickname of the Pokemon */ - public Pokemon(String species, String type, String name) { - - } + public Pokemon(String species, String type, String name) {} /** * Complete this constructor. See the first constructor for default values. */ - public Pokemon(Move[] moveList, int maxHP, int XP, String species, String type, String name, int level) { - - } + public Pokemon( + Move[] moveList, + int maxHP, + int XP, + String species, + String type, + String name, + int level + ) {} /** * Places given move in the moveList at the given index. If a move was previously present, it @@ -65,7 +67,13 @@ public Move learnMove(int index, Move move) { */ public void attack(Pokemon enemy, int moveIndex) { // Type your code here - System.out.println(name + " used " + moveList[moveIndex].getName() + " on " + enemy.getName()); + System.out.println( + name + + " used " + + moveList[moveIndex].getName() + + " on " + + enemy.getName() + ); System.out.println(enemy.getName() + " took " + " damage."); // TODO: Fix } @@ -81,9 +89,7 @@ public boolean takeDamage(int damage) { * Adds given HP to current HP * @param hp given hp to heal */ - public void heal(int hp) { - - } + public void heal(int hp) {} public String getName() { return ""; //TODO: Fix diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Battle.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Battle.java index 4e2b2ca..9689388 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Battle.java +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Battle.java @@ -8,14 +8,14 @@ public class Battle { private Pokemon pokemonTwo; private int turn; - public Battle (Pokemon pokemonOne, Pokemon pokemonTwo) { + public Battle(Pokemon pokemonOne, Pokemon pokemonTwo) { this.pokemonOne = pokemonOne; this.pokemonTwo = pokemonTwo; turn = 1; } public void runBattle() { - while(true) { + while (true) { if (turn == 1) { executeTurn(pokemonOne, pokemonTwo); } else { @@ -23,7 +23,9 @@ public void runBattle() { } System.out.println(pokemonOne); System.out.println(pokemonTwo); - if (pokemonOne.getCurrentHP() <= 0 || pokemonTwo.getCurrentHP() <= 0) { + if ( + pokemonOne.getCurrentHP() <= 0 || pokemonTwo.getCurrentHP() <= 0 + ) { break; } } @@ -34,11 +36,11 @@ public void executeTurn(Pokemon pokemon, Pokemon other) { Move[] pokemonMoves = pokemon.getMoveList(); for (int i = 1; i <= pokemonMoves.length; i++) { - System.out.println(i + ": " + pokemonMoves[i-1].getName()); + System.out.println(i + ": " + pokemonMoves[i - 1].getName()); } System.out.print("Move #: "); int move = sc.nextInt(); - pokemon.attack(other, move-1); + pokemon.attack(other, move - 1); System.out.println(); if (turn == 1) { @@ -47,4 +49,4 @@ public void executeTurn(Pokemon pokemon, Pokemon other) { turn = 1; } } -} \ No newline at end of file +} diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Main.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Main.java index 2009c03..edc0a34 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Main.java +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Main.java @@ -1,20 +1,61 @@ package com.codefortomorrow.intermediate.chapter12.solutions.pokemon; public class Main { - public static void main(String[]args) { + + public static void main(String[] args) { Move a1 = new Move("Water Gun", false, false, false, false, false, 15); Move a2 = new Move("Tackle", false, false, false, false, false, 10); Move a3 = new Move("Bubble", false, false, false, false, false, 16); - Move a4 = new Move("Water Splash", false, false, false, false, false, 6); - Move[] moveListA = {a1, a2, a3, a4}; - Pokemon a = new Pokemon(moveListA, 40, 0, "Squirtle", "Water", "Squirtle", 1); + Move a4 = new Move( + "Water Splash", + false, + false, + false, + false, + false, + 6 + ); + Move[] moveListA = { a1, a2, a3, a4 }; + Pokemon a = new Pokemon( + moveListA, + 40, + 0, + "Squirtle", + "Water", + "Squirtle", + 1 + ); - Move b1 = new Move("Thunder Shock", false, false, false, false, false, 25); + Move b1 = new Move( + "Thunder Shock", + false, + false, + false, + false, + false, + 25 + ); Move b2 = new Move("Slam", false, false, false, false, false, 16); - Move b3 = new Move("Quick Attack", false, false, false, false, false, 10); + Move b3 = new Move( + "Quick Attack", + false, + false, + false, + false, + false, + 10 + ); Move b4 = new Move("Thunder", false, false, false, false, false, 6); - Move[] moveListB = {b1, b2, b3, b4}; - Pokemon b = new Pokemon(moveListB, 30, 0, "Pikachu", "Electric", "Pikachu", 1); + Move[] moveListB = { b1, b2, b3, b4 }; + Pokemon b = new Pokemon( + moveListB, + 30, + 0, + "Pikachu", + "Electric", + "Pikachu", + 1 + ); Battle battle = new Battle(a, b); battle.runBattle(); } diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Move.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Move.java index 063b3d2..257c231 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Move.java +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Move.java @@ -22,7 +22,15 @@ public class Move { * @param freezing whether the move freezes * @param baseDamage base damage of the move */ - public Move(String name, boolean poisoning, boolean sleeping, boolean paralyzing, boolean burning, boolean freezing, int baseDamage) { + public Move( + String name, + boolean poisoning, + boolean sleeping, + boolean paralyzing, + boolean burning, + boolean freezing, + int baseDamage + ) { this.name = name; this.poisoning = poisoning; this.sleeping = sleeping; diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Pokemon.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Pokemon.java index e2309b2..2050900 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Pokemon.java +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Pokemon.java @@ -46,7 +46,15 @@ public Pokemon(String species, String type, String name) { /** * Complete this constructor. See the first constructor for default values. */ - public Pokemon(Move[] moveList, int maxHP, int XP, String species, String type, String name, int level) { + public Pokemon( + Move[] moveList, + int maxHP, + int XP, + String species, + String type, + String name, + int level + ) { this.moveList = moveList; this.maxHP = maxHP; this.XP = XP; @@ -78,7 +86,13 @@ public Move learnMove(int index, Move move) { public void attack(Pokemon enemy, int moveIndex) { int damage = moveList[moveIndex].calculateDamage(level); enemy.takeDamage(damage); - System.out.println(name + " used " + moveList[moveIndex].getName() + " on " + enemy.getName()); + System.out.println( + name + + " used " + + moveList[moveIndex].getName() + + " on " + + enemy.getName() + ); System.out.println(enemy.getName() + " took " + damage + " damage."); } From 456589aac6437f29ac4abf311a1e3c9412537bdf Mon Sep 17 00:00:00 2001 From: Achintya Rajan <39139575+ARajan1084@users.noreply.github.com> Date: Sat, 26 Sep 2020 16:35:55 -0700 Subject: [PATCH 08/15] adds example code for binary search in ch 13 --- .../chapter13/examples/BinarySearch.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/com/codefortomorrow/advanced/chapter13/examples/BinarySearch.java diff --git a/src/com/codefortomorrow/advanced/chapter13/examples/BinarySearch.java b/src/com/codefortomorrow/advanced/chapter13/examples/BinarySearch.java new file mode 100644 index 0000000..4825685 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter13/examples/BinarySearch.java @@ -0,0 +1,47 @@ +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) + { + if (r >= l) { + int mid = l + (r - l) / 2; + + // If the element is present at the + // middle itself + if (arr[mid] == x) + return mid; + + // 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); + + // 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[]) + { + int arr[] = { 2, 3, 4, 10, 40 }; + int n = arr.length; + int x = 10; + int result = binarySearch(arr, 0, n - 1, x); + if (result == -1) + System.out.println("Element not present"); + else + System.out.println("Element found at index " + result); + } + /* This code is contributed by Rajat Mishra + https://www.geeksforgeeks.org/binary-search/ */ + +} From 117fcd34b4fd2c309b3505c13c86cd26cdedc78a Mon Sep 17 00:00:00 2001 From: ARajan1084 Date: Sat, 26 Sep 2020 23:36:37 +0000 Subject: [PATCH 09/15] Bot: Prettified Java code! --- .../chapter13/examples/BinarySearch.java | 72 +++++++++---------- 1 file changed, 34 insertions(+), 38 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter13/examples/BinarySearch.java b/src/com/codefortomorrow/advanced/chapter13/examples/BinarySearch.java index 4825685..276c8e1 100644 --- a/src/com/codefortomorrow/advanced/chapter13/examples/BinarySearch.java +++ b/src/com/codefortomorrow/advanced/chapter13/examples/BinarySearch.java @@ -1,46 +1,42 @@ 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) - { - if (r >= l) { - int mid = l + (r - l) / 2; - - // If the element is present at the - // middle itself - if (arr[mid] == x) - return mid; - - // 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); - - // 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[]) - { - int arr[] = { 2, 3, 4, 10, 40 }; - int n = arr.length; - int x = 10; - int result = binarySearch(arr, 0, n - 1, x); - if (result == -1) - System.out.println("Element not present"); - else - System.out.println("Element found at index " + result); + // 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) { + if (r >= l) { + int mid = l + (r - l) / 2; + + // If the element is present at the + // middle itself + if (arr[mid] == x) return mid; + + // 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); + + // 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[]) { + int arr[] = { 2, 3, 4, 10, 40 }; + int n = arr.length; + int x = 10; + int result = binarySearch(arr, 0, n - 1, x); + if (result == -1) System.out.println( + "Element not present" + ); else System.out.println("Element found at index " + result); + } /* This code is contributed by Rajat Mishra https://www.geeksforgeeks.org/binary-search/ */ From 7c20e93f8c52ecf5231898112bdb576608f59515 Mon Sep 17 00:00:00 2001 From: Achintya Rajan <39139575+ARajan1084@users.noreply.github.com> Date: Sat, 26 Sep 2020 16:41:55 -0700 Subject: [PATCH 10/15] added source code for Fibonacci example in Recursion vs. Iteration section for Ch 13 --- .../chapter13/examples/Fibonacci.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/com/codefortomorrow/advanced/chapter13/examples/Fibonacci.java diff --git a/src/com/codefortomorrow/advanced/chapter13/examples/Fibonacci.java b/src/com/codefortomorrow/advanced/chapter13/examples/Fibonacci.java new file mode 100644 index 0000000..3925267 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter13/examples/Fibonacci.java @@ -0,0 +1,26 @@ +package com.codefortomorrow.advanced.chapter13.examples; + +public class Fibonacci { + public static int fibonacciRecursion(int nthNumber) { + //use recursion + if (nthNumber == 0) { + return 0; + } else if (nthNumber == 1) { + return 1; + } + return fibonacciRecursion(nthNumber - 1) + fibonacciRecursion(nthNumber - 2); + } + + public static int fibonacciLoop(int nthNumber) { + //use loop + int previouspreviousNumber, previousNumber = 0, currentNumber = 1; + 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 +} From 685ad6d7892045c801c49b51e58c3384798c9151 Mon Sep 17 00:00:00 2001 From: ARajan1084 Date: Sat, 26 Sep 2020 23:42:53 +0000 Subject: [PATCH 11/15] Bot: Prettified Java code! --- .../advanced/chapter13/examples/Fibonacci.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter13/examples/Fibonacci.java b/src/com/codefortomorrow/advanced/chapter13/examples/Fibonacci.java index 3925267..700e810 100644 --- a/src/com/codefortomorrow/advanced/chapter13/examples/Fibonacci.java +++ b/src/com/codefortomorrow/advanced/chapter13/examples/Fibonacci.java @@ -1,6 +1,7 @@ package com.codefortomorrow.advanced.chapter13.examples; public class Fibonacci { + public static int fibonacciRecursion(int nthNumber) { //use recursion if (nthNumber == 0) { @@ -8,19 +9,21 @@ public static int fibonacciRecursion(int nthNumber) { } else if (nthNumber == 1) { return 1; } - return fibonacciRecursion(nthNumber - 1) + fibonacciRecursion(nthNumber - 2); + return ( + fibonacciRecursion(nthNumber - 1) + + fibonacciRecursion(nthNumber - 2) + ); } public static int fibonacciLoop(int nthNumber) { //use loop int previouspreviousNumber, previousNumber = 0, currentNumber = 1; - for (int i = 1; i < nthNumber ; i++) { + 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 } From cf4cc79de51454d172cb9b6d97eca123263ae07d Mon Sep 17 00:00:00 2001 From: Achintya Rajan <39139575+ARajan1084@users.noreply.github.com> Date: Sat, 26 Sep 2020 17:19:50 -0700 Subject: [PATCH 12/15] added source code for BufferedReader example in ch 14 --- .../examples/BufferedReaderExample.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/com/codefortomorrow/advanced/chapter14/examples/BufferedReaderExample.java diff --git a/src/com/codefortomorrow/advanced/chapter14/examples/BufferedReaderExample.java b/src/com/codefortomorrow/advanced/chapter14/examples/BufferedReaderExample.java new file mode 100644 index 0000000..bcb39ff --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/examples/BufferedReaderExample.java @@ -0,0 +1,46 @@ +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(); + } + } +} + From 90d76ddfe0f809c1ac8dde7e0f5f9816b072f221 Mon Sep 17 00:00:00 2001 From: ARajan1084 Date: Sun, 27 Sep 2020 00:20:32 +0000 Subject: [PATCH 13/15] Bot: Prettified Java code! --- .../chapter14/examples/BufferedReaderExample.java | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter14/examples/BufferedReaderExample.java b/src/com/codefortomorrow/advanced/chapter14/examples/BufferedReaderExample.java index bcb39ff..c43fd98 100644 --- a/src/com/codefortomorrow/advanced/chapter14/examples/BufferedReaderExample.java +++ b/src/com/codefortomorrow/advanced/chapter14/examples/BufferedReaderExample.java @@ -8,8 +8,8 @@ * Source: https://beginnersbook.com/2014/01/how-to-read-file-in-java-using-bufferedreader/ */ public class BufferedReaderExample { - public static void main(String[] args) { + public static void main(String[] args) { BufferedReader br = null; BufferedReader br2 = null; @@ -28,19 +28,14 @@ public static void main(String[] args) { //Second way of reading the file System.out.println("Reading the file using read() method:"); - int num=0; + int num = 0; char ch; - while((num=br2.read()) != -1) - { - ch=(char)num; + while ((num = br2.read()) != -1) { + ch = (char) num; System.out.print(ch); } - - } - catch (IOException ioe) - { + } catch (IOException ioe) { ioe.printStackTrace(); } } } - From 7d7ac9f23be75d5c1542704f46fb5bc316e1ada4 Mon Sep 17 00:00:00 2001 From: Achintya Rajan <39139575+ARajan1084@users.noreply.github.com> Date: Sun, 27 Sep 2020 13:58:23 -0700 Subject: [PATCH 14/15] added low difficulty exception handling practice problem --- .../practice/account/InvalidAgeException.java | 4 ++ .../account/InvalidPasswordException.java | 4 ++ .../account/InvalidUsernameException.java | 4 ++ .../chapter14/practice/account/Main.java | 31 +++++++++++ .../account/PasswordMismatchException.java | 4 ++ .../account/InvalidAgeException.java | 4 ++ .../account/InvalidPasswordException.java | 4 ++ .../account/InvalidUsernameException.java | 4 ++ .../chapter14/solutions/account/Main.java | 52 +++++++++++++++++++ .../account/PasswordMismatchException.java | 4 ++ 10 files changed, 115 insertions(+) create mode 100644 src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidAgeException.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidPasswordException.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidUsernameException.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/practice/account/Main.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/practice/account/PasswordMismatchException.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidAgeException.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidPasswordException.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidUsernameException.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/solutions/account/Main.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/solutions/account/PasswordMismatchException.java diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidAgeException.java b/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidAgeException.java new file mode 100644 index 0000000..a9b8f06 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidAgeException.java @@ -0,0 +1,4 @@ +package com.codefortomorrow.advanced.chapter14.practice.account; + +public class InvalidAgeException { +} diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidPasswordException.java b/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidPasswordException.java new file mode 100644 index 0000000..8cb1423 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidPasswordException.java @@ -0,0 +1,4 @@ +package com.codefortomorrow.advanced.chapter14.practice.account; + +public class InvalidPasswordException { +} diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidUsernameException.java b/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidUsernameException.java new file mode 100644 index 0000000..a84a756 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidUsernameException.java @@ -0,0 +1,4 @@ +package com.codefortomorrow.advanced.chapter14.practice.account; + +public class InvalidUsernameException { +} diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/account/Main.java b/src/com/codefortomorrow/advanced/chapter14/practice/account/Main.java new file mode 100644 index 0000000..b8d9552 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/practice/account/Main.java @@ -0,0 +1,31 @@ +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(); + 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 { + // TODO: complete + } +} diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/account/PasswordMismatchException.java b/src/com/codefortomorrow/advanced/chapter14/practice/account/PasswordMismatchException.java new file mode 100644 index 0000000..59f58a3 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/practice/account/PasswordMismatchException.java @@ -0,0 +1,4 @@ +package com.codefortomorrow.advanced.chapter14.practice.account; + +public class PasswordMismatchException { +} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidAgeException.java b/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidAgeException.java new file mode 100644 index 0000000..ab749c3 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidAgeException.java @@ -0,0 +1,4 @@ +package com.codefortomorrow.advanced.chapter14.solutions.account; + +public class InvalidAgeException extends Exception{ +} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidPasswordException.java b/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidPasswordException.java new file mode 100644 index 0000000..0292c62 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidPasswordException.java @@ -0,0 +1,4 @@ +package com.codefortomorrow.advanced.chapter14.solutions.account; + +public class InvalidPasswordException extends Exception { +} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidUsernameException.java b/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidUsernameException.java new file mode 100644 index 0000000..bfec7d8 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidUsernameException.java @@ -0,0 +1,4 @@ +package com.codefortomorrow.advanced.chapter14.solutions.account; + +public class InvalidUsernameException extends Exception { +} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/account/Main.java b/src/com/codefortomorrow/advanced/chapter14/solutions/account/Main.java new file mode 100644 index 0000000..d709a57 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/account/Main.java @@ -0,0 +1,52 @@ +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(String username, String password, int age, String confirmPassword) throws + InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException { + 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(); + } + } +} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/account/PasswordMismatchException.java b/src/com/codefortomorrow/advanced/chapter14/solutions/account/PasswordMismatchException.java new file mode 100644 index 0000000..520931b --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/account/PasswordMismatchException.java @@ -0,0 +1,4 @@ +package com.codefortomorrow.advanced.chapter14.solutions.account; + +public class PasswordMismatchException extends Exception { +} From dfe8259dba91b9e5821b1feae2de9fe4c0eee120 Mon Sep 17 00:00:00 2001 From: ARajan1084 Date: Sun, 27 Sep 2020 20:58:57 +0000 Subject: [PATCH 15/15] Bot: Prettified Java code! --- .../practice/account/InvalidAgeException.java | 3 +-- .../practice/account/InvalidPasswordException.java | 3 +-- .../practice/account/InvalidUsernameException.java | 3 +-- .../advanced/chapter14/practice/account/Main.java | 13 +++++++++---- .../practice/account/PasswordMismatchException.java | 3 +-- .../solutions/account/InvalidAgeException.java | 3 +-- .../solutions/account/InvalidPasswordException.java | 3 +-- .../solutions/account/InvalidUsernameException.java | 3 +-- .../advanced/chapter14/solutions/account/Main.java | 13 +++++++++---- .../account/PasswordMismatchException.java | 3 +-- 10 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidAgeException.java b/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidAgeException.java index a9b8f06..0842368 100644 --- a/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidAgeException.java +++ b/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidAgeException.java @@ -1,4 +1,3 @@ package com.codefortomorrow.advanced.chapter14.practice.account; -public class InvalidAgeException { -} +public class InvalidAgeException {} diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidPasswordException.java b/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidPasswordException.java index 8cb1423..f6f6029 100644 --- a/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidPasswordException.java +++ b/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidPasswordException.java @@ -1,4 +1,3 @@ package com.codefortomorrow.advanced.chapter14.practice.account; -public class InvalidPasswordException { -} +public class InvalidPasswordException {} diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidUsernameException.java b/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidUsernameException.java index a84a756..677b349 100644 --- a/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidUsernameException.java +++ b/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidUsernameException.java @@ -1,4 +1,3 @@ package com.codefortomorrow.advanced.chapter14.practice.account; -public class InvalidUsernameException { -} +public class InvalidUsernameException {} diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/account/Main.java b/src/com/codefortomorrow/advanced/chapter14/practice/account/Main.java index b8d9552..5dcaf7a 100644 --- a/src/com/codefortomorrow/advanced/chapter14/practice/account/Main.java +++ b/src/com/codefortomorrow/advanced/chapter14/practice/account/Main.java @@ -3,7 +3,8 @@ import java.util.Scanner; public class Main { - public static void main(String[]args) { + + public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Welcome to Account Creation!"); @@ -21,11 +22,15 @@ public static void main(String[]args) { 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 { + public static void createAccount( + String username, + String password, + int age, + String confirmPassword + ) + throws InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException { // TODO: complete } } diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/account/PasswordMismatchException.java b/src/com/codefortomorrow/advanced/chapter14/practice/account/PasswordMismatchException.java index 59f58a3..e2f7c98 100644 --- a/src/com/codefortomorrow/advanced/chapter14/practice/account/PasswordMismatchException.java +++ b/src/com/codefortomorrow/advanced/chapter14/practice/account/PasswordMismatchException.java @@ -1,4 +1,3 @@ package com.codefortomorrow.advanced.chapter14.practice.account; -public class PasswordMismatchException { -} +public class PasswordMismatchException {} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidAgeException.java b/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidAgeException.java index ab749c3..17084ef 100644 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidAgeException.java +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidAgeException.java @@ -1,4 +1,3 @@ package com.codefortomorrow.advanced.chapter14.solutions.account; -public class InvalidAgeException extends Exception{ -} +public class InvalidAgeException extends Exception {} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidPasswordException.java b/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidPasswordException.java index 0292c62..8f65f31 100644 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidPasswordException.java +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidPasswordException.java @@ -1,4 +1,3 @@ package com.codefortomorrow.advanced.chapter14.solutions.account; -public class InvalidPasswordException extends Exception { -} +public class InvalidPasswordException extends Exception {} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidUsernameException.java b/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidUsernameException.java index bfec7d8..68ccbc2 100644 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidUsernameException.java +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidUsernameException.java @@ -1,4 +1,3 @@ package com.codefortomorrow.advanced.chapter14.solutions.account; -public class InvalidUsernameException extends Exception { -} +public class InvalidUsernameException extends Exception {} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/account/Main.java b/src/com/codefortomorrow/advanced/chapter14/solutions/account/Main.java index d709a57..2000e41 100644 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/account/Main.java +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/account/Main.java @@ -3,7 +3,8 @@ import java.util.Scanner; public class Main { - public static void main(String[]args) { + + public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Welcome to Account Creation!"); @@ -31,11 +32,15 @@ public static void main(String[]args) { System.out.println("Passwords don't match!"); } } - } - public static void createAccount(String username, String password, int age, String confirmPassword) throws - InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException { + public static void createAccount( + String username, + String password, + int age, + String confirmPassword + ) + throws InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException { if (username.length() < 4 || username.length() > 10) { throw new InvalidUsernameException(); } diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/account/PasswordMismatchException.java b/src/com/codefortomorrow/advanced/chapter14/solutions/account/PasswordMismatchException.java index 520931b..248d178 100644 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/account/PasswordMismatchException.java +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/account/PasswordMismatchException.java @@ -1,4 +1,3 @@ package com.codefortomorrow.advanced.chapter14.solutions.account; -public class PasswordMismatchException extends Exception { -} +public class PasswordMismatchException extends Exception {}