62.9k views
1 vote
Imagine you're developing a Pokemon game, and you need to implement the battle party mechanic. Recall that the player's party can hold at most 6 Pokemon. To simplify things, you want to implement it as an ArrayList containing 6 elements (1 for each Pokemon in the party), and to simplify things even further, you decide to avoid making a custom Pokemon class. Instead, you choose to represent each Pokemon as a HashMap containing two keys: "Name" and "Level". The value associated with the key "Name" will be a String denoting the Pokemon's name, and the value associated with the key "Level" will be an Integer denoting the Pokemon's level. TASK: Write a public static method called createParty that has one parameter of type String[] called names containing the Pokemon names, followed by a parameter of type int[] called levels containing the Pokemon levels (where names[i] and levels[i] are the name and level of Pokemon i in the party). It should return the party as a ArrayList> as described above HINT: Each Pokemon would be its own HashMap, and each of these HashMaps has exactly two keys, both of which are Strings: "Name" and "Level" Sample Input: Pikachu Venusaur Charizard Blastoise Lapras Snorlax 88 84 84 84 80 82 Sample Output: Pikachu 88 Venusaur 84 Charizard 84 Blastoise 84 Lapras 80 Snorlax 82 Write a program, test using stdin → stdout

1 Answer

4 votes

Answer:

Complete code is given below and output is also attached.

Step-by-step explanation:

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.Set;

public class Test {

public static void main(String[] args) {

String [] names = {"Pikachu", "Venuasur", "Charizard", "Blastoise", "Lapras", "Sonarlax"};

int[] levels = {88, 84, 84, 84, 80, 82};

ArrayList<HashMap<String, Integer>> party = createParty(names, levels);

//looping through the list

for(HashMap<String, Integer> i: party) {

Set set = i.entrySet();

Iterator itr = set.iterator();

//showing output

while(itr.hasNext()) {

Map.Entry mentry = (Map.Entry)itr.next();

System.out.println(mentry.getKey()+" "+mentry.getValue());

}

}

}

private static ArrayList<HashMap<String, Integer>> createParty(String[] names, int[] levels) {

ArrayList<HashMap<String, Integer>> party = new ArrayList<>();

for(int i=0; i<names.length; i++) {

//creating new hashmap

HashMap<String, Integer> pokemon = new HashMap<>();

pokemon.put(names[i], levels[i]);//adding pokemon

party.add(pokemon);//adding it to the list

}

return party;

}

}

Imagine you're developing a Pokemon game, and you need to implement the battle party-example-1
User Meroelyth
by
7.4k points