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

Implement a new class called a keyedBag. This new class is similar to the ordina

ID: 3733738 • Letter: I

Question

Implement a new class called a keyedBag. This new class is similar to the ordinary bag, but whenever an item is added, the programmer using the bag also provides an integer called key. Each item added to the keyed bag must have a unique key; two items cannot have the same key. So the insertion function has the specification shown here:
void keyed_bag::insert(const value_type& entry, int key);

//Precondition: size() < CAPACITY
//the bag does not contain any item with the given key.
//Postcondition: A new copy of entry
//has been added to the bag, with the given key.

When the programmer wants to remove an item from a keyed bag, the key of the item must be specified, rather than the item itself. The keyed bag should have a Boolean member function that can be used to determine whether the bag has an item with a specified key. In a keyed bag the programmer using the class specifies a particular key when an item is inserted. Write the complete specification, design and implementation of the class keyed bag. Note the each item of the bag must be associated with a key. Therefore identify a design and the data structures that can help you to support this feature.

Write a c++ program that tests the keyedBag class.
These are the actions performed by the driver:
1. Create a keyedBag class.
2. Inserts 5 items in the keyed bag with the key provided by the user using the overloaded operator >>. Do not use the value 3A as a key.
3. Print the content of the keyed bag using the overloaded operator <<.
4. Remove the items by using their key.
5. Print the content of the keyed bag again.
6. Add one more item to the keyed bag
7. Print again the content of the keyed bag.
8. Remove the items with key 3A and verify that this is illegal.
9. Print again the content of the bag with receipts.
10. Now add the item with the key 3A 11. Print again the content of the keyed bag.

thank you :)

Explanation / Answer

There can be many ways to implement them. You can use HashMap in Java which store key value pairs. Or you can use simple array method you have described above (using data[i] and location[i]).

One more lucid method can be -

String data[200][2];
where String[0][i] would store the key for element i,
and String[1][i] would store the data element.

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