Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Question 2: Write a hero class (Hero.java) that could be used in a role playing game. A hero has a name, an amount of health,
Question 2: Write a hero class (Hero.java) that could be used in a role playing game. A hero has a name, an amount of health, an amount of mana (magic power) and can carry a limited number of items to help him or her on their quest. Your class should have data fields as follows name - self-explanatory? health - an integer number between 100 (full health) and 0 (expired/deceased) max items- the maximum number of items that the hero can carry at one time items - a list (ArrayList of Strings?) of items currently in the hero's possession. Think of this as the hero's backpack of holding. Once this value is set, it cannot be changed. The class should define the following methods Constructor (argumented): This constructor should allow for two arguments a name and a number of items. The initial health value for a hero should be 100. A new hero should not be carrying any items but should have an empty list ready to accept items later on. If the maximum items is greater than 10 or less than 0 throw an IllegalArgumentException (look back in the book if you need a reminder) -Constructor (default): This constructor should allow for zero arguments. The initial health value 100. The hero's name should be "Anonymous" and they should have a limit of 2 items in their backpack. -inventory This method takes no arguments and returns (not simply print) a String representation of the items that the hero is currently carrying. If the hero is carrying nothing then a message should be displayed. take): This method should take one argument the name of an item that the hero is adding to their inventory The hero can only "take" an item if they are carrying less than their maximum number of items. If the hero is at their maximum carrying capacity, print an appropriate message -drop): This method should take a single argument the name of an object the hero would like to remove from their inventory. If the user is not currently carrying the requested item then print an appropriate message, otherwise remove the item from inventory -takeDamage(): This method takes a single integer argument and subtracts that amount from the hero's health. It the resulting hero health would be less than zero, set the health to zero. -heal): This method takes a single integer argument and adds that amount from the hero's health. It the resulting hero health would be greater than 100, set the health to 100 toString() write a proper toString method that will display the current state of a hero
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