Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this programming question, you have the following classes: Hero.java and HeroTester.java. Hero.java is as follows: package question2; import java.util.ArrayList; import java.util.Scanner; public class Hero

In this programming question, you have the following classes: Hero.java and HeroTester.java.

Hero.java is as follows:

package question2; import java.util.ArrayList; import java.util.Scanner; public class Hero { private String name; private int health; private int max_items; private ArrayList items; /** * Constructor used to set values. * * @param name for the hero's name. * @param max_items for the maximum number of items that the hero can carry. */ 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 < 0 || max_items > 10) throw new IllegalArgumentException("The maximum number of items that you can carry is from 1 to 10"); // set the max_items to the the second argument provided. this.max_items = max_items; } catch(IllegalArgumentException e) { System.out.println("You cannot carry that many items! " + 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 method which stores the items the hero's items. * * @return a string using the .toString method */ public String inventory() { String sb = ""; // If the hero is not carrying anything, print a suitable message if (items.size() == 0) { sb += ("Your hero is unburdened by wordly possessions."); } // prints the list of items carried by hero else { sb += ("Displaying Inventory : "); sb += ("["); // Loop1: To print all the items for (int i = 0; i < items.size(); i++) { sb += (items.get(i) + ", "); } sb += (" ]"); } return sb.toString(); } /** * Take method for adding items to hero's inventory/. * * @param item is used for the hero to take/ carry an item */ public 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 method used for hero to drop items from the hero's inventory * * @param item which the hero may/may not drop. Depends on either hard-coded hero * or the final user input. */ public void drop(String 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 System.out.println("Brave sir " + getName() + " drops his " + item); items.remove(item); } } /** * Take damage method for the hero to take damage and lose health * * @param amount is used to keep track of how much damage the hero can take. */ public void takeDamage(int amount) { health -= amount; //if hero's health is less than 0, make it 0 if(health < 0) { health = 0; System.out.println("Remember, a hero can't have negative health nor health > 100"); } } /** * Heal method is used to heal the hero * * @param amount is used to heal the hero. */ public 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"); } } @Override /** * toString method used for printing necessary information. */ public String toString() { return "Displaying hero: Hero: Brave Sir " +this.name+ ", "+ "health: " + this.health + ", " + "can carry: " + this.max_items+ ", " + "current inventory: " + inventory(); } /** * getName method to get the hero's name. * * @return hero's name */ public String getName() { return this.name; } /** * setName method to set the hero's name. * * @param name is hero's name. */ public void setName(String name) { this.name = name; } }

HeroTester is as follows:

package question2; import java.util.Scanner; public class HeroTester { public static void main(String[] args) { //Option 1 will decide if user chooses hard coded heroes // or to create a user inputted hero int opt1; Scanner sc = new Scanner(System.in); do { System.out.print("1. Hard code heroes 2. Enter Hero values Enter your option: "); opt1 = sc.nextInt(); System.out.println(" "); if(opt1 != 1 && opt1 != 2) {System.out.println("Please enter a valid choice!");} }while(opt1!=1 && opt1 !=2);

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()); //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; int opt2; do { System.out.print("1. Create an Anonymous Hero. 2. Create a hero with a name."); System.out.print(" Enter your choice: "); opt2 = sc.nextInt(); if(opt2!=1 && opt2 != 2) {System.out.println("Please enter a valid choice! ");} }while(opt2!=1 && opt2 != 2); 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) { //Menu options 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: //In case user enters an invalid option. System.out.println(" Enter a valid option!!"); } } }

} }

Using the following classes, find a way to load and save to a file instead of forcing the user to create a new character every time. Using the following is not allowed:

- String Builder

- Buffered Readers

- Apache commons or any others of the type.

Remeber, the key here is to allow the user to load a character from a file in your desktop (or a location of your preference) and use it to run through the code. Then allow the user to save the progress on the same file, again. Good luck!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Development For Dummies

Authors: Allen G. Taylor

1st Edition

978-0764507526

More Books

Students also viewed these Databases questions