Objectives

PongGame V7.0

Game of Pong - V7.0

In this version of the game, we add methods that calculate the highest and lowest score in the player's score array as well as calculating the average score over all the scores.

Open your latest version of PongGame Version 6. If you are not confident that it is working, you can use this solution here.

Save this as PongGameV7_0. Now, make changes to this work.

Player Class

We will now add three methods to the Player class:

  • lowestScore()

  • highestScore()

  • averageScore()

  //method returns the highest score stored in the scores array   
  //if there is only one element in the scores array, this value is 
  //returned as the highest
  public int highestScore()
  {
     int highestScore = scores[0];
     for(int i = 1; i < count; i++){
         if (scores[i] > highestScore){
            highestScore = scores[i];
         }        
     }   
     return highestScore;
  }

  //method returns the lowest score stored in the scores array   
  //if there is only one element in the scores array, this value 
  //is returned as the lowest
  public int lowestScore()
  {
     int lowestScore = scores[0] ;
     for(int i = 1; i < count; i++){
         if (scores[i] < lowestScore){
            lowestScore = scores[i];
         }        
     }   
     return lowestScore;
  }

  //method calculates and returns the average of all the high 
  //scores stored in the scores array   
  public int averageScore()
  {
     int total = 0;
     for(int i = 0; i < count; i++){
        total = total +  scores[i];        
     }   
     return total / count;
  }

Ensure that you understand the code you have written in. For example:

  • What is the function of the variable highestScore in the highestScore() method?

  • Why is the variable highestScore local?

  • Is this appropriate?

Changes in the PongGame class

We don't make any changes to how the game works in this version update. We only give out more information about what happened during the game.

So we simply change the tournamentOver() method to report on the three new values that our new methods calculate. So our updated tournamentOver() method will now be:

//method displays the player information, high scores and 
//statistics, before exiting the program.
void tournamentOver()
{
   println("Game Over!\n");
   println(player.getPlayerName() + ", your tournament is over!\n"
                                  + "Number of games played: " + numberOfGamesPlayed
                                  + "\n\n"                     + player.toString()
                                  + "\n\nHighest Score: "      + player.highestScore()
                                  +   "\nLowest Score:  "      + player.lowestScore()
                                  +   "\nAverage Score: "      + player.averageScore());
   exit();          
}

You should be able to now play the game and get the extra information reported at the end of the tournament. The information will be sent to the console. Next we will redirect this information to the dialog boxes via JOptionPane.

Save your work.

Make sure you understand the code before moving onto PongGameV8_0.

Solution

If your code is not working, the solution can be found here.