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 { 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..6ed8f5e --- /dev/null +++ b/myPokemon/Charizard.java @@ -0,0 +1,37 @@ +package myPokemon; +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 + void attack(Pokemon target) { + + double finalDamage; + + if (this.getStrength() > target.getShield()) { + + finalDamage = this.getStrength() - target.getShield(); + + finalDamage = Math.floor(Math.random() * (finalDamage - 1)); + + target.takeDamage((int)finalDamage); + } + } +} diff --git a/myPokemon/Jigglypuff.java b/myPokemon/Jigglypuff.java new file mode 100644 index 0000000..0b5012e --- /dev/null +++ b/myPokemon/Jigglypuff.java @@ -0,0 +1,38 @@ +package myPokemon; +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 + @Override + void attack(Pokemon target) { + + double finalDamage; + + if (this.getStrength() > target.getShield()) { + + finalDamage = this.getStrength() - target.getShield(); + + finalDamage = Math.floor(Math.random() * (finalDamage - 1)); + + target.takeDamage((int)finalDamage); + } + } +} diff --git a/myPokemon/Main.java b/myPokemon/Main.java new file mode 100644 index 0000000..c9d0515 --- /dev/null +++ b/myPokemon/Main.java @@ -0,0 +1,35 @@ +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.getHealth() + " figher two health: " + fighterTwo.getHealth()); + } + + 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..e45b525 --- /dev/null +++ b/myPokemon/Pikachu.java @@ -0,0 +1,38 @@ +package myPokemon; +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 + @Override + void attack(Pokemon target) { + + double finalDamage; + + if (this.getStrength() > target.getShield()) { + + finalDamage = this.getStrength() - target.getShield(); + + finalDamage = Math.floor(Math.random() * (finalDamage - 1)); + + target.takeDamage((int)finalDamage); + } + } +} diff --git a/myPokemon/Pokemon.java b/myPokemon/Pokemon.java new file mode 100644 index 0000000..d818dbf --- /dev/null +++ b/myPokemon/Pokemon.java @@ -0,0 +1,79 @@ +package myPokemon; + +public abstract class Pokemon { + + private String name; + + private int health; + private int strength; + private 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; + + } + + //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