+ 1

Can anyone complete this solution?

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 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 Apr 2021, 3:30 PM
Rachita Bhasin
11 Antworten
+ 4
Where is your problem?
26th Apr 2021, 3:56 PM
Aditya
Aditya - avatar
+ 3
Okk Sorry but we don't provide ready made solution. Try to analyse and complete the code by yourself and if you are facing any issues feel free to ask.
26th Apr 2021, 4:00 PM
Aditya
Aditya - avatar
+ 3
I have given you the approach.. try that
26th Apr 2021, 4:20 PM
Aditya
Aditya - avatar
+ 3
Rachita Bhasin don't post same questions repeatedly. Del these or this one and frame it accordingly with proper tags into a single question. Follow the guidelines https://www.sololearn.com/Discuss/2767018/?ref=app https://www.sololearn.com/Discuss/2767029/?ref=app https://www.sololearn.com/Discuss/2767013/?ref=app
26th Apr 2021, 4:25 PM
Aditya
Aditya - avatar
+ 3
Well I have completed the code and the course by using the approach I mentioned above. And I tried my best to explain you.. anyways hard luck.
26th Apr 2021, 4:48 PM
Aditya
Aditya - avatar
+ 2
I don't think you should use iterators again cause in the given snippet itself they are using hashmap. So use that and find the max of no(int) and print it that's it! Rough code would be: String winner = ""; int mrk = 0; for(String nm:players.keySet()) { if(players.get(nm)>mrk) { winner = nm; mrk = players.get(nm); } }
26th Apr 2021, 4:11 PM
Aditya
Aditya - avatar
+ 1
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
26th Apr 2021, 3:57 PM
Rachita Bhasin
+ 1
Yes I tried it by iterator
26th Apr 2021, 4:04 PM
Rachita Bhasin
+ 1
Iterator<String,Integer> it = players.iterator(); String value = it.next(); System.out.println(value); } return(value);
26th Apr 2021, 4:05 PM
Rachita Bhasin
+ 1
Error : illegal start of type
26th Apr 2021, 4:06 PM
Rachita Bhasin
0
Thank you my bad:(
26th Apr 2021, 5:16 PM
Rachita Bhasin