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

Question 2: Write a hero class (Hero.java) that could be used in a role playing

ID: 3879650 • Letter: Q

Question

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.

Explanation / Answer

The code is

import java.util.*;
public class Hero {
String name;
int health;
int max_items;
ArrayList<String> items;
public Hero(String name,int num)
{
this.name=name;
health=100;
if(num>10 || num<0)
throw new IllegalArgumentException();
else
max_items=num;
items=new ArrayList(max_items);
}
public Hero()
{
health=100;
max_items=2;
this.name="Anonymous";
items=new ArrayList(max_items);
}
public String inventory()
{
String s="The items are ";
if (items.size()==0)
return "The hero is not carrying anything";
else
{
int l=items.size();
for(int i=0;i<l;i++)
{
s=s+(i+1)+". "+items.get(i)+" ";
}
return s;
}
}
public void take(String i)
{
if(items.size()<max_items)
{
items.add(i);
}
else
System.out.println("It is not possible to add one more item");
}
public void drop(String i)
{
if(items.contains(i))
{
items.remove(i);
}
else
System.out.println("This item is not present in the inventory");
}
public void takeDamage(int d)
{
if(d>health)
health=0;
else
health-=d;
}
public void heal(int h)
{
if(health+h>100)
health=100;
else
health+=h;
}
public String toString()
{
return "The health is "+health+" "+inventory();
}
public static void main(String args[])
{
Hero h=new Hero();
System.out.println(h);
}
}

The output of this test case is

The health is 100
The hero is not carrying anything

Do give a thumbs up and in case there are doubts leave a comment.

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