In: Computer Science
Create a class Superheros
member: Map heroMap //key = name, value = weapon (or get creative)
member: Set< String> powerSet //superpowers
*** initialize your map & set – preferably in constructor ***
/* One liner to ‘put’ the key, value pair you’re passing in.. */
Method: void putEntryInMap (String key, String value) {}
/* Another 1 liner – well maybe 2. (1) ‘get’ the value from the map with the key (2) capture that value and return it (to your testHarness) */
Method: String getEntryFromMap (String key) {}
/* Another 1/2 liner (1) ‘remove’ the value from the map with the key (2) check if you have actually removed your value from your map by printing the results of heroMap.hasKey(key) */
Method: void removeEntryFromMap (String key){}
/* For all the marbles… you need to: Get the keyset from your map. This will give you a set of all the keys in the map. With your keyset, iterate over each element in your set & in the loop ‘get’ the value for that key from your map & print it. (Note you will need to create a local Set variable in this method). */
Method void displayAllMapEntries(){}
/* add an element to your set */
Method void addToSet(String name){}
/* remove an element from your set */
Method void removeFromSet(String name){}
/* iterate over the elements in your set and print each */
Method void printSet(){}
In your testHarness:
- create a Superhero instance ( make sure your map & set are initialized in your Superhero ctor ) - invoke one of your Superhero methods to ‘put’ a few entries ( 4 or more ) in your map.
- invoke one of your Superhero methods to ‘display’ your cat entries - invoke one of your Superhero methods ‘get’ an entry
- capture the value returned in your harness & print it out. - invoke one of your Superhero methods ‘remove’ an entry - invoke one of your Superhero methods to ‘display’ each entry (do not just print the map)
- invoke one of your Superhero methods to ‘add’ a few entries into your set
Make sure you add a couple duplicate names!
- invoke one of your Superhero methods to ‘display’ your set (do not just print the set) - invoke one of your Superhero methods to ‘remove’ an entry from your set - invoke one of your Superhero methods to ‘display’ your updated set (do not just print the set)
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
public class Superheros {
private HashMap<String,String> heroMap;
private HashSet<String> powerSet;
public Superheros() {
super();
this.heroMap=new HashMap<>();
this.powerSet=new HashSet<>();
}
public void putEntryInMap(String key,String value) {
heroMap.put(key, value);
}
public String getEntryFromMap(String key){
return heroMap.get(key);
}
public void removeEntryFromMap(String key) {
heroMap.remove(key);
}
public void displayAllMapEntries() {
Set<String> keySet=heroMap.keySet();
for (String hero : keySet) {
String weapon=heroMap.get(hero);
System.out.println(hero+" uses "+weapon+" weapon");
}
}
public void addToSet(String name) {
powerSet.add(name);
}
public void removeFromSet(String name) {
powerSet.remove(name);
}
public void printSet() {
int i=0;
for (String power : powerSet) {
System.out.println(++i+"th Superpower of Avengers is "+power);
}
}
}
class TestHarness{
public static void main(String[] args) {
Superheros superheros=new Superheros();
superheros.putEntryInMap("IronMan", "MK42");
superheros.putEntryInMap("CaptainAmerica", "Shield");
superheros.putEntryInMap("Thor", "Hammer");
superheros.putEntryInMap("Antman", "PymParticle");
superheros.putEntryInMap("DrStrange", "TimeStone");
superheros.displayAllMapEntries();
String entry="DrStrange";
System.out.println(entry+" uses "+superheros.getEntryFromMap(entry));
System.out.println("Updated Map of super heroes and their weapons:");
superheros.removeEntryFromMap(entry);
superheros.displayAllMapEntries();
superheros.addToSet("Fly");
superheros.addToSet("Summon Thunder");
superheros.addToSet("Fight");
superheros.addToSet("Web Shooter");
superheros.addToSet("Stamina");
superheros.addToSet("Jump Through Dimensions");
superheros.addToSet("Fly");
superheros.addToSet("Fly");
superheros.addToSet("Web Shooter");
superheros.printSet();
superheros.removeFromSet("Stamina");
System.out.println("Updated Set of super powers:");
superheros.printSet();
}
}