Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please use Java . You have the Hero.java class: public class Hero implements Cloneable { private int energy; private String name, heroClass; private String[] backpack;

Please use Java.

You have the Hero.java class:

public class Hero implements Cloneable {

private int energy; private String name, heroClass; private String[] backpack; private int BACKPACK_SLOTS = 5; /** * Default no-arg constructor. * Precondition: none * Postcondition: name = Anonymous, energy = 100, heroClass = warrior, backpack is empty. * */ public Hero() { energy = 100; name = "Anonymous"; heroClass = "warrior"; backpack = new String[BACKPACK_SLOTS]; } /** * 3-arg constructor * Precondition: Incoming name is not null and not empty * Precondition: Incoming energy is greater than 0 * Precondition: Incoming heroClass is one of warrior, wizard or ranger * Postcondition: All data fields initialized, backpack is empty. * @param energy - initial energy * @param name - initial name * @param heroClass - initial class * @throws IllegalArgumentException if energy is negative. * @throws IllegalArgumentException if name is null or empty. * @throws IllegalArgumentException if class is not warrior, wizard or ranger. */ public Hero(int energy, String name, String heroClass) { if(energy = 0."); else this.energy = energy; if(name == null || name.equals("")) throw new IllegalArgumentException("Name is invalid."); else this.name = name; if(heroClass.toLowerCase().equals("warrior") || heroClass.toLowerCase().equals("wizard") || heroClass.toLowerCase().equals("ranger")) this.heroClass = heroClass; else throw new IllegalArgumentException("Class " + heroClass + " is invalid."); backpack = new String[BACKPACK_SLOTS]; } /** * Get the Hero's name. * @return Hero's name. */ public String getName() { return new String(name); } /** * Get the Hero's energy level. * @return current energy level. */ public int getEnergy() { return energy; } /** * Get the Hero's class. * @return Hero's class. */ public String getHeroClass() { return new String(heroClass); } /** * Show what is currently in the Hero's backpack. * @return a list of what is curently in the hero's backpack. */ public String showInventory() { String temp = ""; if(backpack[0] == null) temp = "You are not burdened by material possesions."; else { int i = 0; while(i

temp = temp.substring(0, temp.length()-1); } return temp; } /** * Determine if the Hero is still alive (true if energy > 0). * @return Hero's class. */ public boolean isAlive() { return energy > 0; } /** * Take away energy points from the hero. * Precondition: injury is positive. * Postcondition: energy is 0 or greater. * @param injury The amount of damage to take from the hero's energy. * @throws IllegalArgumentException if injury negative. */ public void takeDamage(int injury) { if(injury

}

and the HeroGUI.java class:

import java.awt.GridLayout; import java.awt.LayoutManager; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.Iterator; import java.util.LinkedList;

import javax.swing.*;

public class HeroGui extends JFrame{ private HeroList heros; private JPanel addHeroPanel,savePanel,LifeDeathPanel; private JButton addHero, removeHero, saveToFile, removeDead,healAll; private JTextField nameField, classField, energyField, saveToFileField; private JLabel nameLabel, classLabel,energyLabel, saveToFileLabel; JTextArea output;

private JPanel statsPanel;

private JButton highest;

private JButton total;

private JButton average;

private JPanel searchPanel;

private JTextField searchField;

private JButton searchButton;

private JLabel searchLabel;

private JLabel indexLabel;

private JTextField indexField;

private JPanel removePanel;

private JButton remove;

private JTextField RnameField;

private JLabel RnameLabel; public HeroGui(){ heros = new HeroList(); this.setLayout(new GridLayout(7, 1)); //Output Panel output = new JTextArea(2, 20); output.setEditable(false); this.add(output); //Add hero Panel addHeroPanel= new JPanel(); addHero = makeButton("Add Hero", new ButtonHandler()); nameField= new JTextField(10); nameLabel= new JLabel("Name"); classField = new JTextField(10); classLabel = new JLabel("Class"); energyField = new JTextField(10); energyLabel = new JLabel("Energy"); removeHero = makeButton("Remove Hero", new ButtonHandler()); addAll(addHeroPanel, nameLabel, nameField, classLabel, classField, energyLabel, energyField, addHero); this.add(addHeroPanel); //Remove hero panel removePanel = new JPanel(); indexLabel = new JLabel("Index"); indexField = new JTextField(4); RnameLabel= new JLabel("Name"); RnameField = new JTextField(4); remove = makeButton("Remove", new ButtonHandler()); addAll(removePanel,indexLabel,indexField, RnameLabel, RnameField, remove); this.add(removePanel); //Search by name searchPanel = new JPanel(); searchField = new JTextField(10); searchLabel= new JLabel("Name of Hero"); searchButton = new JButton("Search"); addAll(searchPanel,searchLabel, searchField,searchButton); this.add(searchPanel); //Stats Panel highest energy level //Highest, total, and average energy points statsPanel = new JPanel(); highest = makeButton("healthiest hero", new ButtonHandler()); total= makeButton("Total Energy", new ButtonHandler()); average = makeButton("Average Energy", new ButtonHandler()); addAll(statsPanel, highest,total,average); this.add(statsPanel); //Save toFile savePanel = new JPanel(); saveToFile = makeButton("Save",new ButtonHandler()); saveToFileLabel = new JLabel("Filename"); saveToFileField = new JTextField(10); addAll(savePanel, saveToFileLabel, saveToFileField,saveToFile); this.add(savePanel); //Life and Death LifeDeathPanel = new JPanel(); healAll= makeButton("Heal all heros", new ButtonHandler()); removeDead = makeButton("Remove all Dead", new ButtonHandler()); addAll(LifeDeathPanel, healAll, removeDead); this.add(LifeDeathPanel); } public JButton makeButton(String name, ActionListener listener){ JButton button = new JButton(name); button.addActionListener(listener); return button; } public void addAll(JPanel base, JComponent... components){ for(JComponent comp: components){ base.add(comp); } } public void print(Object o){ System.out.println(o); } public class ButtonHandler implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { if(e.getSource().equals(addHero)){ heros.addHero(Integer.parseInt((energyField.getText())), nameField.getText(), classField.getText()); output.setText(heros.toString()); } else if(e.getSource().equals(removeDead)){ heros.removeDead(); output.setText(heros.toString()); } else if(e.getSource().equals(healAll)){ heros.healAll(); output.setText(heros.toString()); } else if(e.getSource().equals(remove)){ String index =indexField.getText() ; String name = RnameField.getText(); if(index.length()>0 && name.length()>0){ print("index and string"); heros.deleteHero( name, Integer.parseInt(index)); output.setText("Hero Removed"); } else if(index.length()>0){ heros.deleteHero(index); output.setText("Hero Removed"); } else if (name.length()>0){ heros.deleteHero(name); output.setText("Hero Removed"); } else{ output.setText("Hero not removed"); } } else if(e.getSource().equals(searchButton)){ if(heros.inList(searchField.getText())){ output.setText("Hero with that name is in the list"); }else{ output.setText("Hero with that name is not in the list"); } } else if(e.getSource().equals(saveToFile)){ try { heros.saveToFile(saveToFileField.getText()); } catch (IOException x) { x.printStackTrace(); } output.append("Saved to "+saveToFileField.getText()); } else if(e.getSource().equals(highest)){ output.setText("The hero with the highest health is "+ heros.healthiest().toString()); } else if(e.getSource().equals(total)){ output.setText("Total health is: "+ heros.totalHealth()); } else if(e.getSource().equals(average)){ output.setText("Average health is: " +heros.averageHealth()); } //Matches no buttons else{ //Does nothing } } } public class HeroList { private LinkedList heros = new LinkedList(); public void addHero(int energy, String name, String className){ heros.add(new Hero(energy,name, className)); } /** * Delete first hero with that name */ public void deleteHero(String name){ Iterator it = heros.iterator(); while(it.hasNext()){ Hero h= it.next(); if(h.getName().equals(name)){ it.remove(); } } } /** * Deletes the hero at index * @param index */ public void deleteHero(int index){ heros.remove(index); } /**Deletes if the name hero at index matches the name parameter * @param name of the hero * @param index of the object */ public void deleteHero(String name, int index){ if(heros.get(index).getName().equals(name)){ deleteHero(index); } } /** * Heal all living heros a random amount between 0 and 50 */ public void healAll(){ for(Hero h:heros){ if(h.isAlive()) h.heal((int)(Math.random()*50.0 +1)); } } /** * Deletes all dead heros from the list */ public void removeDead(){ Iterator it =heros.iterator(); while(it.hasNext()){ Hero h = it.next(); if(h.isAlive()==false) it.remove(); } } public String toString(){ String s=""; for(Hero h: heros){ s+=" "+h.toString(); } return s; } /** * @return a copy of the healthiest hero */ public Hero healthiest(){ int index=0; int highestEnergy=heros.get(index).getEnergy(); Iteratorit = heros.iterator(); for (int i=0;ihighestEnergy){ index=i; highestEnergy=energy; } } Hero h = heros.get(index); return new Hero(h.getEnergy(),h.getName(),h.getHeroClass()); } public double totalHealth(){ double health=0; for(Hero h:heros){ health+=h.getEnergy(); } return health; } public double averageHealth(){ return totalHealth()/heros.size(); } /** * Determine the total and average energy points of the * entire group of heros * @param name of the hero you are trying to find * @return boolean */ public boolean inList(String name){ for(Hero h :heros){ if(h.getName().equals(name)) return true; } return false; } public void saveToFile(String filename) throws IOException{ File f = new File(filename); f.createNewFile(); PrintWriter out = new PrintWriter(filename); out.println(heros); out.close(); } } public static void main(String[] args){ HeroGui gui = new HeroGui(); gui.setVisible(true); gui.setSize(600,600); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

I have already done most of the requirements for this problem. I only need for you to implement the following thing:

When the user enter a name, on the Name of Hero text field, then clicks the "Search" button, I need you to display the hero the user is searching for in the display used for the GUI. That is all. The display area is the one circle in black in the image below. Remember that you can only have three CASE SENSITIVE classes: warrior, wizard or ranger.

image text in transcribed

Thank you!

Herofasdads,warrior,energy.139 inventory.You are not burdened by material possesions.] Herohel,warrior.energy.177,inventory.You are not burdened by material possesions.] Herohela,warrior,energy:1042,inventory.You are not burdened by material possesions.] Name hela Class warrior Energy 1000 Add Hero Index 1 Name hello Remove Name of Hero Search healthiest hero Total Energy Average Energy Heal all heros Remove all Dead

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

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 19 21 2012 Proceedings Part 3 Lnai 7198

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284922, 978-3642284922

More Books

Students also viewed these Databases questions

Question

Plan, write, and format letters

Answered: 1 week ago