-1
UML:
-name: String
-playerId: int
-scores: int[]
-bestScore: int
-numberOfPlayers: int
<< constructors>>
+Player()
+Player(name: String)
<< set methods>>
+setName(name: String):void
+setScores(score: int[]):void
<< get methods >>
+getPlayerId():int
+getName():String
+getScores():int[]
+getBestScore(): int
+calculateAverage(): double
<< helper methods >>
-calculateBestScore(): void
  • The constructors will increment the static variable numberOfPlayers and then assign the value of numberOfPlayers to the playerId.
  • The calculateAverage() public method determines the average score for a player from the data stored in the scores array.
  • The getBestScore() method calls the calculateBestScore() private helper method to determines the highest score achieved so far by a player.

Any ideas how should i set the numberOfPlayers assign value to playerId ? And how to I do the calculateAverage and calculateBestScore methods ?

My code at the moment is:

    //Class declaration of Player class
    public class Player
    {
        /*--------------- Data Fields --------------------------------------
        Attributes of the class
        */
        private String name;
        private int playerId;
        private int bestScore;
        private int numberOfPlayers;
        private int scores;
        /* -------------- CONSTRUCTOR --------------------------------------
        */
        public Player(String name)
        {
            this.name = name;
            this.numberOfPlayers = numberOfPlayers + 1;
            this.playerId = this.numberOfPlayers;
        }
        //Create set method for setName
        public void setName(String name)
        {
            this.name = name;
        }
        //Create set method for setScores
        public void setScore(int score)
        {
            scores = score;
        }
        //Create get method for getPlayerId
        public int getPlayerId()
        {
            return playerId;
        }

        //Create get method for getName
        public String getName()
        {
            return name;
        }
        //Create get method for getScores
        public int getScores()
        {
            return scores;
        }
        //Create get method for getBestScore
        public int getBestScore()
        {
        return bestScore;
        }
        //Create get method for calcualteAverage
        public double calculateAverage()
        {
        }
        }
fdglefevre
  • 672
  • 4
  • 15
  • 2
    Please choose a better title: imagine someone is solving similar problem. The title should let him know if this question helps him or not. – Jan Turoň Apr 01 '15 at 21:04
  • Where is the scores array? And calculating the average is very simple and straight forward same with the best score. – Sybren Apr 01 '15 at 21:07
  • Yes this was my first post so did not have any idea how to post the question. I will make the title more specific next time. Thanks – Anthony Galpo Apr 03 '15 at 13:52

1 Answers1

0

The constructors will increment the static variable numberOfPlayers and then assign the value of numberOfPlayers to the playerId

Give your variable the static identifier, and also initialize it:

private static int numberOfPlayers = 0;

Every time you create a new Player class, increment it by 1:

public Person() {
    numberOfPlayers++;
}

You'll need a method to expose the value of numberOfPlayers, and make it static so any Player class can call it:

public static int getNumPlayers() {
    return numberOfPlayers;
}

Test it's a valid approach as such:

Person a = new Person();
Person b = new Person();
Person c = new Person();

System.out.println(c.getNumPlayers()); //will be 3 based on the above

Regarding calculation of scores, your getScores and setScore methods will be problematic. First, getScores implies you must return more than one score. Second, setScore has me wondering how many times you must do this for one player, and where will you store the values as they grow in a collection of scores? I feel it will be too ambitious for me to guess at the different approaches that may satisfy your need. If this is a school assignment, check with your instructor. I'll share this one approach that uses the ArrayList data structure:

private ArrayList<Integer> scores = new ArrayList<Integer>();

public Person() {

}

public void setScore(int score) {
    scores.add(score);
}

To get the best score - how to get maximum value from the List/ArrayList

To get the average score - Calculating average of an array list?

Community
  • 1
  • 1
ThisClark
  • 14,352
  • 10
  • 69
  • 100
  • Thanks for that @ThisClark. The second part was to create an executable application that will ask the user to guess a number between 1-20. Each player is asked to play the game five times, so five scores are stored for each player. I will try and do this myself from now on I was really just stuck on that numberOfPlayers part. Once again big thanks – Anthony Galpo Apr 03 '15 at 14:04
  • Any ideas what my get Method should be like for getScores. I have set scores into private ArrayList scores = new ArrayList(); and setScore as you mentioned above but I am getting errors on getScore(). public int getScores() { return scores; } – Anthony Galpo Apr 03 '15 at 16:44
  • @AnthonyGalpo Your method is defined as `public int getScores()` but my sample declared the scores variable as `private ArrayList scores` therefore you need to redefine your method as `public ArrayList getScores()` then it should work. In this case, the return type `ArrayList` of your method simply needs to match the thing you are returning. – ThisClark Apr 04 '15 at 02:04
  • Thank you so much I figured it out last night myself. – Anthony Galpo Apr 04 '15 at 07:57