Question
For this assignment, you will have FIVE Java files: Weapon.java (from the previous assignment), Player.java (modified from the previous assignment), Item.java, Food.java, and Assign05.javaInclude the
For this assignment, you will have FIVE Java files: Weapon.java (from the previous assignment), Player.java (modified from the previous assignment), Item.java, Food.java, and Assign05.javaInclude the Weapon class from the previous assignment (no modification necessary) 2 MODIFY the Player class from the previous assignment 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 Players 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))) 3 Implement an Item class It should contain the following PRIVATE data: Page 2 of 9 String name (default: (empty String)) double weight (default: 0) It should have the following PUBLIC methods: Constructor that takes no arguments Constructor that takes name and weight (in that order) Getter method: String getName() Getter method: double getWeight() String toString() Override toString() to return a String with the following format: Example: if name = Spoon and weight = 2: Name: Spoon Weight: 2 4 Implement a Food class that inherits from Item It should contain the following PRIVATE data: double heals (default: 0) It should have the following PUBLIC methods: Constructor that takes name, weight, and heals (in that order) Getter method: double getHeals() Page 3 of 9 String toString() Override toString() to return super.toString() + heals: Example: if name = Cheese, weight = 3, and heals = 20: Name: Cheese Weight: 3 Healing Power: 20 5 Implement a class Assign05 to test the Player, Item, and Food classes. In its main method: Create a Player instance, player, with position (3,3). Do the following in a loop: Ask the user for an item name and weight. You can assume the name is a single word. Ask the user if its a food item. If user enters Y, then ask for healing power. If item is NOT a food item, create an Item instance and add it to player inventory. If item IS a food item, create a Food instance and add it to player inventory. ...while the user doesnt enter a String None for the name. Print player inventory. Ask user to choose an item to use (enter an integer). If the item was successfully used, print the current Player health. Otherwise, print Cannot use this item.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started