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

2 MODIFY the Player class from the previous assignment Add the following PRIVATE

ID: 3700387 • Letter: 2

Question

2 MODIFY the Player class from the previous assignment Add the following PRIVATE data: ArrayList cItem> inventory (default: new Array List) Add the following PUBLIC methods void addItem(Item item): adds an item to inventory boolean useltem (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 Array List) . Return true Otherwise, return false void printInventory ( Print "INVENTORY:" using System.out.println() Loop through your inventory Print "Item+i using System.out.printin (where i is the index in the loop) Print the Item (System.out.print In inventory.getfi)) ho 6 7 8 9

Explanation / Answer

public class Player

{

    // add the original data fields in place of this comment

   

    // add private data field inventory

    ArrayList<Item> inventory;

   

    Player()

    {

        // add the original code in place of this comment

       

        // create a new ArrayList

        this.inventory = new ArrayList<Item>();

    }

   

    Player(int x, int y)

    {

        // add the original code in place of this comment

       

        // create a new ArrayList

        this.inventory = new ArrayList<Item>();

    }

   

    // add all the methods in the class Player in place of this comment

   

    public void addItem(Item item)

    {

        // add item to inventory using add() function

        this.inventory.add(item);

    }

   

    public void useItem(int index)

    {

        // get the index element from inventory

        Item item = this.inventory.get(index);

       

        // add the heals from item to Health

        // assuming getHeals() return the heals

        .// change the name of method accordingly

        this.Health += item.getHeals();

       

        this.inventory.remove(index);

       

        return true;

    }

   

    public void printInventory()

    {

        int i;

       

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

        {

            System.out.println("Item " + i);

           

            System.out.println(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