0
Please help me with its code
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
16 Réponses
+ 12
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());
}
}
+ 6
//you need void method like this
public void getWinner() {
String winner = "";
int score = 0;
for (Map.Entry<String, Integer> entry : players.entrySet()) {
if (entry.getValue() > score) {
score = entry.getValue();
winner = entry.getKey();
}
}
System.out.println(winner);
}
+ 3
Divyaraj Singh
Hii can you please put your attempt in a code so that there can be more chances of getting answers? Also if you are mentioning someone then you must use: for example -
@xyz
+ 1
Divyaraj Singh I think you have solved it and it works fine. Next time make sure to put it on the code playground and share the link. Anyways you are missing the addPlayer() and getWinner() methods.
Edit: I have not modified the code, this is just to help others answer your question.
https://code.sololearn.com/cslePKN06cWz/?ref=app
+ 1
Thank me later
...
import java.util.*;
public class Main {
public static void main(String[] args) {
System.out.println("Tom and Bob are playing a board game, " +
"in which both players start with the same number of points. " +
"Tom won the first game and got 1 point, while Bob lost the game, " +
"and therefore lost 1 point.");
/*You are given a program that is intended to take the initial score and
increase Tom's score by 1 and decrease Bob's score by 1.*/
//Task: Fix the program to result in the expected outputs
//Sample Input: 5
/*Sample Output:
Round 1 results:
6
4*/
/*Explanation
Both players had 5 points at the start of the game.
After the first game, Tom gained 1 point (6, the first outputted number),
and Bob lost 1 point (4, the second outputted number).*/
//taking initial score
System.out.println("Enter the round score:");
Scanner recordedScore = new Scanner(System.in);
int initScore = recordedScore.nextInt();
int scoreTom = initScore;
int scoreBob = initScore;
System.out.println("Round 1 results:");
System.out.println(++scoreTom);
System.out.println(--scoreBob);
}
}
0
Atleast share the code that is already provided to you. It's not very difficult but a good question to practice OOP's.
0
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Bowling {
public static <K, V extends Comparable<V>> Map.Entry<K, V> getMaxEntryInMapBasedOnValue(Map<K, V> map) {
Map.Entry<K, V> entryWithMaxValue = null;
for (Map.Entry<K, V> currentEntry : map.entrySet()) {
if (entryWithMaxValue == null || currentEntry.getValue().compareTo(entryWithMaxValue.getValue()) > 0) {
entryWithMaxValue = currentEntry;
}
}
return entryWithMaxValue;
}
public static void print(Map<String, Integer> map) {
System.out.print("Map: ");
if (map.isEmpty()) {
System.out.println("[]");
} else {
System.out.println(map);
}
}
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("Dave", 42);
map.put("Amy", 103);
map.put("Rob", 64);
print(map);
System.out.println("Entry with highest value: " + getMaxEntryInMapBasedOnValue(map));
}
}
0
Avinesh bro it's still not working
0
public void getWinner(){
String bestPlayer="";
int maxPoint=0;
Iterator<Map.Entry<String,Integer>> CurrentEntry =players.entrySet().iterator();
while(CurrentEntry.hasNext()){
String playerName=CurrentEntry.next().getKey();
Integer checkVal=players.get(playerName);
if (checkVal>=maxPoint){
maxPoint=checkVal;
bestPlayer=playerName;
}
}
System.out.println(bestPlayer);
}
0
- 1
First show your attempt here
- 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 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();
}
}
- 1
public String getWinner() {
String winner = "";
int h = 0;
for (Map.Entry<String, Integer> entry : players.entrySet()) {
if (entry.getValue() > h) {
h = entry.getValue();
winner = entry.getKey();
}
}
return winner;
}
- 1
//can some one help this one
public String getWinner(){
String winner="";
int h=0;
for(Map.Entry<String,Integer>entry:players.entrySet()){
if(entry.getValue()>h){
h=entry.getValue();
winner=entry.getKey();
}
}
return winner;
}
}
- 2
Please I need help. This is the sample code for this question
- 2
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Bowling {
public static <K, V extends Comparable<V>> Map.Entry<K, V> getMaxEntryInMapBasedOnValue(Map<K, V> map) {
Map.Entry<K, V> entryWithMaxValue = null;
for (Map.Entry<K, V> currentEntry : map.entrySet()) {
if (entryWithMaxValue == null || currentEntry.getValue().compareTo(entryWithMaxValue.getValue()) > 0) {
entryWithMaxValue = currentEntry;
}
}
return entryWithMaxValue;
}
public static void print(Map<String, Integer> map) {
System.out.print("Map: ");
if (map.isEmpty()) {
System.out.println("[]");
} else {
System.out.println(map);
}
}
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("Dave", 42);
map.put("Amy", 103);
map.put("Rob", 64);
print(map);
System.out.println("Entry with highest value: " + getMaxEntryInMapBasedOnValue(map));
}
}