Question
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
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
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