Question: PLEASE ANSWER THE QUESTION WITHOUT STRINGBUILDER OR STRINGBUFFER. USE NORMAL STRING (S) TO CONCATENATE THE INVENTORY METHOD. ALSO THE TESTER MUST WORK AS INTENDED. HERE

PLEASE ANSWER THE QUESTION WITHOUT STRINGBUILDER OR STRINGBUFFER. USE NORMAL STRING (S) TO CONCATENATE THE INVENTORY METHOD. ALSO THE TESTER MUST WORK AS INTENDED. HERE IS THE CODE, I HAVE. ALL YOU NEED TO DO, IS FIND AN ALTERNATIVE TO THE INVENTORY METHOD. I WANT THAT METHOD, WHICH IS USING STRINGBUILDER TO USE NORMAL STRING CONACATENATION.

Code:

Hero.java file:

import java.util.ArrayList;

import java.util.Scanner;

public class Hero { private String name;

private int health;

private int max_items;

private ArrayList items;

/*

* Argumented Constructor

* Arguments: name, max_items

*/

public Hero (String name, int max_items) {

// set the hero's name to provided name

this.name = name;

try {

// If the max_items is less than zero or more than 10, throw IllegalArgumentException

if(max_items 10)

throw new IllegalArgumentException("The max_items should be between 1 to 10");

// set the max_items to the the second argument provided.

this.max_items = max_items;

} catch(IllegalArgumentException e) {

System.out.println("Caught inside Argumented constructor : " + e.getMessage());

}

// set hero's health to 100

this.health = 100;

// initialize the items list as empty

this.items = new ArrayList();

}

/*

* Default constructor

*/

public Hero () {

// set the hero's name to "Anonymous"

this.name = "Anonymous";

// set the max_items to 2

this.max_items = 2;

// set hero's health to 100

this.health = 100;

// initialize the items list as empty

this.items = new ArrayList();

}

/*

* inventory(): print hero's inventory

* Argument: no argument

* Return a string containing all the items carried by Hero

*/

String inventory() {

StringBuilder sb = new StringBuilder();

//If the hero is not carrying anything, print a suitable message

if(items.size() == 0) {

sb.append("Your hero is unburdened by wordly possessions.");

}

// prints the list of items carried by hero

else {

sb.append("Displaying Inventory : ");

sb.append("[");

//Loop1: To print all the items

for(int i=0; i

sb.append(items.get(i) + ", ");

}

sb.append(" ]");

}

return sb.toString();

}

/*

* take(): add an item to the hero's inventory

* Argument: The name of the item that the hero wants to add to his inventory

* Print an appropriate message if the inventory is full.

*/

void take(String item) {

System.out.println("Handling Sir " + getName() + " " + item);

// If the inventory is full, print an appropriate message

if(items.size() == max_items) {

System.out.println("Your hero is overburdened and cannot carry more...");

} else {

//add the item to the inventory

items.add(item);

}

}

/*

* drop(): drop an item from hero's inventory

* Argument: The name of the item that the hero wants to drop from his inventory

* Print an appropriate message if the hero is not carrying the item.

*/

void drop(String item) {

System.out.println("Brave sir " + getName() + " drops his " + item);

// If the hero is not carrying the item, print an appropriate message

if(!items.contains(item)) {

System.out.println("Your hero is not carrying that item - " + item);

} else {

//drop the item from the inventory

items.remove(item);

}

}

/*

* takeDamage(): subtracts the given amount from the hero's health

* Argument: The integer amount to be subtracted from the hero's health

*/

void takeDamage(int amount) {

health -= amount;

//if hero's health is less than 0, make it 0

if(health

health = 0;

System.out.println("Remember, a hero can't have negative health nor health > 100");

}

}

/*

* heal(): adds the given amount to the hero's health

* Argument: The integer amount to be added to the hero's health

*/

void heal(int amount) {

health += amount;

//if hero's health is more than 100, make it 100

if(health > 100) {

health = 100;

System.out.println("Remember, a hero can't have negative health nor health > 100");

}

}

/*

* toString(): displays the current state of the hero

* Argument: The integer amount to be subtracted from the hero's health

*/

@Override

public String toString() {

StringBuilder sb = new StringBuilder();

sb.append("Displaying hero : Hero : Brave Sir " + this.name + ", ")

.append("health: " + this.health + ", ")

.append("can carry: " + this.max_items + ", ")

.append("current inventory: " + inventory());

return sb.toString();

}

// get hero name

public String getName() {

return name;

}

// set hero name

public void setName(String name) {

this.name = name;

}

}

then there is the tester, called HeroTester:

import java.util.Scanner;

public class HeroTester { public static void main(String[] args) {

//opt1 decides whether to hard code Heroes to take user input to control them

// 1. Hard coded

// 2. Take user input to create heroes

int opt1;

System.out.print("1. Hard code heroes 2. Enter Hero values Enter your option: ");

Scanner sc = new Scanner(System.in);

opt1 = sc.nextInt();

System.out.println(" ");

if(opt1 == 1) {

//Hard code the hero values

System.out.println(" ----------Creating Heroes----------");

System.out.println("Creating Hero with no name.");

//create a new hero using default constructor

Hero hero1 = new Hero();

//print hero1 state

System.out.println(hero1.toString());

System.out.println("Creating Hero with a name.");

//create a new hero using argumented constructor

Hero hero2 = new Hero("Robin", 3);

//print hero2 state

System.out.println(hero2.toString());

System.out.println(" ----------Testing Methods----------");

//Add an item to inventory

hero2.take("sword");

System.out.println(hero2.inventory());

//Add an item to inventory

hero2.take("spoon");

System.out.println(hero2.inventory());

//Add an item to inventory

hero2.take("cape of good fortune");

System.out.println(hero2.inventory());

//Add an item to inventory

hero2.take("tomato");

System.out.println(hero2.inventory());

//Drop an item from inventory

hero2.drop("spoon");

System.out.println(hero2.inventory());

//Drop an item from inventory

hero2.drop("watch");

System.out.println(hero2.inventory());

//Drop an item from inventory

hero2.drop("sword");

System.out.println(hero2.inventory());

//Drop an item from inventory

hero2.drop("cape of good fortune");

System.out.println(hero2.inventory());

//Drop an item from inventory

hero2.take("pointy hat of success");

System.out.println(hero2.inventory());

//Reduce health

System.out.println("Brave sir " + hero2.getName() + " is so busy worrying about his inventory"

+ "he doesn't notice the danger and takes 50"

+ " damage from a shrubery!");

hero2.takeDamage(50);

System.out.println(hero2.toString());

//Increase health

System.out.println("Brave sir " + hero2.getName() + " takes a deep breath"

+ " and recovers 5 health");

hero2.heal(5);

System.out.println(hero2.toString());

//Reduce health

System.out.println("Brave sir " + hero2.getName() + " is so busy worrying about his inventory"

+ " he doesn't notice the danger and takes 119"

+ " damage from a shrubery!");

hero2.takeDamage(119);

System.out.println(hero2.toString());

//Increase health

System.out.println("Brave sir " + hero2.getName() + " enters the Avatar state"

+ " and heals for 128 points.");

hero2.heal(128);

System.out.println(hero2.toString());

//print the heros' final state

System.out.println(" ----------Final State----------");

System.out.println(hero1.toString());

System.out.println(hero2.toString());

} else if(opt1==2){

//User wants to enter values

Hero hero;

System.out.print("1. Create an Anonymous Hero. 2. Create a hero with a name.");

System.out.print(" Enter your choice: ");

int opt2;

opt2 = sc.nextInt();

System.out.println(" ");

if(opt2 == 1) {

System.out.println("Creating Hero with no name.");

//create a new hero using default constructor

hero = new Hero();

//print hero state

System.out.println(hero.toString());

} else {

System.out.println("Creating Hero with a name.");

System.out.print("Enter hero's name: ");

String name = sc.next();

System.out.print(" Enter max items that the hero can carry: ");

int max = sc.nextInt();

//create a new hero using argumented constructor

hero = new Hero(name, max);

//print hero state

System.out.println(hero.toString());

}

while(true) {

System.out.print(" 1. Add item to inventory 2. Drop item from inventory"

+ " 3. Cause damage to the hero 4. Heal the hero"

+ " 5. Display State of the hero 6. Exit ");

System.out.print(" Enter your choice: ");

int opt3;

opt3 = sc.nextInt();

System.out.println(" ");

switch(opt3) {

case 1:

//add item to hero's inventory

System.out.print("Enter item's name to be added: ");

String take = sc.next();

hero.take(take);

System.out.println(hero.toString());

break;

case 2:

//drop item from hero's inventory

System.out.print("Enter item's name to be dropped: ");

String drop = sc.next();

hero.drop(drop);

System.out.println(hero.toString());

break;

case 3:

//Cause damage to the hero

System.out.print("Enter the health damage amount: ");

int damage = sc.nextInt();

hero.takeDamage(damage);

System.out.println(hero.toString());

break;

case 4:

//Heal the hero

System.out.print("Enter the healing amount: ");

int heal = sc.nextInt();

hero.heal(heal);

System.out.println(hero.toString());

break;

case 5:

//Display the hero's state

System.out.println(hero.toString());

break;

case 6:

System.out.println("Exiting!!");

return;

default:

System.out.println(" Enter a valid option!!");

}

}

}

}

}

And here is the final output the question should display:

PLEASE ANSWER THE QUESTION WITHOUT STRINGBUILDER OR STRINGBUFFER. USE NORMAL STRING (S)

TO CONCATENATE THE INVENTORY METHOD. ALSO THE TESTER MUST WORK AS INTENDED.

HERE IS THE CODE, I HAVE. ALL YOU NEED TO DO, IS

If it works as intended, I will rate immediately. Thanks very much!

il Problems @Javadoc Declaration console 6 ProgressDebug HeroTester [Java Application) Library/Java/JavaVirtualMachines/jdk1.8.0144.jdk/Contents/Home/oinjava (09-Jan-2018, 9:52:30 PM) 1. Hard code heroes 2. Enter Hero values Enter your option: 1 -Creating Heroes Creating Hero with no name Displaying hero: HeroBrave Sir Anonymous, health: 100, can carry: 2, current inventory: Your hero is unburdened by wordly possessions Creating Hero with a name Displaying hero : Hero Brave Sir Robin, health: 100, can carry: 3, current inventory: Your hero is unburdened by wordly possessions Testing Methods Handling Sir Robin sword Displaying Inventory [sword, Handling Sir Robin spoon Displaying Inventory [sword, spoon, ] Handling Sir Robin cape of good fortune Displaying Inventory [sword, spoon, cape of good fortune, 1 Handling Sir Robin tomato Your hero is overburdened and cannot carry more... Displaying Inventory [sword, spoon, cape of good fortune, Brave sir Robin drops his spoon Displaying Inventory[sword, cape of good fortune, Brave sir Robin drops his watch Your hero is not carrying that item - watch Displaying Inventory[sword, cape of good fortune, ] Brave sir Robin drops his sword Displaying Inventory [cape of good fortune, ] Brave sir Robin drops his cape of good fortune Your hero is unburdened by wordly possessions Handling Sir Robin pointy hat of success Displaying Inventory [pointy hat of success, ] Brave sir Robin is so busy worrying about his inventoryhe doesn't notice the danger and takes 50 damage from a shrubery! Displaying hero: HeroBrave Sir Robin, health: 50, can carry: 3, current inventory: Displaying Inventory: [pointy hat of success, 1 Brave sir Robin takes a deep breath and recovers 5 health Displaying hero: Hero: Brave Sir Robin, health: 55, can carry: 3, current inventory: Displaying Inventory [pointy hat of success, Brave sir Robin is so busy worrying about his inventory he doesn't notice the danger and takes 119 damage from a shrubery

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!