Question: Write code in Java import java.util.*; public class Bowling { HashMap players; Bowling() { players = new HashMap (); } public void addPlayer(String name, int

Write code in Java

import java.util.*;

public class Bowling {

HashMap players;

Bowling() {

players = new HashMap();

}

public void addPlayer(String name, int p) {

players.put(name, p);

}

//your code goes here

public void getWinner(){

HashMap hm = new HashMap();

Map.Entry entryWithMaxValue = null;

for (Map.Entry currentEntry : hm.entrySet()) {

if (entryWithMaxValue == null || currentEntry.getValue().compareTo(entryWithMaxValue.getValue()) > 0) {

entryWithMaxValue = currentEntry;

}

}

System.out.println(entryWithMaxValue.getKey());

}

}

public class Program {

public static void main(String[ ] args) {

Bowling game = new Bowling();

Scanner sc = new Scanner(System.in);

for(int i=0;i

String input = sc.nextLine();

String[] values = input.split(" ");

String name = values[0];

int points = Integer.parseInt(values[1]);

game.addPlayer(name, points);

}

game.getWinner();

}

}

Write code in Java import java.util.*; public class Bowling { HashMap players;Bowling() { players = new HashMap(); } public void addPlayer(String name, int

vling Game Run Problem Bowling Game 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. import java.util.*; public class Bowling { HashMap players; Bowling({ players = new HashMap(); } public void addPlayer(String name, int p) { players.put(name, ); } //your code goes here public void getwinner() { HashMap hm = new HashMap(); Map.Entry entrywithMaxvalue = null; for (Map.Entry currentEntry : hm.entryset()) { if (entrywithMaxvalue == null || currentEntry.getValue().compareTo(entryWithMaxvalue.getValue()) > ) { entrywithMaxvalue = currentEntry; } System.out.println(entrywithMaxvalue.getKey(); Sample Input: Dave 42 Amy 103 Rob 64 } Sample Output: Amy You need to iterate through the HashMap to find the element with the maximum points and output its corresponding key. public class Program { public static void main(String[] args) { Bowling game = new Bowling; Scanner sc = new Scanner(System.in); for(int i=0;i players; Bowlingo { players = new HashMap(); public void addPlayer (String name, int p) { players.put(name, p); //your code goes here public void getwinner(){ HashMap hm = new HashMap(); Map.Entry entryWithMaxvalue = null; for (Map.Entry currentEntry : hm.entrySet()) { if (entrywithMaxvalue = null || currententry.getvalue().compare to (entryWithMaxvalue.getvalue()) > ) { entrywithMaxvalue = currentEntry; Your Output Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Map$Entry.getKey()" because *" is null at Bowling-getWinner(Bowling.java:20) at Program.main(Program.java:17) 13 14 15 16 17 18 19 28 21 22 23 System.out.println(entrywithmaxvalue.getKey(); Expected Output John } 25 X Test Case 2 27 28 29 38 31 input public class Program { public static void main(String[] args) { Bowling game = new Bowling(); Scanner sc = new Scanner(System.in); for(int i=0;i3;i++) { string input = sc.nextLine(); string[] values = input.split(" "); string name = values[@]; int points = Integer.parseInt(values[1]); game.addPlayer (name, points); } game.getwinner(); B 110 C77 Your Output Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Map$Entry.getKey()" because *" is null at Bowling.getWinner (Bowling.java:20) at Program.main(Program.java:17) Expected Output Test Case 3 This test case is hidden Test Case 4 This test case is hidden

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!