Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Program Specification You will build a class inheritance structure for a small portion of the game Pokemon. You must implement the class hierarchy below.

Java

Program Specification

You will build a class inheritance structure for a small portion of the game Pokemon. You must implement the class hierarchy below. Your concrete classes must call the superclass constructor with the proper parameters. The # symbol in the UML diagram means it is protected, italicized means static, and italicized&bold means abstract. The two italicized methods must be implemented in the subclasses. + means public, - means private.

Pokemon

# name: String
# health: int
# power: int
# level: int
+ Pokemon(name: String, health: int, power: int, level: int)
+ getName(): String
+ getHealth(): Int
+ getPower(): int
+ getPower(power: int)
+ getLevel(): int
+setLevel(): int
+setLevel(level: int)
+ isDefeated(): boolean
# hurt(damage: int)
+specialAttack(target: Pokemon)
+physicalAttack(target: Pokemon)

Pikachu
- final thunderBolt = 6: int
- final swipe = 3: int
+ Pikachu (health: int, power: int, level: int)
+ toString(): String
+ equals(other: Object): boolean
+specialAttack(target: Pokemon)
+physicalAttack(target: Pokemon)

Charmander
- final fireBall = 5: int
- final bite = 4: int
+ Charmander (health: int, powerL int, level: int)
+ toString(): String
+ equals(other: Object): boolean
+specialAttack(target: Pokemon)
+physicalAttack(target: Pokemon)

Requirements

hurt(damage: int)

o The hurt method is implemented in the superclass because its implementation will not change from one type of Pokemon to the next. The hurt method takes an int argument (the damage to be dealt) that is to be subtracted from the health only if the argument passed in is positive and the pokemon isDefeated() returns false. If the value is negative or isDefeated returns true nothing happens, because if we subtracted this value from the health it would actually heal them or you are attacking something that is dead! If the attack is greater than the current health value, health is set to zero. Health should never be negative.

specialAttack(target: Pokemon)

o This 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 fireBall. Both are of type int that 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 after they attack. If the attacking Pokemons power is not zero, but they do not have enough power for a normal special attack, their remaining power is used for the attack and their power is depleted. If their power is zero and specialAttack is called, you must call physicalAttack instead. This check can be done in specialAttack method. If either Pokemon isDefeated, meaning health is 0, nothing is to be done.

Example:

Charmander has power 7, special attack is 5.

Pikachu has health 15.

Charmander special attacks Pikachu, Pikachu health is 10, and Charmander power is 2.

Charmander special attacks Pikachu again, but its power is only 2 so it attacks for 2.

Pikachu health is 8, and Charmander power is 0.

Charmander special attacks Pikachu again, but its power is 0.

So Charmander does a physical attack instead, whose damage is 4.

Pikachu health is 4 now.

physicalAttack(target: Pokemon)

o This method is similar to specialAttack except that it does not need to check its power level for how much damage to deal, it always deals the same amount of damage to the target based on which Pokemon is attacking. If either Pokemon isDefeated, nothing happens.

Setters

o All setters/mutators for power, health, and level will set the instance variables if the argument passed in is greater than or equal to 0. Note that the name is read only, meaning it can not be changed so no setters for this.

toString()

o Prints out the contents of the Pokemon.

equals(Object other)

o The equals method takes an instance of Object which can be anything. We only have two types of Pokemon, but a Pokemon is only the same of another Pokemon if ALL of their instance variables are the same.

Driver

o Your main method should create a 4-element array of Pokemon. You should prompt the user four 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 the 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 methods are

static Pokemon makePokemon(name: String, health: int, power: int, level: int) { . . . }

static boolean contains (Pokemon pokemon, Pokemon[ ] pokemons) { . . . }

The makePokemon method should return the proper concrete subclass, determined by the name, or return null if the value of name is not Pikachu or Charmander (is case sensitive). This means every name of every Pokemon created will either be Pikachu or Charmander. Keep this in mind when calling the super constructors.

The contains method should check if the passed Pokemon object exists in the array or not. Duplicates are not allowed. If a non-null, unique type of Pikachu or Charmander 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. The first number is the Pokemon who will attack, the second number is the Pokemon to get attacked. The print method will print the content of the array.

Sample Output

I have added the comments to explain a few things, they will not show in your runs. It is ok if your program crashes while taking invalid user input.

Enter name, health, power, and level for Pokemon#0: evee 200 10 5 //illegal

Enter name, health, power, and level for Pokemon#0: pikachu 100 10 10 //good

Enter name, health, power, and level for Pokemon#1: PIKAchu 300 20 20 //good

Enter name, health, power, and level for Pokemon#2: PiKaChu 300 20 20 //duplicate

Enter name, health, power, and level for Pokemon#2: charmander 150 12 12 //good

Enter name, health, power, and level for Pokemon#3: pikachu 100 10 10 //duplicate

Enter name, health, power, and level for Pokemon#3: p 3 3 3 //illegal

Enter name, health, power, and level for Pokemon#3: charmander 200 20 15 //good

Pokemons before playing 0: Pikachu [name=Pikachu, health=100, power=10, level=10]

1: Pikachu [name=Pikachu, health=300, power=20, level=20]

2: Charmander [name=Charmander, health=150, power=12, level=12]

3: Charmander [name=Charmander, health=200, power=20, level=15]

# 0 Please enter the pokemons you want to play 0 to 3: -1 2 //bad indices

# 0 Please enter the pokemons you want to play 0 to 3: 10 45 //bad indices

# 0 Please enter the pokemons you want to play 0 to 3: 1 2

# 1 Please enter the pokemons you want to play 0 to 3: 0 3

# 2 Please enter the pokemons you want to play 0 to 3: 0 1

Pokemons After playing

0: Pikachu [name=Pikachu, health=93, power=0, level=10]

1: Pikachu [name=Pikachu, health=292, power=14, level=20]

2: Charmander [name=Charmander, health=144, power=12, level=12]

3: Charmander [name=Charmander, health=194, power=20, level=15]

----------------------------------------------------------------------------------------

Code Provided

-----------------------------------------------------------------

Driver Class

import java.util.Scanner;

public class Driver {

public static void main(String[] args) {

Pokemon[] arrOfPokemons = new Pokemon[4];

Scanner stdIn = new Scanner(System.in);

Pokemon pokemon = null;

for (int i = 0; i < arrOfPokemons.length; i++) {

//TODO

//Read input from user.

//Create a pokemon and add it to the array if it is not a duplicate.

}

System.out.println();

System.out.println("Pokemons before playing");

print(arrOfPokemons);

System.out.println();

for(int i=0; i<3;i++){

play(arrOfPokemons, stdIn, i);

}

System.out.println();

System.out.println("Pokemons After playing");

print(arrOfPokemons);

stdIn.close();

}

private static void print(Pokemon[] arrOfPokemons){

//Print the pokemons in the arrOfPokemons array

for (int index = 0; index < arrOfPokemons.length; index++) {

System.out.printf("%d: %s ", index, arrOfPokemons[index].toString());

}

}

private static void play(Pokemon[] arrOfPokemons, Scanner stdIn, int numberOfPlay) {

int firstPokemon=0;

int secondPokemon=0;

do{

System.out.printf("# %d Please enter the pokemons you want to play 0 to 3: ",numberOfPlay);

firstPokemon = stdIn.nextInt();

secondPokemon = stdIn.nextInt();

}while(firstPokemon<0 ||firstpokemon>4 ||secondPokemon<0 ||secondpokemon>4 );

arrOfPokemons[firstPokemon].specialAttack(arrOfPokemons[secondPokemon]);

//think of this part as a counterattack

arrOfPokemons[secondPokemon].physicalAttack(arrOfPokemons[firstPokemon]);

}

/**

* Creates a pokemon based on name passed to method.

*

* @param name

* @param health

* @param power

* @param level

* @return Pokemon (Charmander or Pikachu)

*/

static Pokemon makePokemon(String name, int health, int power, int level) {

//TODO

}

/**

* Tells if array contains a pokemon already.

*

* @param pok

* @param arrOfPokemons

* @return boolean

*/

protected static boolean contains(Pokemon pok, Pokemon[] arrOfPokemons) {

//TODO

}

}

------------------------------------------------

Pokemon Class

public class Pokemon

{

//TODO

}

-------------------------------------------

Testing Class

import static org.junit.Assert.*;

import org.junit.*;

public class testing {

Pokemon a;

Pokemon b;

Pokemon c;

Pokemon d;

Pokemon e;

Pokemon f;

Pokemon g;

Pokemon h;

Pokemon i;

Pokemon j;

Pokemon k;

@Before

public void setUp() {

a = new Pikachu(-10, -10, -5);

b = new Charmander(-2, -5, -3);

c = new Pikachu(10, 50, 2);

d = new Pikachu(3, 5, 3);

e = new Charmander(20, 4, 6);

f = new Charmander(5, 0, 1);

g = new Charmander(5, 0, 1);

h = new Pikachu(1, 10, 10);

i = new Charmander(10, 20, 5);

j = new Pikachu(100, 8, 10);

k = new Charmander(100, 7, 5);

}

@After

public void tearDown() {

a = null;

b = null;

c = null;

d = null;

e = null;

f = null;

g = null;

h = null;

i = null;

j = null;

k = null;

}

@Test

public void TestConstructorsForPikachu() {

assertEquals(true, a instanceof Pikachu);

assertEquals(true, a instanceof Pokemon);

assertEquals(false, a instanceof Charmander);

assertEquals(Pikachu.class, a.getClass());

}

@Test

public void TestConstructorsForCharmander() {

assertEquals(true, b instanceof Charmander);

assertEquals(true, b instanceof Pokemon);

assertEquals(false, b instanceof Pikachu);

assertEquals(Charmander.class, b.getClass());

}

@Test

public void TestInitialFields1() {

assertEquals(0, a.getHealth());

assertEquals(0, a.getLevel());

assertEquals("Pikachu", a.getName());

assertEquals(0, a.getPower());

}

@Test

public void TestInitialFields2() {

assertEquals(0, b.getHealth());

assertEquals(0, b.getPower());

assertEquals(0, b.getLevel());

assertEquals("Charmander", b.getName());

}

@Test

public void TestInitialFields3() {

assertEquals(10, c.getHealth());

assertEquals(50, c.getPower());

assertEquals(2, c.getLevel());

assertEquals("Pikachu", c.getName());

}

@Test

public void TestInitialFields4() {

assertEquals(3, d.getHealth());

assertEquals(5, d.getPower());

assertEquals(3, d.getLevel());

assertEquals("Pikachu", d.getName());

;

}

@Test

public void TestSetters1() {

e.setPower(-2);

assertEquals(4, e.getPower());

e.setLevel(-10);

assertEquals(6, e.getLevel());

e.setHealth(-4);

assertEquals(20,e.getHealth());

d.specialAttack(e);

assertEquals(15, e.getHealth());

assertEquals(0, d.getPower());// d lost power since he attacked e

assertEquals("Charmander", e.getName());

;

}

@Test

public void TestSetters2() {

d.setPower(-1);

assertEquals(5, d.getPower());

d.setLevel(-6);

assertEquals(3, d.getLevel());

d.setHealth(-10);

assertEquals(3,d.getHealth());

f.setHealth(5);

assertEquals(5,f.getHealth()); //Here I had a typo

f.specialAttack(d);

assertEquals(0, d.getHealth());

assertEquals(0, f.getPower());// f lost power since f attacked d

assertEquals("Pikachu", d.getName());

}

@Test

public void TestIsDefeated() {

//h = new Pikachu(1, 10, 10);

//i = new Charmander(10, 20, 5);

assertFalse(h.isDefeated());

i.specialAttack(h);

assertTrue(h.isDefeated());

h.specialAttack(i);

assertEquals(10, i.getHealth());//h is dead, so it can not attack and should not damage

assertEquals(10, h.getPower());//h is dead, so h power should be the same

h.physicalAttack(i);

assertEquals(10, i.getHealth());//h can not attack

i.specialAttack(h);

assertEquals(15, i.getPower());//i can not attack something that is dead, so power should remain

}

/* j = new Pikachu(100, 8, 10);

k = new Charmander(100, 7, 5);*/

@Test

public void TestAttacks() {

j.specialAttack(k);

assertEquals(94, k.getHealth());

j.specialAttack(k);

assertEquals(92, k.getHealth());

j.specialAttack(k);

assertEquals(89, k.getHealth());

j.physicalAttack(k);

assertEquals(86, k.getHealth());

k.specialAttack(j);

assertEquals(95, j.getHealth());

k.specialAttack(j);

assertEquals(93, j.getHealth());

k.specialAttack(j);

assertEquals(89, j.getHealth());

k.physicalAttack(j);

assertEquals(85, j.getHealth());

assertEquals(0, j.getPower());

assertEquals(0, k.getPower());

}

@Test

public void TestEquals() {

assertEquals(true, e.equals(e));

assertEquals(false, e.equals(f));

assertEquals(false, e.equals(null));

assertEquals(false, c.equals(f));

assertEquals(false, e.equals(new Integer(1)));// I am trying to pass an

// object that is not of

// type BossRoadTrip

assertEquals(true, f.equals(g));

}

@Test

public void TestMakePokemon() {

Pokemon p1 = Driver.makePokemon("Pikachu", 10, 10, 2);

Pokemon p2 = Driver.makePokemon("Charmander", 5, 7, 3);

Pokemon p3 = Driver.makePokemon("Whatever", 5, 7, 3);

assertEquals(Pikachu.class, p1.getClass());

assertEquals(Charmander.class, p2.getClass());

assertEquals(true, p3==null);

}

@Test

public void Testexist() {

Pokemon[] pokemons = { c, d, e };

boolean arrayContainsC = Driver.contains(c, pokemons);

boolean arrayContainsD = Driver.contains(d, pokemons);

boolean arrayContainsE = Driver.contains(e, pokemons);

boolean arrayContainsF = Driver.contains(f, pokemons);

assertTrue(arrayContainsC);// check if the Driver.contains(c, pokemons)

// returned true since c is in the array

assertTrue(arrayContainsD);// check if the Driver.contains(d, pokemons)

// returned true since d is in the array

assertTrue(arrayContainsE);// check if the Driver.contains(e, pokemons)

// returned true since e is in the array

assertFalse(arrayContainsF);// check if the Driver.contains(f, pokemons)

// returned false since f is not in the

// array

}

@Test

public void TestTostring() {

String cPokemon = c.toString();

String fPokemon = f.toString();

int cName = cPokemon.indexOf("Pikachu");

int cHealth = cPokemon.indexOf("10");

int cPower = cPokemon.indexOf("50");

int cLevel = cPokemon.indexOf("50");

int fName = fPokemon.indexOf("Charmander");

int fHealth = fPokemon.indexOf("5");

int fPower = fPokemon.indexOf("0");

int fLevel = fPokemon.indexOf("1");

assertEquals(true, cName != -1);

assertEquals(true, cHealth != -1);

assertEquals(true, cPower != -1);

assertEquals(true, cLevel != -1);

assertEquals(true, fName != -1);

assertEquals(true, fHealth != -1);

assertEquals(true, fPower != -1);

assertEquals(true, fLevel != -1);

}

}

-----------------------------------------------------------------

Pikachu Class

public class Pikachu extends Pokemon

{

//TODO

}

--------------------------------------------------

Charmander Class

public class Charmander extends Pokemon

{

//TODO

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database Upgrade Migration And Transformation Tips And Techniques

Authors: Edward Whalen ,Jim Czuprynski

1st Edition

0071846050, 978-0071846059

More Books

Students also viewed these Databases questions

Question

How flying airoplane?

Answered: 1 week ago