Brian Brian - 9 months ago 69
Java Question

Simple Java Pokemon Fight Simulator

I have written a class to create and battle pokemon but I cannot figure out how to call the battle method in the tester class in order to test the class I wrote.

My assaignment is to write and test a simulation that models a battle between two Pokemon. Each Pokemon has a health value, a strength value, and a speed value. The health, strength, and speed values are passed in to the constructor as arguments. These values must be between 1 and 300 initially and should be nonzero initially. The overall idea for the finished game is that two Pokemon will “battle” one another in the simulation, with the pokemon taking turns attacking. (The one with the highest speed value goes first each round) The attacking Pokemon’s strength will be subtracted from the “attackee’s” health.

public class Pokemon{
private int health;
private int strength;
private int speed;

* Constructs the pokemon
* @Require:
* health is an integer greater than or equal to 1 but less than or equal to 300
* strength is and integer greater than or equal to 1 but less than or equal to 300
* speed is an integer greater than or equal to 1 but less than or equal to 300
public Pokemon(int health, int strength, int speed){
assert health >= 1;
assert health <= 300;
assert strength >= 1;
assert strength <= 300;
assert speed >= 1;
assert speed <= 300; = health;
this.strength = strength;
this.speed = speed;

public void battle(Pokemon pokemon1, Pokemon pokemon2){
System.out.println(pokemon1+" begins the fight against "+pokemon2); = - pokemon1.strength;

System.out.println(pokemon1 +" does "+ pokemon1.strength +" damage to "+
pokemon2 +" and "+ pokemon2 +" has "+ +" left."); = - pokemon2.strength;

System.out.println(pokemon2 +" does "+ pokemon2.strength +" damage to "+
pokemon1 +" and "+ pokemon1 +" has "+ +" left.");

}while( >= 1 || >= 1);
if( < 1)
System.out.println(pokemon1 +" has lost the fight");
System.out.println(pokemon2 +" has lost the fight");

Pokemon Tester

public class PokemonTester{
private Pokemon charizard;
private Pokemon blastoise;
private Pokemon venusaur;

public PokemonTester(){
charizard = new Pokemon(100,50,50);
blastoise = new Pokemon(150,25,150);
venusaur = new Pokemon(300,10,100);

public static void main(String[] args){
Pokemon.battle(charizard, blastoise); //will not compile

I do realize I have not implemented the speed aspect in taking turns yet as I'm trying to just make it work.

Answer Source

Add static to battle function, just like in main.

Also, you cannot use charizard and blastoise in main. Non-static variables cannot be used in static functions. You need to create local variables in `main

public static void main(String[] args){
    Pokemon charizard = new Pokemon(100,50,50);
    Pokemon blastoise = new Pokemon(150,25,150);
    Pokemon.battle(charizard, blastoise);

You can also create new PokemonTester and use it's variables:

public static void main(String[] args){
    PokemonTester tester=new PokemonTester();
    Pokemon.battle(tester.charizard, tester.blastoise);

You can learn more about static members here