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

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

ID: 3875929 • Letter: I

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 {
   private String name;
   private int health;
   private int max_items;
   private ArrayList<String> 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!

Explanation / Answer

HeroTest.java

package question2;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.util.ArrayList;

import java.util.Scanner;

public class HeroTester {

  

public static void main(String[] args) {

// save hero to file

WriteObject heroSave= new WriteObject();

//reazd hero from file

ReadObject heroRead= new ReadObject();

//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());  

// heroSave.serializeHeroSave(hero1);

//  

// heroSave.serializeHeroSave(hero2);

ArrayList<Hero> woi=new ArrayList<>();

woi.add(hero1);

woi.add(hero2);

heroSave.serializeHeroSave(woi);

System.out.println( heroRead.serializeHeroRead("c:\temp\Hero.ser"));

} 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

ArrayList<Hero> woi=new ArrayList<>();

woi.add(hero);

//woi.add(hero2);

heroSave.serializeHeroSave(woi);

System.out.println( heroRead.serializeHeroRead("c:\temp\Hero.ser"));

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

ArrayList<Hero> woi=new ArrayList<>();

woi.add(hero);

//woi.add(hero2);

heroSave.serializeHeroSave(woi);

System.out.println( heroRead.serializeHeroRead("c:\temp\Hero.ser"));

}

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!!");

}

}

}

}

}

// writeobject

package question2;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.util.ArrayList;

public class WriteObject {

public static void main(String args[]) {

WriteObject obj = new WriteObject();

Hero Hero = new Hero();

ArrayList<Hero> al= new ArrayList<>();

al.add(Hero);

obj.serializeHeroSave(al);

}

public void serializeHeroSave(ArrayList<Hero> woi) {

FileOutputStream fout = null;

ObjectOutputStream oos = null;

try {

fout = new FileOutputStream("c:\temp\Hero.ser");

oos = new ObjectOutputStream(fout);

oos.writeObject(woi);

// ReadObject r= new ReadObject();

System.out.println("Saved data to file SUCCEsfully" );

} catch (Exception ex) {

ex.printStackTrace();

} finally {

if (fout != null) {

try {

fout.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (oos != null) {

try {

oos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

public void serializeHeroSaveJDK7(Hero Hero) {

try (ObjectOutputStream oos =

new ObjectOutputStream(new FileOutputStream("c:\temp\Hero2.ser"))) {

oos.writeObject(Hero);

System.out.println("Done");

} catch (Exception ex) {

ex.printStackTrace();

}

}

}

//readobject

package question2;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.util.ArrayList;

import java.util.Iterator;

public class ReadObject {

public static void main(String args[]) {

ReadObject obj = new ReadObject();

Hero Hero = obj.serializeHeroRead("c:\temp\Hero.ser");

System.out.println(Hero);

}

public Hero serializeHeroRead(String filename) {

Hero Hero = null;

FileInputStream fin = null;

ObjectInputStream ois = null;

try {

fin = new FileInputStream(filename);

ois = new ObjectInputStream(fin);

ArrayList<Hero> woi=new ArrayList<>();

woi=(ArrayList<Hero>)ois.readObject();

Iterator<Hero> it= woi.iterator();

while(it.hasNext()) {

System.out.println(it.next());

}

} catch (Exception ex) {

ex.printStackTrace();

} finally {

if (fin != null) {

try {

fin.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (ois != null) {

try {

ois.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return Hero;

}

public Hero serializeHeroReadJDK7(String filename) {

Hero Hero = null;

try (ObjectInputStream ois

= new ObjectInputStream(new FileInputStream(filename))) {

Hero = (Hero) ois.readObject();

} catch (Exception ex) {

ex.printStackTrace();

}

return Hero;

}

}

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