+ 5

You are creating a bowling game! The given code declares a Bowling class with its constructor and addPlayer() method. Each p

Bowls ng

22nd Dec 2020, 6:47 PM
david ukwen
david ukwen - avatar
18 Réponses
+ 26
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); } public void getWinner(){ String best=""; Iterator<Map.Entry<String,Integer>> it =players.entrySet().iterator(); int max=0; while(it.hasNext()){ String playerName=it.next().getKey(); Integer checkVal=players.get(playerName); if (checkVal>=max){ max=checkVal; best=playerName; } } System.out.println(best); } } public class Program { public static void main(String[ ] args) { Bowling game = new Bowling(); Scanner sc = new Scanner(System.in); for(int i=0;i<3;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(); } }
13th Feb 2021, 7:40 PM
Esteban L
Esteban L - avatar
+ 6
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 public void getWinner() { int max = Collections.max(players.values()); for (Map.Entry<String, Integer> entry : players.entrySet()) { if (entry.getValue().equals(max)) { System.out.println(entry.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<3;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(); } }
29th May 2021, 7:12 PM
Krisztian Hackl
Krisztian Hackl - avatar
+ 4
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); } public void getWinner(){ String best=""; Iterator<Map.Entry<String,Integer>> it =players.entrySet().iterator(); int max=0; while(it.hasNext()){ String playerName=it.next().getKey(); Integer checkVal=players.get(playerName); if (checkVal>=max){ max=checkVal; best=playerName; } } System.out.println(best); } } public class Program { public static void main(String[ ] args) { Bowling game = new Bowling(); Scanner sc = new Scanner(System.in); for(int i=0;i<3;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(); } }
2nd Oct 2021, 10:01 AM
Hariom Kumar
Hariom Kumar - avatar
+ 2
import java.util.*; //Please Subscribe to My Youtube Channel //Channel Name: Fazal Tuts4U class Bowling { HashMap<String, Integer> players; Bowling() { players = new HashMap<String, Integer>(); } public void addPlayer(String name, int p) { players.put(name, p); } public void getWinner() { int max = Collections.max(players.values()); for (Map.Entry<String, Integer> entry : players.entrySet()) { if (entry.getValue().equals(max)) { System.out.println(entry.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<3;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(); } }
5th Sep 2021, 8:27 AM
Fazal Haroon
Fazal Haroon - avatar
+ 2
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); } public void getWinner(){ String best=""; Iterator<Map.Entry<String,Integer>> it =players.entrySet().iterator(); int max=0; while(it.hasNext()){ String playerName=it.next().getKey(); Integer checkVal=players.get(playerName); if (checkVal>=max){ max=checkVal; best=playerName; } } System.out.println(best); } } public class Program { public static void main(String[ ] args) { Bowling game = new Bowling(); Scanner sc = new Scanner(System.in); for(int i=0;i<3;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(); } }
1st Jan 2022, 12:38 PM
M.I.M.Aqueel
M.I.M.Aqueel - avatar
+ 1
#trythis 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); } public void getWinner(){ String best=""; Iterator<Map.Entry<String,Integer>> it =players.entrySet().iterator(); int max=0; while(it.hasNext()){ String playerName=it.next().getKey(); Integer checkVal=players.get(playerName); if (checkVal>=max){ max=checkVal; best=playerName; } } System.out.println(best); } } public class Program { public static void main(String[ ] args) { Bowling game = new Bowling(); Scanner sc = new Scanner(System.in); for(int i=0;i<3;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(); } }
17th Dec 2021, 9:44 AM
Mirmahmud Ilyosov
Mirmahmud Ilyosov - avatar
+ 1
public class Bowling { HashMap<String, Integer> players; Bowling() { players = new HashMap<String, Integer>(); } public void addPlayer(String name, int p) { players.put(name, p); } void getWinner(){ int maxValue = Collections.max(players.values()); String[] name = new String[players.size()]; name = players.keySet().toArray(name); for (String v : name){ if (players.get(v)<maxValue){ players.remove(v); } } String output = players.keySet().toString().replaceAll("(^\\[|\\]$)", ""); System.out.println(output); } } public class Program { public static void main(String[ ] args) { Bowling game = new Bowling(); Scanner sc = new Scanner(System.in); for(int i=0;i<3;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(); } }
26th Jan 2022, 10:05 AM
Максим Малов
Максим Малов - avatar
0
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 public void getWinner(){ HashMap<String, Integer> hm = new HashMap<String, Integer>(); Map.Entry<String, Integer> entryWithMaxValue = null; for (Map.Entry<String, Integer> 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<3;i++) { String input = sc.nextLine(); S
22nd Dec 2020, 6:49 PM
david ukwen
david ukwen - avatar
0
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); } public void getWinner(){ String best=""; Iterator<Map.Entry<String,Integer>> it =players.entrySet().iterator(); int max=0; while(it.hasNext()){ String playerName=it.next().getKey(); Integer checkVal=players.get(playerName); if (checkVal>=max){ max=checkVal; best=playerName; } } System.out.println(best); } } public class Program { public static void main(String[ ] args) { Bowling game = new Bowling(); Scanner sc = new Scanner(System.in); for(int i=0;i<3;i++) { String input = sc.nextLine(); String[] values = input.split(" ");
1st Feb 2022, 6:50 PM
Avrian Shandy
Avrian Shandy - avatar
0
He
24th Apr 2022, 3:54 AM
Philip Acquah
Philip Acquah - avatar
0
import java.util.*; import java.util.HashMap; import java.util.Map.Entry; public class Bowling { HashMap<String, Integer> players; Bowling() { players = new HashMap<String, Integer>(); } public void addPlayer(String name, int p) { players.put(name, p); } public void getWinner() { int max=Collections.max(players.values()); for(Entry<String, Integer> entry: players.entrySet()) { if(entry.getValue() == max) { System.out.println(entry.getKey()); break; } } } }
9th Jul 2022, 11:29 AM
Mulukutla Parimala Sai Lakshmi
Mulukutla Parimala Sai Lakshmi - avatar
0
//public void getWinner method public void getWinner(){ String[] nameArr = new String[players.size()]; nameArr = players.keySet().toArray(nameArr); int score=0; String s=""; for (String emp : nameArr){ if(players.get(emp)>score){ score=players.get(emp); s=emp; } } System.out.println(s); }
31st Jan 2023, 3:15 AM
Sudesh Parate
Sudesh Parate - avatar
- 1
This is my solution so far but it's not correct it displays null pointer error
22nd Dec 2020, 6:50 PM
david ukwen
david ukwen - avatar
- 1
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 public void getWinner(){ String best=""; Iterator<Map.Entry<String,Integer>> it =players.entrySet().iterator(); int max=0; while(it.hasNext()){ String playerName=it.next().getKey(); Integer checkVal=players.get(playerName); if (checkVal>=max){ max=checkVal; best=playerName; } } System.out.println(best); } } public class Program { public static void main(String[ ] args) { Bowling game = new Bowling(); Scanner sc = new Scanner(System.in); for(int i=0;i<3;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(); } }
3rd Apr 2021, 5:16 PM
Anshul Soni
Anshul Soni - avatar
- 1
//your code goes here public void getWinner(){ HashMap<String, Integer> hm = new HashMap<String, Integer>(); Map.Entry<String, Integer> entryWithMaxValue = null; for (Map.Entry<String, Integer> 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<3;i++) { String input = sc.nextLine();
1st Jun 2021, 6:16 AM
WEKESA M. JOEL
WEKESA M. JOEL - avatar
- 2
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
22nd Dec 2020, 6:47 PM
david ukwen
david ukwen - avatar
- 3
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); } public String getWinner() { String winner = ""; int highestPoints = 0; for (Map.Entry<String, Integer> entry : players.entrySet()) { if (entry.getValue() > highestPoints) { highestPoints = entry.getValue(); winner = entry.getKey(); } } return winner; } } public class Program { public static void main(String[ ] args) { Bowling game = new Bowling(); Scanner sc = new Scanner(System.in); for(int i=0;i<3;i++) { String input = sc.nextLine(); String[] values = input.split(" "); String name = values[0]; int points = Integer.parseInt(values[1]); game.addPlayer(name, points); } System.out.println(game.getWinner()); } }
12th Jan 2021, 4:42 AM
Isuru Dananjaya Samarasekara
- 3
Esteban L's solution is correct.
10th Mar 2021, 9:27 AM
Panos