Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We are now going to write a class to represent a Scoreboard that keeps track of

ID: 649840 • Letter: W

Question

We are now going to write a class to represent a Scoreboard that keeps track of the score for both users in your Joust Game. Please implement the following API in your class:

+ Scoreboard()
+ Scoreboard(highScoresCapacity : int)
+ incrementScore1() : void
+ incrementScore2() : void
+ getScore1() : int
+ getScore2() : int
+ isGameOver() : boolean
+ endGame() : void
+ getHighScores() : ArrayList<Integer>
+ addHighScore(int newScore) : void
+ loadHighScores(filename : String) : void
+ saveHighScores(filename : String) : void
+ toString() : String

(- = private, + = public)

Constructors

Both constructors should set score1 and score2 to 0, gameOver to false, and highScores to a new (empty) ArrayList<Integer>. The null (default) constructor should set highScoresCapacity to 3, and the specific constructor should set highScoresCapacity using the given parameter.

endGame

This method should set gameOver to true. You will use this in place of a setter.

addHighScore(int newScore)

This method should add newScore to highScores, in the appropriate location so that highScores remains sorted in decreasing order of scores. If there are now more than highScoresCapacity elements in highScores, remove the last element.

You might find some of the Collections class for sorting the list (note we want it in the reverse of Java

Scoreboard ? score1 : int
? score2 : int
? gameOver : boolean
? highScores : ArrayList<Integer>
? highScoresCapacity : int

+ Scoreboard()
+ Scoreboard(highScoresCapacity : int)
+ incrementScore1() : void
+ incrementScore2() : void
+ getScore1() : int
+ getScore2() : int
+ isGameOver() : boolean
+ endGame() : void
+ getHighScores() : ArrayList<Integer>
+ addHighScore(int newScore) : void
+ loadHighScores(filename : String) : void
+ saveHighScores(filename : String) : void
+ toString() : String

(- = private, + = public)

Constructors

Both constructors should set score1 and score2 to 0, gameOver to false, and highScores to a new (empty) ArrayList<Integer>. The null (default) constructor should set highScoresCapacity to 3, and the specific constructor should set highScoresCapacity using the given parameter.

endGame

This method should set gameOver to true. You will use this in place of a setter.

addHighScore(int newScore)

This method should add newScore to highScores, in the appropriate location so that highScores remains sorted in decreasing order of scores. If there are now more than highScoresCapacity elements in highScores, remove the last element.

You might find some of the Collections class for sorting the list (note we want it in the reverse of Java

? score1 : int ? score2 : int ? gameOver : boolean ? highScores : ArrayList ? highScoresCapacity : int

Explanation / Answer


import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Scanner;


public class Scoreboard {
  
   int score1;
   int score2;
   boolean gameOver;
   ArrayList<Integer> highScores;
   int highScoresCapacity;
  
   public Scoreboard()
   {
       this.score1 = score2 = 0;
       this.gameOver = false;
       this.highScores = new ArrayList<>();
       this.highScoresCapacity = 3;
   }
  
   public Scoreboard(int highScoresCapacity)
   {
       this.score1 = score2 = 0;
       this.gameOver = false;
       this.highScores = new ArrayList<>();
       this.highScoresCapacity = highScoresCapacity;
   }
  
   public void incrementScore1()
   {
       score1++;
   }
  
   public void incrementScore2()
   {
       score2++;
   }
  
   public int getScore1()
   {
       return score1;
   }
   public int getScore2()
   {
       return score2;
   }
  
   public boolean isGameOver()
   {
       return gameOver;
   }
  
   public void endGame() {
       gameOver = true;
   }
  
   public void addHighScore(int newScore)
   {
       highScores.add(newScore);
      
       Collections.sort(highScores, Collections.reverseOrder());
      
       int size = highScores.size();
       if(size > highScoresCapacity)
       {
           highScores.remove(size-1);
       }
   }
  
   public void loadHighScores(String filename) throws FileNotFoundException
   {
       //open reader file
       Scanner inputFile = new Scanner(new FileReader(filename));
              
       while (inputFile.hasNext()) {
          
           String data = inputFile.next();
          
           for (String temp : data.split(",")) {
               int num = Integer.parseInt(temp);
               addHighScore(num);
           }
       }      
      
       //close file pointers
       inputFile.close();
   }
  
   public void saveHighScores(String filename) throws FileNotFoundException {
       PrintWriter outFile = new PrintWriter(filename);
      
       String toWrite = highScores.toString().replace("[", "").replace("]", "").replace(", ", ",");
       outFile.write(toWrite);
      
       outFile.close();
   }
  
   public String toString() {
       return (score1 + "-" + score2 + ". Game is " + (isGameOver() ? "" : "not ") + "over. High Scores: " + highScores.toString().replace("[", "").replace("]", "")
   .replace(", ", ","));
      
   }
  
   public ArrayList<Integer> getHighScores()
   {
       return highScores;
   }
  
  
  
   public static void main(String[] args) throws FileNotFoundException{
      
       //sample API data usage
       Scoreboard sb = new Scoreboard();
       System.out.println("Is 1st game Over: "+sb.isGameOver());
       System.out.println("Score 1: "+sb.getScore1());
       System.out.println("Score 2: "+sb.getScore2());
       for(int i=0; i<7; i++)
       {
           sb.incrementScore1();
       }
       System.out.println("Score 1: "+sb.getScore1());
       System.out.println("Score 2: "+sb.getScore2());
       sb.incrementScore2();
       sb.addHighScore(sb.getScore1());
       sb.addHighScore(sb.getScore2());
       sb.saveHighScores("highScores.txt");
      
      
       Scoreboard sb2 = new Scoreboard(1);
       System.out.println("Scoreboard 2: "+sb2.toString());
       sb2.loadHighScores("highScores.txt");
       System.out.println("HighScore: "+sb2.getHighScores());
       sb2.endGame();
       System.out.println("Is 1st Game Over: "+sb.isGameOver());
       System.out.println("Is 2nd Game Over: "+sb2.isGameOver());
      
      
       System.out.println(sb.toString());
       System.out.println(sb2.toString());
      
   }
  
  
}

---------------------------------------------------------------------------------------------------------------------------------------------------------

OUTPUT

Is 1st game Over: false
Score 1: 0
Score 2: 0
Score 1: 7
Score 2: 0
Scoreboard 2: 0-0. Game is not over. High Scores:
HighScore: [7]
Is 1st Game Over: false
Is 2nd Game Over: true
7-1. Game is not over. High Scores: 7,1
0-0. Game is over. High Scores: 7

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote