0% found this document useful (0 votes)
2 views

HighScores

Uploaded by

dumydore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

HighScores

Uploaded by

dumydore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1

AP-Style FRQ - High Scores


A database is used to record the highest scores of players of a game. Each player is represented by an
object of the following Player class. Each Player object contains a String value playerName and
an int value highScore which represents the players highest score on the game to date.

public class Player


{
private String playerName;
private int highScore;

public Player (String name, int score)


{
playerName = name;
highScore = score;
}

/** @param score an int value representing the player’s most recent score
* updates the value of highScore to score if this is higher than the current value
*/
public void updateScore(int score)
{
if(score > highScore)
highScore = score;
}

/** @return the player’s name


*/
public String getName()
{ return playerName; }

/** @return the high score of the player


*/
public int getHighScore()
{ return highScore; }
}

FRQ: Array/ArrayList AP Computer Science A


2

The following class ScoreList is used to create the database of players who have played the game. The
players are sorted into order according to their highScore value from greatest to least.

public class ScoreList


{
private ArrayList<Player> scoreboard;
// listed in decreasing order of highScore value
// no two Player objects have the same playerName

/** Moves the Player object at index pos upwards in scoreboard to the correct
* position in the list according to its highScore value
* @param pos a valid index for the scoreboard list
*/
public void moveUp(int pos)
{ /* to be implemented in part (a) */ }

/** Searches for a Player in scoreboard with a matching name and updates the high
* score if there is one. Otherwise a new player is created with the name and high score
* passed at the end of scoreboard. The new/updated Player object is moved up to
* the correct place on the list using the method moveUp.
* @param name the name of the player who recorded the score
* @param score the score recorded
* @return true if a new Player was created; false otherwise
*/
public boolean newScore(String name, int score)
{ /* to be implemented in part (b) */ }

// There may be instance variables, constructors, and methods that are not shown.
}

FRQ: Array/ArrayList AP Computer Science A


3

(a) Write the scoreList method moveUp that compares the highScore of the Player object at the
index pos to each Player in the list above it. This object will be moved to the correct position in the list
(you may assume that all items with an index smaller than pos in the list are in the correct order). If the
Player initially at pos has a score which is equal to one or more other players in the list it should be
placed after the other players on the list (i.e at a higher index).

Complete method moveUp below.

/** Moves the Player object at index pos upwards in scoreboard to the correct
* position in the list according to its highScore value
* @param pos a valid index for the scoreboard list
*/
public void moveUp(int pos)

FRQ: Array/ArrayList AP Computer Science A


4

(b) Write the scoreList method newScore that updates the highScore of a player if a player with
the name passed to the method already exists, and creates a player with the specified name and score
if there is no such player already. The method then moves the player that was added/had a score
updated to the correct position.

In writing newScore, assume that moveUp works as specified regardless of what you wrote in part (a).

Complete method newScore below.

/** Searches for a Player in scoreboard with a matching name and updates the high
* score if there is one. Otherwise a new player is created with the name and high score
* passed at the end of scoreboard. The new/updated Player object is moved up to
* the correct place on the list using the method moveUp.
* @param name the name of the player who recorded the score
* @param score the score recorded
* @return true if a new Player was created; false otherwise
*/
public boolean newScore(String name, int score)

FRQ: Array/ArrayList AP Computer Science A

You might also like