From afa05ed0c083cbda72773ea7f969a7c62f10de32 Mon Sep 17 00:00:00 2001 From: Voyager162 Date: Fri, 25 Oct 2024 10:38:07 -0700 Subject: [PATCH 1/4] finished pokemon assignment --- Pokemon/README.md | 2 +- myPokemon/Charizard.java | 29 +++++++++++++++++++++++++++++ myPokemon/Jigglypuff.java | 29 +++++++++++++++++++++++++++++ myPokemon/Main.java | 27 +++++++++++++++++++++++++++ myPokemon/Pikachu.java | 29 +++++++++++++++++++++++++++++ myPokemon/Pokemon.java | 29 +++++++++++++++++++++++++++++ 6 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 myPokemon/Charizard.java create mode 100644 myPokemon/Jigglypuff.java create mode 100644 myPokemon/Main.java create mode 100644 myPokemon/Pikachu.java create mode 100644 myPokemon/Pokemon.java diff --git a/Pokemon/README.md b/Pokemon/README.md index d84f7ac..f38e710 100644 --- a/Pokemon/README.md +++ b/Pokemon/README.md @@ -13,7 +13,7 @@ b. Must have an instance variable for name - c. Must have a non-default constructor taking in parameters for initial health and name values and settingthe instance variables to those values + c. Must have a non-default constructor taking in parameters for initial health and name values and setting the instance variables to those values d. Must have a defined (regular) method “take damage” that intakes an integer parameter and subtracts that value from health diff --git a/myPokemon/Charizard.java b/myPokemon/Charizard.java new file mode 100644 index 0000000..645a4d4 --- /dev/null +++ b/myPokemon/Charizard.java @@ -0,0 +1,29 @@ +package myPokemon; +import java.util.Random; + +public class Charizard extends Pokemon { + Random rand = new Random(); + @Override + //code to attack, deals damage equal to a random integer between 0 and the attackers strength subtracted by the defenders shield + void attack(Pokemon target) { + double finalDamage; + if (this.strength > target.shield) { + finalDamage = this.strength - target.shield; + finalDamage = Math.floor(Math.random() * (finalDamage - 1)); + target.takeDamage((int)finalDamage); + } + } + //basic constructer + Charizard() { + super("charizard", 12); + this.shield = 5; + this.strength = 18; + + } + //custom constructer + Charizard(String name, int health, int shield, int strength) { + super(name, health); + this.shield = shield; + this.strength = strength; + } +} diff --git a/myPokemon/Jigglypuff.java b/myPokemon/Jigglypuff.java new file mode 100644 index 0000000..a67eb84 --- /dev/null +++ b/myPokemon/Jigglypuff.java @@ -0,0 +1,29 @@ +package myPokemon; +import java.util.Random; + +public class Jigglypuff extends Pokemon{ + Random rand = new Random(); + //code to attack, deals damage equal to a random integer between 0 and the attackers strength subtracted by the defenders shield + @Override + void attack(Pokemon target) { + double finalDamage; + if (this.strength > target.shield) { + finalDamage = this.strength - target.shield; + finalDamage = Math.floor(Math.random() * (finalDamage - 1)); + target.takeDamage((int)finalDamage); + } + } + //basic constructer + Jigglypuff() { + super("Jigglypuff", 35); + this.shield = 5; + this.strength = 8; + + } + //custom constructer + Jigglypuff(String name, int health, int shield, int strength) { + super(name, health); + this.shield = shield; + this.strength = strength; + } +} diff --git a/myPokemon/Main.java b/myPokemon/Main.java new file mode 100644 index 0000000..714d55b --- /dev/null +++ b/myPokemon/Main.java @@ -0,0 +1,27 @@ +package myPokemon; + +public class Main { + + //Main program to create two basic pokemon and have them fight and two custom pokemon to fight + public static void main(String[] args) { + Charizard charizard = new Charizard(); + Pikachu pikachu = new Pikachu(); + System.out.println(fight(charizard, pikachu) + " WINS!!!!"); + + Charizard charizardCustom = new Charizard("Chary", 20, 8, 16); + Jigglypuff JigglypuffCustom = new Jigglypuff("jiggly", 28, 12, 12); + System.out.println(fight(charizardCustom, JigglypuffCustom) + " WINS!!!!"); + } + + + //method to have the pokemon fight + public static String fight(Pokemon fighterOne, Pokemon fighterTwo) { + while (!fighterOne.getIsFainted() && !fighterTwo.getIsFainted()) { + fighterOne.attack(fighterTwo); + fighterTwo.attack(fighterOne); + System.out.println("fighter one health: " + fighterOne.health + " figher two health: " + fighterTwo.health); + } + if (fighterOne.getIsFainted()) return fighterTwo.getName(); + else return fighterOne.getName(); + } +} \ No newline at end of file diff --git a/myPokemon/Pikachu.java b/myPokemon/Pikachu.java new file mode 100644 index 0000000..c644160 --- /dev/null +++ b/myPokemon/Pikachu.java @@ -0,0 +1,29 @@ +package myPokemon; +import java.util.Random; + +public class Pikachu extends Pokemon{ + Random rand = new Random(); + //code to attack, deals damage equal to a random integer between 0 and the attackers strength subtracted by the defenders shield + @Override + void attack(Pokemon target) { + double finalDamage; + if (this.strength > target.shield) { + finalDamage = this.strength - target.shield; + finalDamage = Math.floor(Math.random() * (finalDamage - 1)); + target.takeDamage((int)finalDamage); + } + } + //basic constructer + Pikachu() { + super("Pikachu", 14); + this.shield = 12; + this.strength = 10; + + } + //custom constructer + Pikachu(String name, int health, int shield, int strength) { + super(name, health); + this.shield = shield; + this.strength = strength; + } +} diff --git a/myPokemon/Pokemon.java b/myPokemon/Pokemon.java new file mode 100644 index 0000000..1b79ee6 --- /dev/null +++ b/myPokemon/Pokemon.java @@ -0,0 +1,29 @@ +package myPokemon; + +public abstract class Pokemon { + private String name; + int health; + int strength; + int shield; + //basic super constructer + Pokemon(String name, int health) { + this.name = name; + this.health = health; + } + //construture to take damage from an attack + public void takeDamage(int damage) { + this.health -= damage; + } + //abstract method to attack + abstract void attack(Pokemon target); + + //checks if the health is below 0 + public boolean getIsFainted() { + return this.health <= 0; + } + + //gets the name of the pokemon + public String getName() { + return this.name; + } +} From f17297eb783b112d065872e1555e321f07e3d34c Mon Sep 17 00:00:00 2001 From: Voyager162 Date: Fri, 25 Oct 2024 11:05:22 -0700 Subject: [PATCH 2/4] adjusted all programs for readability --- myPokemon/Charizard.java | 8 ++++++++ myPokemon/Jigglypuff.java | 9 +++++++++ myPokemon/Main.java | 8 ++++++++ myPokemon/Pikachu.java | 10 ++++++++++ myPokemon/Pokemon.java | 12 ++++++++++++ 5 files changed, 47 insertions(+) diff --git a/myPokemon/Charizard.java b/myPokemon/Charizard.java index 645a4d4..aae466d 100644 --- a/myPokemon/Charizard.java +++ b/myPokemon/Charizard.java @@ -6,15 +6,21 @@ public class Charizard extends Pokemon { @Override //code to attack, deals damage equal to a random integer between 0 and the attackers strength subtracted by the defenders shield void attack(Pokemon target) { + double finalDamage; + if (this.strength > target.shield) { + finalDamage = this.strength - target.shield; + finalDamage = Math.floor(Math.random() * (finalDamage - 1)); + target.takeDamage((int)finalDamage); } } //basic constructer Charizard() { + super("charizard", 12); this.shield = 5; this.strength = 18; @@ -22,8 +28,10 @@ void attack(Pokemon target) { } //custom constructer Charizard(String name, int health, int shield, int strength) { + super(name, health); this.shield = shield; this.strength = strength; + } } diff --git a/myPokemon/Jigglypuff.java b/myPokemon/Jigglypuff.java index a67eb84..541b4db 100644 --- a/myPokemon/Jigglypuff.java +++ b/myPokemon/Jigglypuff.java @@ -3,18 +3,25 @@ public class Jigglypuff extends Pokemon{ Random rand = new Random(); + //code to attack, deals damage equal to a random integer between 0 and the attackers strength subtracted by the defenders shield @Override void attack(Pokemon target) { + double finalDamage; + if (this.strength > target.shield) { + finalDamage = this.strength - target.shield; + finalDamage = Math.floor(Math.random() * (finalDamage - 1)); + target.takeDamage((int)finalDamage); } } //basic constructer Jigglypuff() { + super("Jigglypuff", 35); this.shield = 5; this.strength = 8; @@ -22,8 +29,10 @@ void attack(Pokemon target) { } //custom constructer Jigglypuff(String name, int health, int shield, int strength) { + super(name, health); this.shield = shield; this.strength = strength; + } } diff --git a/myPokemon/Main.java b/myPokemon/Main.java index 714d55b..60dd06f 100644 --- a/myPokemon/Main.java +++ b/myPokemon/Main.java @@ -6,22 +6,30 @@ public class Main { public static void main(String[] args) { Charizard charizard = new Charizard(); Pikachu pikachu = new Pikachu(); + System.out.println(fight(charizard, pikachu) + " WINS!!!!"); Charizard charizardCustom = new Charizard("Chary", 20, 8, 16); Jigglypuff JigglypuffCustom = new Jigglypuff("jiggly", 28, 12, 12); + System.out.println(fight(charizardCustom, JigglypuffCustom) + " WINS!!!!"); } //method to have the pokemon fight + public static String fight(Pokemon fighterOne, Pokemon fighterTwo) { + while (!fighterOne.getIsFainted() && !fighterTwo.getIsFainted()) { + fighterOne.attack(fighterTwo); fighterTwo.attack(fighterOne); + System.out.println("fighter one health: " + fighterOne.health + " figher two health: " + fighterTwo.health); } + if (fighterOne.getIsFainted()) return fighterTwo.getName(); + else return fighterOne.getName(); } } \ No newline at end of file diff --git a/myPokemon/Pikachu.java b/myPokemon/Pikachu.java index c644160..1f71b29 100644 --- a/myPokemon/Pikachu.java +++ b/myPokemon/Pikachu.java @@ -2,19 +2,27 @@ import java.util.Random; public class Pikachu extends Pokemon{ + Random rand = new Random(); + //code to attack, deals damage equal to a random integer between 0 and the attackers strength subtracted by the defenders shield @Override void attack(Pokemon target) { + double finalDamage; + if (this.strength > target.shield) { + finalDamage = this.strength - target.shield; + finalDamage = Math.floor(Math.random() * (finalDamage - 1)); + target.takeDamage((int)finalDamage); } } //basic constructer Pikachu() { + super("Pikachu", 14); this.shield = 12; this.strength = 10; @@ -22,8 +30,10 @@ void attack(Pokemon target) { } //custom constructer Pikachu(String name, int health, int shield, int strength) { + super(name, health); this.shield = shield; this.strength = strength; + } } diff --git a/myPokemon/Pokemon.java b/myPokemon/Pokemon.java index 1b79ee6..afab4ec 100644 --- a/myPokemon/Pokemon.java +++ b/myPokemon/Pokemon.java @@ -1,29 +1,41 @@ package myPokemon; public abstract class Pokemon { + private String name; + int health; int strength; int shield; + //basic super constructer Pokemon(String name, int health) { + this.name = name; this.health = health; + } + //construture to take damage from an attack public void takeDamage(int damage) { + this.health -= damage; + } //abstract method to attack abstract void attack(Pokemon target); //checks if the health is below 0 public boolean getIsFainted() { + return this.health <= 0; + } //gets the name of the pokemon public String getName() { + return this.name; + } } From e178197e414007e1857a4911946883c488e34c5c Mon Sep 17 00:00:00 2001 From: Voyager162 Date: Fri, 25 Oct 2024 11:06:19 -0700 Subject: [PATCH 3/4] fixed answers --- Pokemon/Answers/Geodude.java | 2 +- Pokemon/Answers/Jigglypuff.java | 2 +- Pokemon/Answers/Main.java | 2 +- Pokemon/Answers/Pokemon.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Pokemon/Answers/Geodude.java b/Pokemon/Answers/Geodude.java index 5102c22..770748f 100644 --- a/Pokemon/Answers/Geodude.java +++ b/Pokemon/Answers/Geodude.java @@ -1,4 +1,4 @@ -package JavaTraining2024.Pokemon.Answers; +package Pokemon.Answers; public class Geodude extends Pokemon { diff --git a/Pokemon/Answers/Jigglypuff.java b/Pokemon/Answers/Jigglypuff.java index d4be853..1e96c76 100644 --- a/Pokemon/Answers/Jigglypuff.java +++ b/Pokemon/Answers/Jigglypuff.java @@ -1,4 +1,4 @@ -package JavaTraining2024.Pokemon.Answers; +package Pokemon.Answers; public class Jigglypuff extends Pokemon { diff --git a/Pokemon/Answers/Main.java b/Pokemon/Answers/Main.java index 03928bc..646e399 100644 --- a/Pokemon/Answers/Main.java +++ b/Pokemon/Answers/Main.java @@ -1,4 +1,4 @@ -package JavaTraining2024.Pokemon.Answers; +package Pokemon.Answers; public class Main { public static void main(String[] args) { diff --git a/Pokemon/Answers/Pokemon.java b/Pokemon/Answers/Pokemon.java index 465c743..2400134 100644 --- a/Pokemon/Answers/Pokemon.java +++ b/Pokemon/Answers/Pokemon.java @@ -1,4 +1,4 @@ -package JavaTraining2024.Pokemon.Answers; +package Pokemon.Answers; public abstract class Pokemon { From 1561d56010a02eadb9c633276e0437801ed085d6 Mon Sep 17 00:00:00 2001 From: Voyager162 Date: Sun, 27 Oct 2024 20:01:25 -0700 Subject: [PATCH 4/4] Added in check for understanding, moved constructer methods to the top of the pokemon child classes, made all the instance variables in the parent pokemon class private --- myPokemon/Charizard.java | 36 +++++++++++++------------- myPokemon/Jigglypuff.java | 36 +++++++++++++------------- myPokemon/Main.java | 2 +- myPokemon/Pikachu.java | 35 +++++++++++++------------- myPokemon/Pokemon.java | 46 +++++++++++++++++++++++++++++++--- myPokemon/Understanding.txt | 50 +++++++++++++++++++++++++++++++++++++ 6 files changed, 146 insertions(+), 59 deletions(-) create mode 100644 myPokemon/Understanding.txt diff --git a/myPokemon/Charizard.java b/myPokemon/Charizard.java index aae466d..6ed8f5e 100644 --- a/myPokemon/Charizard.java +++ b/myPokemon/Charizard.java @@ -2,6 +2,22 @@ import java.util.Random; public class Charizard extends Pokemon { + //basic constructer + Charizard() { + + super("charizard", 12); + this.setShield(5); + this.setStrength(18); + + } + //custom constructer + Charizard(String name, int health, int shield, int strength) { + + super(name, health); + this.setShield(shield); + this.setStrength(strength); + + } Random rand = new Random(); @Override //code to attack, deals damage equal to a random integer between 0 and the attackers strength subtracted by the defenders shield @@ -9,29 +25,13 @@ void attack(Pokemon target) { double finalDamage; - if (this.strength > target.shield) { + if (this.getStrength() > target.getShield()) { - finalDamage = this.strength - target.shield; + finalDamage = this.getStrength() - target.getShield(); finalDamage = Math.floor(Math.random() * (finalDamage - 1)); target.takeDamage((int)finalDamage); } } - //basic constructer - Charizard() { - - super("charizard", 12); - this.shield = 5; - this.strength = 18; - - } - //custom constructer - Charizard(String name, int health, int shield, int strength) { - - super(name, health); - this.shield = shield; - this.strength = strength; - - } } diff --git a/myPokemon/Jigglypuff.java b/myPokemon/Jigglypuff.java index 541b4db..0b5012e 100644 --- a/myPokemon/Jigglypuff.java +++ b/myPokemon/Jigglypuff.java @@ -2,6 +2,22 @@ import java.util.Random; public class Jigglypuff extends Pokemon{ + //basic constructer + Jigglypuff() { + + super("Jigglypuff", 35); + this.setShield(5); + this.setStrength(8); + + } + //custom constructer + Jigglypuff(String name, int health, int shield, int strength) { + + super(name, health); + this.setShield(shield); + this.setStrength(strength); + + } Random rand = new Random(); //code to attack, deals damage equal to a random integer between 0 and the attackers strength subtracted by the defenders shield @@ -10,29 +26,13 @@ void attack(Pokemon target) { double finalDamage; - if (this.strength > target.shield) { + if (this.getStrength() > target.getShield()) { - finalDamage = this.strength - target.shield; + finalDamage = this.getStrength() - target.getShield(); finalDamage = Math.floor(Math.random() * (finalDamage - 1)); target.takeDamage((int)finalDamage); } } - //basic constructer - Jigglypuff() { - - super("Jigglypuff", 35); - this.shield = 5; - this.strength = 8; - - } - //custom constructer - Jigglypuff(String name, int health, int shield, int strength) { - - super(name, health); - this.shield = shield; - this.strength = strength; - - } } diff --git a/myPokemon/Main.java b/myPokemon/Main.java index 60dd06f..c9d0515 100644 --- a/myPokemon/Main.java +++ b/myPokemon/Main.java @@ -25,7 +25,7 @@ public static String fight(Pokemon fighterOne, Pokemon fighterTwo) { fighterOne.attack(fighterTwo); fighterTwo.attack(fighterOne); - System.out.println("fighter one health: " + fighterOne.health + " figher two health: " + fighterTwo.health); + System.out.println("fighter one health: " + fighterOne.getHealth() + " figher two health: " + fighterTwo.getHealth()); } if (fighterOne.getIsFainted()) return fighterTwo.getName(); diff --git a/myPokemon/Pikachu.java b/myPokemon/Pikachu.java index 1f71b29..e45b525 100644 --- a/myPokemon/Pikachu.java +++ b/myPokemon/Pikachu.java @@ -2,7 +2,22 @@ import java.util.Random; public class Pikachu extends Pokemon{ + //basic constructer + Pikachu() { + + super("Pikachu", 14); + this.setShield(12); + this.setStrength(10); + + } + //custom constructer + Pikachu(String name, int health, int shield, int strength) { + super(name, health); + this.setShield(shield); + this.setStrength(strength); + + } Random rand = new Random(); //code to attack, deals damage equal to a random integer between 0 and the attackers strength subtracted by the defenders shield @@ -11,29 +26,13 @@ void attack(Pokemon target) { double finalDamage; - if (this.strength > target.shield) { + if (this.getStrength() > target.getShield()) { - finalDamage = this.strength - target.shield; + finalDamage = this.getStrength() - target.getShield(); finalDamage = Math.floor(Math.random() * (finalDamage - 1)); target.takeDamage((int)finalDamage); } } - //basic constructer - Pikachu() { - - super("Pikachu", 14); - this.shield = 12; - this.strength = 10; - - } - //custom constructer - Pikachu(String name, int health, int shield, int strength) { - - super(name, health); - this.shield = shield; - this.strength = strength; - - } } diff --git a/myPokemon/Pokemon.java b/myPokemon/Pokemon.java index afab4ec..d818dbf 100644 --- a/myPokemon/Pokemon.java +++ b/myPokemon/Pokemon.java @@ -4,9 +4,9 @@ public abstract class Pokemon { private String name; - int health; - int strength; - int shield; + private int health; + private int strength; + private int shield; //basic super constructer Pokemon(String name, int health) { @@ -32,10 +32,48 @@ public boolean getIsFainted() { } - //gets the name of the pokemon + //getters public String getName() { return this.name; } + public int getStrength() { + + return this.strength; + + } + public int getHealth() { + + return this.health; + + } + public int getShield() { + + return this.shield; + + } + + //setters + + public void setName(String name) { + + this.name = name; + + } + public void setStrength(int strength) { + + this.strength = strength; + + } + public void setHealth(int health) { + + this.health = health; + + } + public void setShield(int shield) { + + this.shield = shield; + + } } diff --git a/myPokemon/Understanding.txt b/myPokemon/Understanding.txt new file mode 100644 index 0000000..373c7d3 --- /dev/null +++ b/myPokemon/Understanding.txt @@ -0,0 +1,50 @@ +A. Explain why Pokemon should be an abstract class + + Pokemon should be an abstract class because instances of that class should never be instantiated. Pokemon is a category, not a class. +It provides certain characteristics but should is missing some key details that a pokemon needs such as the type of pokemon, what +subclass it is, and characteristics that are selective to only that type. Another example of an abstract class would be food. You would +never create an object that is simply food, it would fit into certain categories, then sub-categories and sometimes even further. + + An abstract class also provides instructions to a future editor, user, or reader of your code. It tells them that this class is +simply a main categorie not a class of itself. This would provide furthur clarification and clarity of your code allowing you to become +a better programmer. + +B. Explain why inheriting from Pokemon is useful + + Inheriting from a pokemon is useful because it allows certain code to be saved from being rewritten. You could have simply took all +the methods and variables from the pokemon class and added them to each seperate pokemon class, but problems arise with that scenario. + + One, if you wish to make a slight adjustment to one of those methods or variables it requires you to go into each of your classes to rewrite +all those changes versus simply making the slight adjustment to the main class. Two, it also allows more clear reading of your code allowing +the reader to not be overwhelmed by all the methodes and variables stuffed into each class instead of most of it being organized into the +parent class. Third, one of the main rules of coding is to never repeat yourself, and if you do not make a parent-child class setup you are +doomed to break this rule at some point. + +C. Explain an example of method polymorphism in your code + + I used method polymorphism for each of the constructers for each of the classes. For each class I had two constructures, a +default one that set the standard variables to something pretty common for that pokemon, for the second I allowed for the coder to add in their +own values to customize the pokemon instead of having to write the code to customize it later using the set functions. + +D. Explain an example of subtype polymorphism in your code + + I used subtype polymorphism when I defined an abstract attack method in the parent Pokemon class, and had each child Pokémon class override +it with unique behavior. When I call attack on a Pokemon reference, Java dynamically selects the appropriate method implementation based on the +actual type of Pokémon. This allows each subclass of pokemon to define its specific attack functionality. + +E. Explain why you made one of your methods public + + In all of my getters and setters in the main pokemon class I wanted to make those methods public because it would allow the user to access +all of those variables. If those methods were not public then the user would not be able to access all of the private variables that I had defined +in my pokemon class. + + I also made another function in the main method that made two pokemon fight each other until one of them was dead. I named this method fight +and I decided to make it public becuase in case I decided to expand my code to different functions and differnet scenarios it would be useful +to call this function in those other classes because fighting is such a crucial element of the game. + +F. Explain why you made one of your instance variables private + + All of my instance variables in the pokemon class are private because it provided future coders with instrunctions that these variables should +not change often. For example, the name of a pokemon does not change very often because it is such a critical part of thier identity. I added in public +getter and setters though in the case that the variables needed to be changed in the future. Like in the actual game of pokemon, I assume that you are +allowed to change your pokemon's name, but it does not happen very often \ No newline at end of file