Starting from:

$20

CS1331 Homework 11 - Pokemon Solved

Problem Description
Struck by sudden nostalgia, you quit your job and start playing games from your childhood, like Pokemon! However, life seems determined to make you a Software Engineer. One day, as you sit huddled underneath a blanket snacking on saltine crackers, your DS suddenly glows bright blue. You shield your eyes, and when you open them you find yourself in the world of Pokemon. You immediately walk to the nearest Pokemon Center that you know is 108.15 yards to your right. Nurse Joy is very kind to you, but after watching her treat the injured Pokemon, you realize she is overworked and extremely disorganized. This calls for some programming!

Solution Description
For this assignment, create the following files:

•      Pokemon.java

•      Pikachu.java

•      Charmander.java

•      PokemonCenter.java

Pokemon Class:

Implements the Comparable interface. A pokemon is “greater” when it has more health; a pokemon is “lesser” when it has less health. Should be abstract.

Instance Variables:

•      medicalHistory, an ArrayList of Integers that tracks changes to the pokemon’s health. For example, if a pokemon loses 5 health, -5 should be appended to medicalHistory. As another example, if a pokemon gains 7 health, 7 should be appended to medicalHistory. This instance variable should have private access.

•      trainerName, a String which is the pokemon’s trainer’s name. It should have private access.

•      health, an int that is the pokemon’s health. It should have private access.

•      maxHealth, an int that is the pokemon’s maximum health. It should have private access.

Constructors:

•      Pokemon(String trainerName, int health, int maxHealth) – Sets trainerName, health and maxHealth properly.

– Initializes an empty ArrayList of Integers for medicalHistory.

•      Pokemon(Pokemon p) copy-constructor, provides a deep copy of the object.

Methods:

•      void heal(int amount) increase health by specified amount. However, health cannot be greater than maxHealth. In cases where health plus amount is greater than maxHealth, set health to maxHealth. Because health has changed, make sure to update medicalHistory. Only update with actual (not expected) change to health. For example, if a pokemon has a health of 6, maxHealth of 7, and is healed by amount 5, only append 1 to medicalHistory. As another example, if a pokemon has health 8, maxHealth 8, and is healed by amount 2, append 0 to medicalHistory.

•      void damage(int amount) decrease health by the specified amount. However, health cannot go below 0. In cases where decreasing health by the specified amount makes health negative, set health to 0 and update medicalHistory with only the amount of health lost; for example, if a pokemon has 3 health and takes 4 damage, you would append -3 to its medicalHistory.

•      int compareTo(Pokemon other) A pokemon is “greater” when it has more health; a pokemon is “lesser” when it has less health.

•      boolean equals(Object other) Pokemon are considered equal if they have the same trainerName, health, maxHealth, and medicalHistory.

•      int hashCode() Write a proper hashCode for Pokemon that contains the proper fields. Only use the maxHealth field to compute the hash code.

•      Getters for trainerName, health, and maxHealth.

•      Setter for trainerName.

Pikachu Class:

Is a subclass of Pokemon Instance Variables:

•      int friendshipLevel the level of friendship the pikachu has with its trainer. It should have private access.

Constructors:

•      Pikachu(String trainerName, int health, int maxHealth, int friendshipLevel) sets trainerName, health, maxHealth, and friendshipLevel properly.

•      Pikachu(Pikachu p) copy-constructor, provides a deep copy of the object.

Methods:

•      void heal(int amount) increase health by twice the amount because pikachus heal quickly. However, health cannot be greater than maxHealth. In cases where health plus amount is greater than maxHealth, set health to maxHealth. Because health has changed, make sure to update medicalHistory. Only update with actual (not expected) change to health. Reuse code if possible!

•      boolean equals(Object other) Pikachus are considered equal if they have the same trainer, health, maxHealth, medicalHistory, and friendshipLevel. Reuse code if possible!

•      int hashCode() Write a proper hashCode for Pikachu that contains the proper fields. Use the fields maxHealth and friendshipLevel for computing the hash code.

Charmander Class:

Is a subclass of Pokemon Instance Variables:

•      int flameLevel represents how much flame the charmander has. It should have private access.

Constructors:

•      Charmander(String trainerName, int health, int maxHealth, int flameLevel) sets trainerName, health, maxHealth, and flameLevel properly.

•      Charmander(Charmander c) copy-constructor, provides a deep copy of the object.

Methods:

•      boolean equals(Object other) Charmanders are considered equal if they have the same trainer, health, maxHealth, medicalHistory, and flameLevel. Reuse code if possible!

•      int hashCode() Write a proper hashCode for Charmander that contains the proper fields. Use the fields maxHealth and flameLevel for computing the hash code.

•      void damage(int amount, boolean isWaterDamage) decrease health by amount if it is not water damage. if it is water damage, decrease health by twice the amount. Health cannot go below 0. In cases where health falls below 0, set health to 0 and update medicalHistory with only the amount of health lost; for example, if a pokemon has 3 health and takes 4 damage, you would append -3 to its medicalHistory. Make sure to update medicalHistory accordingly and reuse code wherever possible!

PokemonCenter Class:

Instance Variables

•      injured, an ArrayList of pokemon that holds the injured pokemon in line to be treated. The list should be ordered by how recently a pokemon was seen. Most recently seen pokemon are always at the end of the list. It should have private access.

•      nurses, an int that is the number of nurses at the Pokemon Center. It should have private access.

Constructors:

•      PokemonCenter(ArrayList<Pokemon injured, int nurses) sets the two instance variables to the provided values.

•      PokemonCenter() sets the two instance variables to their default values; empty ArrayList of injured pokemon and one nurse.

Methods:

•      boolean add(Pokemon pokemon) adds a pokemon to the list of injured pokemon, regardless of whether or not it is injured. Always add to the end of the injured list. Just make sure that a pokemon isn’t already on the list! This method returns true if a pokemon was added, else false.

•      void heal(int amount). For each nurse in the center, remove a pokemon from the list and increase its health by the specified amount. If there are more nurses than pokemon in the list, simply heal each pokemon once. If (after healing) a pokemon is not fully healed, add it to the list of injured Pokemon. Remember, most recently seen pokemon go at the end!

•      void healMostInjured(int amount). Sort the list, putting the most injured pokemon in the front and the least injured pokemon in the back. (HINT: Check out the Collections class) Then, for each nurse in the center, remove a pokemon from the front of the list and increase its health by the specified amount. If there are more nurses than pokemon in the list, simply heal each pokemon once. If the pokemon isn’t fully healed, add it back to the list. Make sure that after this method finishes executing, injured is still ordered by how recently a pokemon has been seen. (most recently seen pokemon go at the end!)

•      String toString() which returns “This Pokemon Center has [number of nurses] nurses and [number of pokemon in injured list] pokemon waiting to be seen.”

Testing your code:

If you want to test out your own code before submitting we recommend creating a separate java file and implementing a main method there. You can then create new animals and test your methods. Below is some sample testing code. These tests are not comprehensive!

Pikachu pikachu = new Pikachu("Ryan", 20, 25, 10);

Pikachu pikachu2 = new Pikachu(pikachu);

Charmander charmander = new Charmander("Emma", 20, 30, 10);

Charmander charmander2 = new Charmander("Emma", 10, 30, 10);

System.out.println(pikachu.equals(pikachu2)); // True pikachu.heal(10);

System.out.println(pikachu.equals(pikachu2)); // False

PokemonCenter pokemonCenter = new PokemonCenter();

pokemonCenter.add(charmander2); pokemonCenter.add(charmander); pokemonCenter.healMostInjured(7); //charmander2 health = 17 now

Feel free to create your own tests in the main method! Try to write 

More products