- 1

Can anyone help?

You are creating a bowling game! The given code declares a Bowling class with its constructor and addPlayer() method. Each player of the game has a name and points, and are stored in the players HashMap. The code in main takes 3 players data as input and adds them to the game. You need to add a getWinner() method to the class, which calculates and outputs the name of the player with the maximum points. Sample Input: Dave 42 Amy 103 Rob 64 Sample Output: Amy import java.util.*; public class Bowling { HashMap<String, Integer> players; Bowling() { players = new HashMap<String, Integer>(); } public void addPlayer(String name, int p) { players.put(name, p); } //your code goes here // Collections.sort(players); System.out.println(players); // String,Integer getWinner(){ //Collections.sort(players); // System.out.println(players); } } public class Program { public static void main(String[ ] args) { Bowling

26th Apr 2021, 3:32 PM
Rachita Bhasin
2 Antworten
0
Thanks
1st May 2021, 1:41 AM
Rachita Bhasin
- 2
public void addPlayer(String name, int p) { players.put(name, p); } //your code goes here public void getWinner(){ String maxKey = null; int maxValue = 0; for(String i : players.keySet()) { if(players.get(i) > maxValue) { maxKey = i; maxValue = players.get(i); } } System.out.println(maxKey); }
30th Apr 2021, 10:39 PM
Miyar Karthik Kamath
Miyar Karthik Kamath - avatar