Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please in the below code fix the spots that say fix. If you could keep the other

ID: 3749372 • Letter: P

Question

Please in the below code fix the spots that say fix. If you could keep the other code the same I would appreciate it because it relies on other code. Thanks!!

__________________________________________________________-

import java.util.Random;
import java.util.Scanner;

public class PokemonField {
  
public static void main(String[] args) {

Player player1 = new Player("dragonslayer");
Player player2 = new Player("voldemort");
Random random = new Random();
  
initialize(player1, player2);
player1.printStat();
player2.printStat();
  
play(random, player1, player2);
  
System.out.println("Game over");
  
player1.printStat();
player2.printStat();
}
  
public static boolean fight(Random random, Pokemon p1, Pokemon p2){
double prob = (double)p1.getAttackPoints() / (p1.getAttackPoints() + p2.getAttackPoints());
while(p1.getHitPoints() > 0 && p2.getHitPoints() > 0) {
if(random.nextDouble() < prob) {
p2.setHitPoints(Math.max(p2.getHitPoints()- p1.getAttackPoints(), 0));
}
else{
p1.setHitPoints(Math.max(p1.getHitPoints() - p2.getAttackPoints(), 0));
}
}
  
if(p1.getHitPoints() > 0){
System.out.println(p1.getName() + " won!");
return true;
}
else {
System.out.println(p2.getName() + " won!");
return false;
}
}
  
public static void initialize(Player player1, Player player2) {
String[] pokemonNames1 = {"Butterfree", "Ivysaur", "Pikachu", "Squirtle"};
int[] pokemonMaxHps1 = {10, 15, 20, 30};
int[] pokemonAps1 = {1, 2, 2, 3};
String[] pokemonNames2= {"Jigglypuff","Oddish","Gloom","Psyduck","Machop"};
int[] pokemonMaxHps2 = {12, 18, 25, 27, 32};
int[] pokemonAps2 = {1, 1, 2, 2, 3};
  
for(int i = 0; i < pokemonNames1.length; i++) {
/*FIX(1) create a Pokemon object with the following three arguments:pokemonNames1[i], pokemonMaxHps1[i], pokemonAps1[i]
Add the Pokemon object to the Pokemon array list in player1
Increment player1's level by 1*/

}
  
for(int i = 0; i < pokemonNames2.length; i++) {
/*FIX(2) create a Pokemon object with the following three arguments:pokemonNames2[i], pokemonMaxHps2[i], pokemonAps2[i]
Add the Pokemon object to the Pokemon array list in player2
Increment player2's level by 1*/
}
  
}
  
public static void play(Random random, Player player1, Player player2) {
Pokemon p1, p2;
int index1, index2;
Scanner scnr = new Scanner(System.in);
boolean result;
  
System.out.println("Let's play!");
System.out.println(player1.getName() + " choose a pokemon to fight enter 1 to " + player1.getPokemons().size() + " or -1 to stop");
index1 = scnr.nextInt();
System.out.println(player2.getName() + " choose a pokemon to fight enter 1 to " + player2.getPokemons().size() + " or -1 to stop");
index2 = scnr.nextInt();
  
while(index1 != -1 && index2 != -1) {
p1 = player1.getPokemons().get(index1-1);
p2 = player2.getPokemons().get(index2-1);
  
result = fight(random, p1, p2);
if(result) {
/*FIXME(3) p1 won, reset p2, remove p2 from player2, and add p2 to player 1, increment player1's level by 1*/
  
}
else {
/*FIXME(4) p2 won, reset p1, remove p1 from player1, and add p1 to player 2, increment player2's level by 1*/
}
  
System.out.println(player1.getName() + " choose a pokemon to fight enter 1 to " + player1.getPokemons().size() + " or -1 to stop");
index1 = scnr.nextInt();
System.out.println(player2.getName() + " choose a pokemon to fight enter 1 to " + player2.getPokemons().size() + " or -1 to stop");
index2 = scnr.nextInt();
}
}
}

__________________________________________________________________

import java.util.ArrayList;
public class Player {
/*FIXME(1) Add three fields: name, level, pokemons*/
private String name;
private int level;
private ArrayList<Pokemon> pokemons;

/*FIXME(2) Add a constructor, which takes one parameter: String n.
Initialize name to n, level to 0, and create an ArrayList for pokemons*/

public Player(String name) {
this.name = name;
this.level = 0;
this.pokemons = new ArrayList<Pokemon>();
}
/*FIXME (3) Define the accessor for name*/
  
public String getName() {
return name;
}
/*FIXME (4) Define the accessor for level*/

public int getLevel() {
return level;
}
/*FIXME (5) Define the accessor for pokemons*/

public ArrayList<Pokemon> getPokemons() {
return pokemons;
}
/*FIXME (6) Define the mutator for level*/
public void setLevel(int level) {
this.level = level;
}

/*FIXME(7) Complete this method by adding p into the Pokemon ArrayList*/
public void addPokemon(Pokemon p){
this.pokemons.add(p);
}

/*FIXME(8) Complete this method by removing p from the Pokemon ArrayList*/
public void removePokemon(Pokemon p){
pokemons.remove(p);
}

public void printStat(){
System.out.println(" Player: " + name + " at level " + level + ", has " + pokemons.size() + " pokemons");
/*FIXME(9) Complete this method by printing the stat of each pokemon in the Pokemon ArrayList.
Each pokemon's stat is printed in an individual line. Hint: You can call the printStat method in Pokemon class*/

}
}

Explanation / Answer

Solution:

###Player.java########

import java.util.ArrayList;

public class Player {

/*FIXME(1) Add three fields: name, level, pokemons*/

private String name;

private int level;

private ArrayList<Pokemon> pokemons;

/*FIXME(2) Add a constructor, which takes one parameter: String n.

Initialize name to n, level to 0, and create an ArrayList for pokemons*/

public Player(String name) {

this.name = name;

this.level = 0;

this.pokemons = new ArrayList<Pokemon>();

}

/*FIXME (3) Define the accessor for name*/

public String getName() {

return name;

}

/*FIXME (4) Define the accessor for level*/

public int getLevel() {

return level;

}

/*FIXME (5) Define the accessor for pokemons*/

public ArrayList<Pokemon> getPokemons() {

return pokemons;

}

/*FIXME (6) Define the mutator for level*/

public void setLevel(int level) {

this.level = level;

}

/*FIXME(7) Complete this method by adding p into the Pokemon ArrayList*/

public void addPokemon(Pokemon p){

this.pokemons.add(p);

}

/*FIXME(8) Complete this method by removing p from the Pokemon ArrayList*/

public void removePokemon(Pokemon p){

pokemons.remove(p);

}

public void printStat(){

System.out.println(" Player: " + name + " at level " + level + ", has " + pokemons.size() + " pokemons");

/*FIXME(9) Complete this method by printing the stat of each pokemon in the Pokemon ArrayList.

Each pokemon's stat is printed in an individual line. Hint: You can call the printStat method in Pokemon class*/

}

}

####PokemonField.java####

import java.util.Random;

import java.util.Scanner;

public class PokemonField {

static Player player1 = new Player("dragonslayer");

static Player player2 = new Player("voldemort");

public static void main(String[] args) {

Random random = new Random();

initialize(player1, player2);

player1.printStat();

player2.printStat();

play(random, player1, player2);

System.out.println("Game over");

player1.printStat();

player2.printStat();

}

public static boolean fight(Random random, Pokemon p1, Pokemon p2){

double prob = (double)p1.getAttackPoints() / (p1.getAttackPoints() + p2.getAttackPoints());

while(p1.getHitPoints() > 0 && p2.getHitPoints() > 0) {

if(random.nextDouble() < prob) {

p2.setHitPoints(Math.max(p2.getHitPoints()- p1.getAttackPoints(), 0));

}

else{

p1.setHitPoints(Math.max(p1.getHitPoints() - p2.getAttackPoints(), 0));

}

}

if(p1.getHitPoints() > 0){

System.out.println(p1.getName() + " won!");

return true;

}

else {

System.out.println(p2.getName() + " won!");

return false;

}

}

public static void initialize(Player player1, Player player2) {

String[] pokemonNames1 = {"Butterfree", "Ivysaur", "Pikachu", "Squirtle"};

int[] pokemonMaxHps1 = {10, 15, 20, 30};

int[] pokemonAps1 = {1, 2, 2, 3};

String[] pokemonNames2= {"Jigglypuff","Oddish","Gloom","Psyduck","Machop"};

int[] pokemonMaxHps2 = {12, 18, 25, 27, 32};

int[] pokemonAps2 = {1, 1, 2, 2, 3};

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

/*FIX(1) create a Pokemon object with the following three arguments:pokemonNames1[i], pokemonMaxHps1[i], pokemonAps1[i]

Add the Pokemon object to the Pokemon array list in player1

Increment player1's level by 1*/

player1.addPokemon(new Pokemon(pokemonNames1[i], pokemonMaxHps1[i], pokemonAps1[i]));

}

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

/*FIX(2) create a Pokemon object with the following three arguments:pokemonNames2[i], pokemonMaxHps2[i], pokemonAps2[i]

Add the Pokemon object to the Pokemon array list in player2

Increment player2's level by 1*/

player2.addPokemon(new Pokemon(pokemonNames2[i], pokemonMaxHps2[i], pokemonAps2[i]));

}

}

public static void play(Random random, Player player1, Player player2) {

Pokemon p1, p2;

int index1, index2;

Scanner scnr = new Scanner(System.in);

boolean result;

System.out.println("Let's play!");

System.out.println(player1.getName() + " choose a pokemon to fight enter 1 to " + player1.getPokemons().size() + " or -1 to stop");

index1 = scnr.nextInt();

System.out.println(player2.getName() + " choose a pokemon to fight enter 1 to " + player2.getPokemons().size() + " or -1 to stop");

index2 = scnr.nextInt();

while(index1 != -1 && index2 != -1) {

p1 = player1.getPokemons().get(index1-1);

p2 = player2.getPokemons().get(index2-1);

result = fight(random, p1, p2);

if(result) {

/*FIXME(3) p1 won, reset p2, remove p2 from player2, and add p2 to player 1, increment player1's level by 1*/

System.out.println("Player1 won");

p2.reset(); //resetting p2

player2.removePokemon(p2); //Removing p2 from player2

player1.addPokemon(p2); //Adding p2 to player1's pokemon list

player1.setLevel(player1.getLevel() + 1); //Incrementing level of player1 by 1

}

else {

/*FIXME(4) p2 won, reset p1, remove p1 from player1, and add p1 to player 2, increment player2's level by 1*/

System.out.println("Player2 won");

p1.reset(); //resetting p2

player1.removePokemon(p1); //Removing p2 from player2

player2.addPokemon(p1); //Adding p2 to player1's pokemon list

player2.setLevel(player2.getLevel() + 1); //Incrementing level of player1 by 1

}

System.out.println(player1.getName() + " choose a pokemon to fight enter 1 to " + player1.getPokemons().size() + " or -1 to stop");

index1 = scnr.nextInt();

System.out.println(player2.getName() + " choose a pokemon to fight enter 1 to " + player2.getPokemons().size() + " or -1 to stop");

index2 = scnr.nextInt();

}

}

}

###Pokemon.java####

public class Pokemon {

private String name;

private int maxHp;

private int hitPoints;

private int attackPoints;

public Pokemon(String name, int maxHp, int ap) {

this.name = name;

this.maxHp = maxHp;

hitPoints = maxHp;

attackPoints = ap;

}

public String getName(){

return name;

}

public int getHitPoints() {

return hitPoints;

}

public int getAttackPoints() {

return attackPoints;

}

public void setHitPoints(int hp) {

hitPoints = hp;

}

public void setAttackPoints(int ap) {

attackPoints = ap;

}

public void reset() {

hitPoints = maxHp;

}

public void printStat() {

System.out.println(name + " has " + hitPoints + " hit points and " + attackPoints + " attack points");

}

}

Sample Run:

Player: dragonslayer at level 0, has 4 pokemons

Player: voldemort at level 0, has 5 pokemons
Let's play!
dragonslayer choose a pokemon to fight
enter 1 to 4 or -1 to stop
1
voldemort choose a pokemon to fight
enter 1 to 5 or -1 to stop
1
Jigglypuff won!
Player2 won
dragonslayer choose a pokemon to fight
enter 1 to 3 or -1 to stop
1
voldemort choose a pokemon to fight
enter 1 to 6 or -1 to stop
2
Ivysaur won!
Player1 won
dragonslayer choose a pokemon to fight
enter 1 to 4 or -1 to stop
3
voldemort choose a pokemon to fight
enter 1 to 5 or -1 to stop
2
Squirtle won!
Player1 won

Note: If you have any doubt please do comment below before giving any negative feedback.