Provide two classes, GameEntry and GameScores, to maintain a list of top scores
ID: 668714 • Letter: P
Question
Provide two classes, GameEntry and GameScores, to maintain a list of top scores according to the requirements below. Each game entry (GameEntry) consists of a player name, a score (1 to 1000), and a date (min/dd/yyyy). Include appropriate constructor(s), setters, and getters for class GameEntry. The game scores (GameScores) hold a list of up to top 10 scores by using an array of game entries (a current list might have fewer than 10 entries). A new game entry can be added if applicable (i.e., add to the list, replace an existing entry, or ignore for being too low). An existing game entry can be removed by specifying a ranking (e.g., specify rank 1 to remove highest score, specify rank 2 to remove second highest score, and so on; ignore invalid rank). A complete list of scores can be printed upon request (highest to lowest).Explanation / Answer
public class GameEntry
{
protected String name; // name of the person earning this score
protected int score; // the score value
/** Constructor to create a game entry */
public GameEntry(String name, int score)
{
this.name = name;
this.score = score;
}
/** Retrieves the name field */
public String getName()
{
return name;
}
/** Retrieves the score field */
public int getScore()
{
return score;
}
/** Returns a string representation of this entry */
public String toString()
{
return "(" + name + ", " + score + ")";
}
}
public class GameScores {
/**
* @param args
*/
public static void main(String[] args)
{
GameEntry entry;
Scores highScores = new Scores();
entry = new GameEntry("Anna", 600);
highScores.add(entry);
entry = new GameEntry("Paul", 720);
highScores.add(entry);
System.out.println("The Original High Scores");
System.out.println(highScores);
entry = new GameEntry("Jill", 1150);
highScores.add(entry);
System.out.println("Scores after adding Jill");
System.out.println(highScores);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.