Question

In: Computer Science

Create a class Superheros member: Map heroMap //key = name, value = weapon (or get creative)...

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)

Solutions

Expert Solution

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

Related Solutions

1. Use the get() method to print the value of the "name" key and the value...
1. Use the get() method to print the value of the "name" key and the value of the "age" key of the stuInfo dictionary. Use the variable given below: stuInfo = {'name': 'John Smith', "gpa": 3.456, "age": 20} 2. Use the dict() function to make a copy of the NY dictionary to NewYorkState dictionary. 3. Change the "name" value from "John Smith" to "James Bond" of the stuInfo dictionary. Use the variable given below: stuInfo = {'name': 'John Smith', "gpa":...
HashMap is a Map based collection class that is used for storing Key & value pairs,...
HashMap is a Map based collection class that is used for storing Key & value pairs, it is denoted as HashMap<Key, Value> or HashMap<K, V>. This class makes no guarantees as to the order of the map. It is similar to the Hashtable class except that it is unsynchronized and permits nulls(null values and null key). 5.Write a Java program to test if a map contains a mapping for the specified key.a.Add values in the Hash mapb.Printthe mapc.Check for a...
Create a UEmployee class that contains member variables for the university employee name and salary. The...
Create a UEmployee class that contains member variables for the university employee name and salary. The UEmployee class should contain member methods for returning the employee name and salary. Create Faculty and Staff classes that inherit the UEmployee class. The Faculty class should include members for storing and returning the department name. The Staff class should include members for storing and returning the job title. Write a runner program that creates one instance of each class and prints all of...
1)  Create a UEmployee class that contains member variables for the university employee name and salary. The...
1)  Create a UEmployee class that contains member variables for the university employee name and salary. The UEmployee class should contain member methods for returning the employee name and salary. Create Faculty and Staff classes that inherit the UEmployee class. The Faculty class should include members for storing and returning the department name. The Staff class should include members for storing and returning the job title. Write a runner program that creates one instance of each class and prints all of...
Create a student class that stores name, registration number and percentage, grade. Add member functions -...
Create a student class that stores name, registration number and percentage, grade. Add member functions - Read( string n, int r, float p): Read function that accepts parameter - Display(): To display student’s information - CalculateGrade()
directions: use c++ Create a  ContactInfo class that contains the following member variables: name age phoneNumber The...
directions: use c++ Create a  ContactInfo class that contains the following member variables: name age phoneNumber The ContactInfo class should have a default constructor that sets name = "", phoneNumber = "", and age = 0. The ContactInfo class should have a constructor that accepts the name and phone number as parameters and sets name = <value of parameter name>, aAge = 0, and phoneNumber = <value of parameter phone number>. The ContactInfo class should have accessor and mutator functions for...
First lab: Create a Fraction class Create member variables to store numerator denominator no additional member...
First lab: Create a Fraction class Create member variables to store numerator denominator no additional member variable are allowed Create accessor and mutator functions to set/return numerator denominator Create a function to set a fraction Create a function to return a fraction as a string ( common name ToString(), toString())  in the following format: 2 3/4 use to_string() function from string class to convert a number to a string; example return to_string(35)+ to_string (75) ; returns 3575 as a string Create...
Racket/Scheme language [15p] create-mapping A map data structure is described as either a set of key-value...
Racket/Scheme language [15p] create-mapping A map data structure is described as either a set of key-value pairs or a set of keys and a set of values whose relation is described by a function with a one-to-one mapping from keys to values. Write a function, create-mapping, which takes a list of symbols (keys) and a list of any type of Scheme values (vals) and returns a function that takes one symbol argument (the key) and returns the corresponding value. The...
Create a class DogWalker member List <String>doggies method: void addDog(String name ) //add dog to the...
Create a class DogWalker member List <String>doggies method: void addDog(String name ) //add dog to the List method: void printDogList() //print all dogs in list /* You’ll need an index. Iterate over your list of dog names in a while loop. Use your index to “get” the dog at the current index When you ‘find’ your dog name in the list, print it out */ Method: void findDogUsingWhile(String dogName) /* You’ll need an index. Iterate over your list of dog...
Question 1 (10) Create a class Student with public member variables: Student name, student number, contact...
Question 1 (10) Create a class Student with public member variables: Student name, student number, contact number, ID number. The following specifications are required:  Add init() method of the class that initializes string member variables to empty strings and numeric values to 0. (2)  Add the method populate() to the class. The method is used to assign values to the member variables of the class. (2)  Add the method display() to the class. The method is used...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT