Question: Enhance the code on bottom by creating a Tribute class with three instance variables: name (a String), gender (a char), and district (an int). Provide

Enhance the code on bottom by creating a Tribute class with three instance variables: name (a String), gender (a char), and district (an int). 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:
HashMap tributeMap = new HashMap<>();
Populate the tributes list with these objects:
Tribute(Cato, m, 2);
Tribute(Katniss, f, 12);
Tribute(Peeta, m,12);
Tribute(Rue, f, 11);
Tribute(Finnick, m, 4);
Then, in a for-each loop that iterates through the tributes list, use Maps put method to put each
tribute into tributeMap, using each tributes name as the key to the object that more completely
describes that tribute. Instead of Math.random to pick random indices, use the Collections classs
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:
-First, use tributes.get(i) to get the next object in the tributes list.
-Second, use Maps remove method to remove this object from the tributesMap map.
-Third, 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.
/******************************************************************
* HungerGames.java
* Dean & Dean
*
* This class creates an ArrayList of tributes.
* It randomly chooses one tribute and removes him/her.
******************************************************************/
import java.util.ArrayList;
public class HungerGames
{
public static void main(String[] args)
{
int deceasedIndex; // index of deceased tribute
String deceased; // name of deceased tribute
ArrayList tributes = new ArrayList<>();
tributes.add(Cato);
tributes.add(Katniss);
tributes.add(Peeta);
tributes.add(Rue);
tributes.add(1, Finnick);
deceasedIndex = (int) (Math.random() * tributes.size());
deceased = tributes.remove(deceasedIndex);
System.out.println(deceased + is out of the game.);
System.out.println(Remaining: + tributes);
} // end main
} // end HungerGames

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Databases Questions!