diff --git a/Pokemon/Answers/Geodude.java b/Pokemon/Answers/Geodude.java deleted file mode 100644 index 5102c22..0000000 --- a/Pokemon/Answers/Geodude.java +++ /dev/null @@ -1,21 +0,0 @@ -package JavaTraining2024.Pokemon.Answers; - -public class Geodude extends Pokemon { - - // default constructor, setting to 20 health and naming the pokemon "Geodude" - public Geodude() { - super(18, Geodude.class.getSimpleName()); // Fancy way of getting the name of the class - } - - // non-default constructor, allowing the user to specify Geodude's health and name - public Geodude(int health, String name) { - super(health, name); - } - - // Attack method for damaging an opposing pokemon - @Override - public void attack(Pokemon opposingPokemon) { - opposingPokemon.takeDamage(2); // I have decided that geodude's atack should do 2 damage - } - -} diff --git a/Pokemon/Answers/Jigglypuff.java b/Pokemon/Answers/Jigglypuff.java deleted file mode 100644 index d4be853..0000000 --- a/Pokemon/Answers/Jigglypuff.java +++ /dev/null @@ -1,21 +0,0 @@ -package JavaTraining2024.Pokemon.Answers; - -public class Jigglypuff extends Pokemon { - - // default constructor, setting to 15 health and naming the pokemon "Jigglypuff" - public Jigglypuff() { - super(15, "Jigglypuff"); - } - - // non-default constructor, allowing the user to specify Geodude's health and name - public Jigglypuff(int health, String name) { - super(health, name); - } - - // Attack method for damaging an opposing pokemon - @Override - public void attack(Pokemon opposingPokemon) { - opposingPokemon.takeDamage(3); // I have decided that jigglypuff's atack should do three damage - } - -} diff --git a/Pokemon/Answers/Main.java b/Pokemon/Answers/Main.java deleted file mode 100644 index 03928bc..0000000 --- a/Pokemon/Answers/Main.java +++ /dev/null @@ -1,35 +0,0 @@ -package JavaTraining2024.Pokemon.Answers; - -public class Main { - public static void main(String[] args) { - // Round 1 - Pokemon geodude = new Geodude(); - Pokemon jigglypuff = new Jigglypuff(); - - while (!geodude.getIsFainted() && !jigglypuff.getIsFainted()) { - geodude.attack(jigglypuff); - jigglypuff.attack(geodude); - } - - if (geodude.getIsFainted()) { - System.out.println("The winner is " + jigglypuff.getName()); - } else { - System.out.println("The winner is " + geodude.getName()); - } - - // Round 2 - Pokemon toughGuy = new Geodude(25, "Tough Guy"); - Pokemon littleBuddy = new Jigglypuff(12, "Little Buddy"); - - while (!toughGuy.getIsFainted() && !littleBuddy.getIsFainted()) { - toughGuy.attack(littleBuddy); - littleBuddy.attack(toughGuy); - } - - if (toughGuy.getIsFainted()) { - System.out.println("The winner is " + littleBuddy.getName()); - } else { - System.out.println("The winner is " + toughGuy.getName()); - } - } -} diff --git a/Pokemon/Answers/Pokemon.java b/Pokemon/Answers/Pokemon.java deleted file mode 100644 index 465c743..0000000 --- a/Pokemon/Answers/Pokemon.java +++ /dev/null @@ -1,29 +0,0 @@ -package JavaTraining2024.Pokemon.Answers; - -public abstract class Pokemon { - - private int health; - private String name; - - public Pokemon(int health, String name) { - this.health = health; - this.name = name; - } - - public void takeDamage(int damage) { - health -= damage; - } - - public abstract void attack(Pokemon opposingPokemon); - - public boolean getIsFainted() { - return health <= 0; - } - - public String getName(){ - return name; - } - public int getHealth(){ - return health; - } -} diff --git a/Pokemon/README.md b/Pokemon/README.md deleted file mode 100644 index 63bef29..0000000 --- a/Pokemon/README.md +++ /dev/null @@ -1,77 +0,0 @@ -# Let's Make Pokemon! - -## Rules -1. All classes must be in their own file (Packages) - -2. Mustuse public and private appropriately in all instance variables and methods (Encapsulation!) - -3. Must have comments above each method breifly explaining what it is intended to do - -4. Must have an abstract class “Pokemon” that all individual pokemon inherit from (Abstraction!) - - a. Must have an instance variable for health - - 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 - - d. Must have a defined (regular) method “take damage” that intakes an integer parameter and subtracts that value from health - - e. Must have abstract method “attack” that intakes a parameter of a Pokemon object (Abstraction!) - - f. Must have a getter “getIsFainted” that returns true or false for if the pokemon is dead (health <=0) - - g. Must have a getter "getName" that returns the name of the pokemon - -5. Must have a child class for individual pokemon (i.e. public class Jigglypuff) that extends (inherits from) Pokemon. Must make 1, it'd be better practice to make 2 - - a. Must overload (@override) the "attack" method (method polymorphism!). Call the "take damage" method of that pokemon inside of this method, passing in however much damge the attack is meant to do (subtype polymorphism!). If you want other effect to happen, put them here too! - - b. Must have a default constructor setting the "health" and "name" variables to reasonable default values (i.e. 14, "Jigglypuff") - - c. Must have a non-default constructor that sets "health" and "name" to values given in the parameters (method polymorphism!) - -6. Within a main method, make default objects of your pokemon and have them fight! - - a. In a while loop that continues until one or both pokemon have fainted, have them take turns attacking each other, passing the object of the defender into the attack method of the attacker - - b. Print out the name of your winner! - -7. Repeat 7, but with non-default objects of your pokemon (different health, different names) - - -## When you Finish - -1. Make sure you commit changes to github - -2. Make sure you push your changes (send them to the cloud) - -3. Make a Pull Request on your own github repo, see slides for how. In it, have your description/comment for it include the following: - - a. Explain why Pokemon should be an abstract class - - b. Explain why inheriting from Pokemon is useful - - c. Explain an example of method polymorphism in your code - - d. Explain an example of subtype polymorphism in your code - - e. Explain why you made one of your methods public - - f. Explain why you made one of your instance variables private - -Your explanations should have a bit of detail relating to specific parts of your code, not just citing things I've said or are written in these instructions - -3. Send a link to your github pull request to @Noah over discord. - -If you get it to me before Saturday, I will for sure review it before the end of the weekend. If you get it to me before Tuesday, I will review it before the next weekend - - -## Optional Further Practice -- Include Types (water, fire, grass) with strengths and weaknesses -- Include multiple different attacks on each pokemon -- Include levels that assign health values and change the damage dealt in the attack method -- Make "status conditions" (sleep, paralysis, poison) that have a percent chance to activate after an attack (import java.util.Random;, google how to use it to get random values) -- Give attacks a chance to miss - - diff --git a/README.md b/README.md index 7a145b6..6004465 100644 --- a/README.md +++ b/README.md @@ -1,62 +1,37 @@ -# Java Training Exercises -🖊️ Exercises for our software training! +a. Explain why Pokemon should be an abstract class +Abstract classes cannot be instantiated, so there cannot be a Pokemon object. This makes sense because Pokemon itself is too general since there needs to be a specific type of Pokemon as an object. -## How to Use this Repository -
    -
  1. -
    - Complete activites in your own fork! 🍴 -
    -
    Click "Use this template" to create a fork
    - -
    -
    SELECT "Include all branches"
    - -
    -
    -
  2. - -
  3. -
    - Switch branches to see execersizes pertaining to each class 🌲 -
    - -
    -
    -
  4. - -
  5. -
    - Find exercises in .md files from their respective folders & subtopics 📂 -
    - -
    - -
    -
    -
  6. - -
  7. -
    - When a whole day is complete, create a pull request to merge your branch to the main branch (This is what the leads will be looking for) 📫 -
    - -
    - -
    -
    Change the base repository to YOUR FORK not Team-Optix-3749: Java-Training
    -
    SHOULD NOT look like this
    - -
    SHOULD look like this
    - -
    - -
    - -
    - -
    -
    -
  8. - -
+b. Explain why inheriting from Pokemon is useful +The Pokemon class contains general attributes that specific Pokemon classes can inherit from. This is useful because although different types of Pokemon are different, they all share common attributes that may become repetitive if the same code is rewritten for multiple classes. An example of how this is useful is how the attack method is overidden in the Jigglypuff and Pikachu classes. +``` java +@Override + void attack(Pokemon obj) { + obj.takeDamage(15); + } +``` +c. Explain an example of method polymorphism in your code +The attack method in the Pokemon class is defined with no additional code. The method override for the attack method in the Pikachu class uses the takeDamage method to specify a specific amount of damage dealt thats unique to the Pikachu class. +```java + @Override + void attack(Pokemon obj) { + obj.takeDamage(25); + } +``` +d. Explain an example of subtype polymorphism in your code +j and p are referenced to the Pokemon class but are instances of the Jigglypuff and Pikachu classes. This shows how Jigglypuff and Pikachu are subclasses of Pokemon. +```java +Pokemon j = new Jigglypuff(100, "Jigglypuff"); +Pokemon p = new Pikachu(100, "Pikachu"); +``` +e. Explain why you made one of your methods public +I made getName() public because its a getter method so its purpose is for outside classes to access the value of the private variable name. +```java + public String getName() { + return name; + } +``` +f. Explain why you made one of your instance variables private +I made the health variable private because the health should not be able to be modified from outside classes. It should only be used and changed in controlled ways, such as through methods in the class. +```java + private int health; +``` \ No newline at end of file diff --git a/Week4/Jigglypuff.java b/Week4/Jigglypuff.java new file mode 100644 index 0000000..01e5d8e --- /dev/null +++ b/Week4/Jigglypuff.java @@ -0,0 +1,13 @@ +package Week4; + +public class Jigglypuff extends Pokemon{ + // have jigglypuff attack its opponent + @Override + void attack(Pokemon obj) { + obj.takeDamage(15); + } + //constructor + Jigglypuff (int health, String name) { + super(health, name); + } +} diff --git a/Week4/Main.java b/Week4/Main.java new file mode 100644 index 0000000..90ac133 --- /dev/null +++ b/Week4/Main.java @@ -0,0 +1,21 @@ +package Week4; + +public class Main { + public static void main(String[] args) { + // create jigglypuff and pikachu objects + Pokemon j = new Jigglypuff(100, "Jigglypuff"); + Pokemon p = new Pikachu(100, "Pikachu"); + // have the pokemons attack each other until one collapses + while(!j.getIsFainted() && !p.getIsFainted()) { + j.attack(p); + p.attack(j); + } + // printing the winner + if (j.getIsFainted()) { + System.out.println("Pikachu won!"); + + } else { + System.out.println("Jigglypuff won!"); + } + } +} diff --git a/Week4/Pikachu.java b/Week4/Pikachu.java new file mode 100644 index 0000000..efe34b7 --- /dev/null +++ b/Week4/Pikachu.java @@ -0,0 +1,13 @@ +package Week4; + +public class Pikachu extends Pokemon{ + // have pikachu do damage to its opponent + @Override + void attack(Pokemon obj) { + obj.takeDamage(25); + } + // constructor + Pikachu (int health, String name) { + super(health, name); + } +} diff --git a/Week4/Pokemon.java b/Week4/Pokemon.java new file mode 100644 index 0000000..e9c5642 --- /dev/null +++ b/Week4/Pokemon.java @@ -0,0 +1,30 @@ +package Week4; + +public abstract class Pokemon { + //private instance variables + private int health; + private String name; + // constructor + Pokemon (int health, String name) { + this.health = health; + this.name = name; + } + // subtract health when taken damage + public void takeDamage(int damage) { + health -= damage; + } + // attack method + abstract void attack(Pokemon obj); + // check if pokemon collapsed + public boolean getIsFainted() { + if (health <= 0) { + return true; + } + else { + return false; + } + } + public String getName() { + return name; + } +}