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

Into to Java Modify the Player program below with the following: Add the followi

ID: 3699401 • Letter: I

Question

Into to Java

Modify the Player program below with the following:

Add the following PRIVATE data: ArrayList inventory (default: new ArrayList<>())

Add the following PUBLIC methods: void addItem(Item item): adds an item to inventory.

boolean useItem(int index): If the index is in bounds AND the Item at inventory.get(index) is an instance of Food: • Add the heals from the item to the Player’s health • Remove the item from the inventory (remove() method of ArrayList) • Return true Otherwise, return false.

void printInventory(): Print ”INVENTORY:” using System.out.println(). Loop through your inventory: • Print ”Item ” + i using System.out.println (where i is the index in the loop) • Print the Item (System.out.println(inventory.get(i)))

Program:

public class Player {
private int X;
private int Y;
private int array[];
private int Health;
private Weapon currentWeapon;
public Player() {
array = new int[2];
array[0] = 0;
array[1] = 0;
this.X = 0;
this.Y = 0;
}
public Player(int x, int y){
array = new int[2];
array[0] = x;
array[1] = y;
this.X = x;
this.Y = y;
}
public int getX() {
return X;
}
public int getY() {
return Y;
}
public int getHealth() {
return Health;
}
public Weapon getCurrentWeapon() {
return currentWeapon;
}
public void setX(int x) {
X = x;
}
public void setY(int y) {
Y = y;
}
public void setHealth(int health) {
Health = health;
}
public void setCurrentWeapon(Weapon currentWeapon) {
this.currentWeapon = currentWeapon;
}
}

Explanation / Answer

public class Player {

    private int X;

    private int Y;

    private int array[];

    private int Health;

    private Weapon currentWeapon;

   

    private ArrayList<> inventory;

   

    public Player() {

        array = new int[2];

        array[0] = 0;

        array[1] = 0;

        this.X = 0;

        this.Y = 0;

       

        this.inventory = new ArrayList<>();

    }

    public Player(int x, int y){

        array = new int[2];

        array[0] = x;

        array[1] = y;

        this.X = x;

        this.Y = y;

    }

    public int getX() {

        return X;

    }

    public int getY() {

        return Y;

    }

    public int getHealth() {

        return Health;

    }

    public Weapon getCurrentWeapon() {

        return currentWeapon;

    }

    public void setX(int x) {

        X = x;

    }

    public void setY(int y) {

        Y = y;

    }

    public void setHealth(int health) {

        Health = health;

    }

    public void setCurrentWeapon(Weapon currentWeapon) {

        this.currentWeapon = currentWeapon;

    }

   

    // adds an item to inventory

    public void addItem(Item item)

    {

        this.inventory.add(item);

    }

   

    boolean useItem(int index)

    {

        // if index is in bounds

        if( index >= 0 && index < this.inventory.size() && ( inventory.get(index) instanceof Food ) )

        {

            // getHeals() returns the heals from item

            this.Health += item.getHeals();

           

            // Remove the item from the inventory

            this.inventory.remove(index);

           

            return true;

        }

        else

            return false;

    }

   

    public void printInventory()

    {

        System.out.println("INVENTORY: ");

       

        int i;

       

        for( i = 0 ; i < this.inventory.size() ; i++ )

            System.out.println("Item " + i + " " + this.inventory.get(i));

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote