Question: Enhance the HungerGames program. Create a Tribute class with three instance variables, name (a String), district (an int), and gender (a char). Provide this new

Enhance the HungerGames program. Create a Tribute class with three instance variables, name (a String), district (an int), and gender (a char). Provide this new Tribute class with a constructor to initialize its instance variables and with get methods to access them. Modify the HungerGames class by changing String deceased to Tribute deceased, making the tributes list a list of Tribute objects, and providing this additional local variable:

Map tributeMap = new HashMap<>(); 

Populate the tributes list with these objects:

Tribute("Jody", 3, 'f')

Tribute("Harve", 3, 'm',)

Tribute("Newt", 5, 'm')

Tribute("Claire", 6, 'f')

Tribute("Ruth", 10, 'f')

Then, in a for-each loop that iterates through the tributes list, use Map’s put method to put each tribute into tributeMap, using each tribute’s name as the key to the object that more completely describes that tribute. Instead of using Math.random to pick random indices, use the Collections class’s shuffle method to randomize the sequence of the tributes list. Then, in an ordinary for loop that iterates through all but the last element in the shuffled tributes list, do the following:

a) Use tributes.get

(i) to get the next object in the tributes list.

b) Use Map’s remove method to remove this object from the tributesMap map.

c) Display the three attributes of the removed object. After this for loop, in a print statement, call tributesMap.keySet() to display the keys of all remaining entries in tributesMap. Does the operation that removes an entry from the map also remove an element from the list?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

I will explain how to complete the tasks in the question one by one and finally provide the implementation for the requested program This program crea... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Introduction To Programming With Java A Problem Solving Approach Questions!