Question
CompSci 251: Intermediate Computer Programming Spring 2017 JAVA Assignment 5 Topics Covered: Inheritance, Arrays of classes, Abstract, static fields. In this assignment, you will build
CompSci 251: Intermediate Computer Programming Spring 2017 JAVA
Assignment 5
Topics Covered: Inheritance, Arrays of classes, Abstract, static fields.
In this assignment, you will build a class inheritance structure for a small portion of the game of Pokemon GO.
Implementing the below class hierarchy. Your concrete classes must call the superclass constructor with the proper parameters. The # symbol in the UML diagram below means protected and underline means static.
> Pokemon |
# name: String # health: int # power: int |
+Pokemon(name: String, health: int , power: int) +getName(): String +getHealth(): int +setHealth(health: int) +getPower(): int +setPower(power: int) #hurt(damage: int): void +attack(target: Pokemon) |
Pikachu |
- final thunderBolt =5 : int |
+Pikachu(health: int, power: int) + toString(): String + equals(other: Object): boolean +attack(target: Pokemon): void |
Mew |
- final psychicBlast =4: int |
+Mew(health: int, power: int) + toString(): String + equals(other: Object): boolean +attack(target: Pokemon): void |
1
The hurt method is implemented in the superclass as it does not change from one type of Pokemon to another type (because it is common behaviour). The hurt method takes an int argument that is going to be subtracted from the health only if the argument passed is positive and less than or equal to the current value of the health. If the attack is greater than the current value of health, health is set to zero. Health can never be negative.
The attack method is abstract and must be overridden in each subclass as the attack is dependent on the type of Pokemon. The attack method should call the hurt method of the target Pokemon to decrease the health of the target Pokemon. The argument that is going to be passed will be either thunderBolt or psychicBlast(both of type int and they represent the amount that will be taken from the target pokemons health for a single hurt). The amount of damage the attacking pokemons special attack does will be subtracted from their power when they attack. If the attacking Pokemons power is not zero, but they do not have enough power for a normal attack, their remaining power is used for the attack then their power is depleted. If they do not have enough power to attack, nothing happens.
Examples:
Mew has power 7, attack of 4. Pikachu has health of 15. Mew is attacking Pikachu. After first attack: Mew power is 3, Pikachu health is 11. After second attack: Mew power is 0, Pikachu health is 8. After third attack: nothing happens because Mew power is 0.
The setter and mutator for power will set the instance variable only if the argument passed in is greater than or equal to zero(note that the name is a read only, no setters should be provided). You also will be responsible to add toString and equals methods to each concrete (non abstract) subclasses. The equals method takes an instance of Object which can be anything (even null). We are only creating two types of Pokemon, but a Pokemon is only the same of another Pokemon if ALL of their instance variables are the same.
Driver Your main method should create a 5-element array of Pokemon. You should prompt the user five times for a name of the pokemon, health, and power. Each time, create a concrete instance of a Pokemon by calling the makePokemon method with appropriate arguments. Add this Pokemon to the next index in the array only if the Pokemon was successfully created and if this Pokemon does not occur in the array already. There are two methods we require you to implement to help with creating/adding pokemon. The method signatures are below.
static Pokemon makePokemon(name: String, health: int , power: int ) { // ...
}
static boolean contains(Pokemon pokemon, Pokemon[] pokemons) { // ...
}
The makePokemon method should return the proper concrete subclass (determined by the value of name), or return null if the value of name is not Pikachu or Mew (case-sensitive). The way the constructors are set up, this means the name of every Pokemon created will either be Pikachu or Mew. Keep that in mind when calling the super class constructor.
The contains method should check if the passed Pokemon object exists in the array or not. Duplicate 2
pokemon are not allowed. If a non null, unique type of Pikachu or Mew is created, it means it can be added to the array.
You will be provided with two helper methods, play and print. The play method will prompt the user three times to enter two integer numbers, entered on the same line and separated by a space, that represents the index of the pokemon you want to have attack. The second number is the index of the Pokemon you want to be attacked. The print method will print the content of the array(rely of the toString).
When your array is filled with Pokemon, call print to show all of your Pokemon. After print, call play. When play is complete , call print one more time to see how their variables have changed.
1 Sample Output
The comments below are not part of the output, I just put them for your understanding
Enter name, health, power for Pokemon#0: Rattata 100 500// NOTE: was illegal Enter name, health, power for Pokemon#0: mew 100 300 Enter name, health, power for Pokemon#1: pikachu 50 10 Enter name, health, power for Pokemon#2: mew 100 300// NOTE: was duplicate Enter name, health, power for Pokemon#2: mew 100 900
Enter name, health, power for Pokemon#3: pikachu 80 60 Enter name, health, power for Pokemon#4: pIKACHU 80 60// NOTE: was duplicate Enter name, health, power for Pokemon#4: pIKACHU 90 150
Pokemons before playing 0: Mew [name=Mew, health=100, power=300] 1: Pikachu [name=Pikachu, health=50, power=10] 2: Mew [name=Mew, health=100, power=900] 3: Pikachu [name=Pikachu, health=80, power=60] 4: Pikachu [name=Pikachu, health=90, power=150]
# 0 Please enter the pokemons you want to play 0 to 4: -1 2// NOTE: was illegal # 0 Please enter the pokemons you want to play 0 to 4: 10 12// NOTE: was illegal # 0 Please enter the pokemons you want to play 0 to 4: 1 2 # 1 Please enter the pokemons you want to play 0 to 4: 2 3 # 2 Please enter the pokemons you want to play 0 to 4: 3 4
Pokemons After playing 0: Mew [name=Mew, health=100, power=300] 1: Pikachu [name=Pikachu, health=50, power=5] 2: Mew [name=Mew, health=95, power=896] 3: Pikachu [name=Pikachu, health=76, power=55] 4: Pikachu [name=Pikachu, health=85, power=150]
UML:
Template:
abstract Pokemon. name: String health: int power: int +Pokemon name: String, health: int power: int) getName(): String +get Health(): int setHealth (health: int) get Power(): int set Power power: int #hurt damage: int): void. attack target: Pokemon Pikachu Mew final thunder Bolt 5: int final psychicBlast 4: int Pikachu (health: int, power int Mew (health: int, power: int) toString(): String toString String equals er Object): boolean equals (other: Object): boolean -t attack target: Pokemon void attack target: Pokemon): void abstract Pokemon. name: String health: int power: int +Pokemon name: String, health: int power: int) getName(): String +get Health(): int setHealth (health: int) get Power(): int set Power power: int #hurt damage: int): void. attack target: Pokemon Pikachu Mew final thunder Bolt 5: int final psychicBlast 4: int Pikachu (health: int, power int Mew (health: int, power: int) toString(): String toString String equals er Object): boolean equals (other: Object): boolean -t attack target: Pokemon void attack target: Pokemon): voidStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started