Question: JUNIT TESTER FOR SOURCE CODE import java.util.Observable; public class Pokemon extends Observable{ private int myHealth; /** @invariant myHealth >=0 */ private int myAttackPower; /** @invariant

JUNIT TESTER FOR SOURCE CODE

import java.util.Observable;

public class Pokemon extends Observable{

private int myHealth; /** @invariant myHealth >=0 */ private int myAttackPower; /** @invariant myAttackPower >= 0 */ private String myName; /** @invariant myName != "" */ public Pokemon(String name, int initialHealth, int initialAttackPower) { myName = name; myAttackPower = initialAttackPower; myHealth = initialHealth; }

/** @return An int corresponding to the Pokemon's current health @custom.ensure getHealth() >= 0 */ public int getHealth() { return myHealth; }

/** @return An int corresponding to the Pokemon's current attack @custom.ensure getAttackPower() >= 0 */ public int getAttackPower() { return myAttackPower; }

/** @return A string corresponding to the Pokemon's name @custom.ensure getName() != "" */ public String getName() { return myName; }

/** @return Returns true of the Pokemon's health is zero, otherwise false */ public boolean isDead() { if (myHealth == 0) return true; return false; }

public void attack(Pokemon other) { other.takeDamage(this.myAttackPower, this); }

public void takeDamage(int amountOfDamage, Pokemon attacker) { int amtOfDamageActuallyDealt; if (amountOfDamage > myHealth) { amtOfDamageActuallyDealt = myHealth; myHealth = 0; } else { myHealth -= amountOfDamage; amtOfDamageActuallyDealt = amountOfDamage; } setChanged(); notifyObservers(new WhatHappened(this,attacker,"Body Slam",amtOfDamageActuallyDealt)); }

/** @return Returns a string corresponding the the state of the Pokemon */ public String toString() { String returnval = ""; returnval += "Pokemon " + myName + ": \thealth: " + myHealth + " \tattack: " + myAttackPower; return returnval; }

} // end class Pokemon

import java.util.Observer; import java.util.Observable;

public class PokemonTrainer implements Observer {

private String myName; /** @invariant myName != "" */ /** @param name The name of the PokemonTrainer @since Monday, Sep 9, 2018

@custom.require name != "" */ public PokemonTrainer(String name) { myName = name; }

/** @return A string corresponding to the PokemonTrainer's name @custom.ensure getName() != "" */ public String getName() { return myName; }

@Override /** {@inheritdoc} */ public void update(Observable obs, Object o) { String whatHappened = ""; whatHappened += this + " is notified that " + o; System.out.println(whatHappened); }

/** @return Returns a string corresponding the the state of the PokemonTrainer */ public String toString() { String returnval = ""; returnval += "PokemonTrainer " + myName; return returnval; }

} // end class PokemonTrainer

public class WhatHappened {

private Pokemon attacker; private Pokemon victim; private String typeOfAttack; private int amountOfDamageDone; /** @param attacker The pokemon that attacked @param victim The pokemon that was attacked @param typeOfAttack A descriptive String that states the type of attack made @param amtOfDamage The amount of damage dealt in the attack */ public WhatHappened(Pokemon attacker, Pokemon victim, String typeOfAttack, int amtOfDamage) { this.attacker = attacker; this.victim = victim; this.typeOfAttack = typeOfAttack; this.amountOfDamageDone = amtOfDamage; }

/** @return The descriptive String stating the type of attack that was made */ public String getTypeOfAttack() { return this.typeOfAttack; }

/** @return a reference to the Pokemon that made the attack */ public Pokemon getAttacker() { return this.attacker; }

/** @return a reference to the Pokemon that was attacked */ public Pokemon getVictim() { return this.victim; }

/** @return the amount of damage that was dealt in the attack */ public int getAmtOfDamage() { return this.amountOfDamageDone; }

/** @return a String stating exactly what happened in this bout of conflict: [Attacker] attacked Pokemon [victim] using [type of attack] for [amount] damage. */ public String toString() { String returnval = ""; returnval += "Pokemon " + attacker.getName() + " attacked Pokemon " + victim.getName() + " using " + typeOfAttack + " for " + amountOfDamageDone + " damage."; if (victim.isDead()) returnval += "Pokemon " + victim.getName() + " is dead."; return returnval; }

} // end class WhatHappened

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!