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