Calculate average score of an individual player
Calculate average score of an individual player
What I am trying to achieve here is to use unique names of players to accumulate the scores of all games by individual and then print the score
average by dividing the accumulated score by individual gameCounts. Can you help?
package bowling_score_tracker;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
public class Bowling_score_tracker
String PlayerName;
int gameCounts;
int score;
Date date;
Bowling_score_tracker(String PlayerName, int gameCounts, int score, Date date)
this.PlayerName=PlayerName;
this.gameCounts=gameCounts;
this.score=score;
this.date=date;
public static void main(String args) throws ParseException
Bowling_score_tracker s1=new Bowling_score_tracker("Brown",1,98,new java.util.Date(System.currentTimeMillis()));
Bowling_score_tracker s2=new Bowling_score_tracker("Tony",1,99,new java.util.Date(System.currentTimeMillis()));
Bowling_score_tracker s3=new Bowling_score_tracker("Anne",1,100,new java.util.Date(System.currentTimeMillis()));
Bowling_score_tracker s4=new Bowling_score_tracker("Tony",2,105,new java.util.Date(System.currentTimeMillis()));
Bowling_score_tracker s5=new Bowling_score_tracker("Tony",3,106,new java.util.Date(System.currentTimeMillis()));
ArrayList<Bowling_score_tracker> al=new ArrayList<Bowling_score_tracker>();
al.add(s1);
al.add(s2);
al.add(s3);
al.add(s4);
al.add(s5);
int sum = 0;
Iterator itr=al.iterator();
while(itr.hasNext())
I think I need to include the logic to sum all scores by individual and then devide by gameCounts but not sure how to construct here.
/*sum += itr.next();
average = sum / gameCounts*/
Bowling_score_tracker st=(Bowling_score_tracker)itr.next();
System.out.println(st.PlayerName+" "+st.gameCounts+" "+st.score+" "+st.date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date yourDate = sdf.parse("2018-08-29");
2 Answers
2
If you are using Java8, you can easily get this thing done like so,
Map<String, Double> playerToAvgScore = al.stream().collect(Collectors.groupingBy(
Bowling_score_tracker::getPlayerName,
Collectors.averagingInt(Bowling_score_tracker::getScore)));
First use the groupingBy
collector to group the players by their name. Each group contains all the games the player involved. Then compute the average score on each group using the downstream collector. And you are done !
groupingBy
First of all, you need to create getter and setter methods for all fields in class.
Then you need to create other class(outside of existing class) with score and game count field.
class AvgScore
int score1;
int gameCount1;
public AvgScore(int score1, int gameCount1)
super();
this.score1 = score1;
this.gameCount1 = gameCount1;
public int getScore1()
return score1;
public void setScore1(int score1)
this.score1 = score1;
public int getGameCount1()
return gameCount1;
public void setGameCount1(int gameCount1)
this.gameCount1 = gameCount1;
Then need to create a Map that store each player total score and game count(This step written in main after add all element in list).
//Create an Map to store Player name as key and
//its total score and game count as vale
Map<String, AvgScore> avgScore=new HashMap<>();
for(Bowling_score_tracker bl:al)
if(avgScore.containsKey(bl.getPlayerName()))
AvgScore sc=avgScore.get(bl.getPlayerName());
int scr=bl.getScore()+sc.getScore1();
int gc=bl.getGameCounts()+sc.getGameCount1();
avgScore.put(bl.getPlayerName(),new AvgScore(scr,gc));
else
avgScore.put(bl.getPlayerName(), new AvgScore(bl.getScore(), bl.getGameCounts()));
Following step store each player total score and game count.
Then need to calculate average of each player.
//Calculate Average socre.
for(Map.Entry<String, AvgScore> map:avgScore.entrySet())
AvgScore sc=map.getValue();
double avg=((double)sc.getScore1()/sc.getGameCount1());
System.out.println("Name: "+map.getKey()+" Avg.= "+avg);
This will calculate each player average score.
Hop it will help you.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
One way is to use a Map. Another is to create a class that represents one player, not each score, and have it hold a list of scores.
– Hovercraft Full Of Eels
Aug 30 at 2:35