Question: Write a class that maintains the top ten scores for a game application, implementing the add and remove methods of Section 3.1.1, but using a
Write a class that maintains the top ten scores for a game application, implementing the add and remove methods of Section 3.1.1, but using a singly linked list instead of an array. For simplicity you can use the SinglyLinkedList with concatenate method. Please USE THE CODE PROVIDED BELOW. THANK YOU.
PLEASE PLEASE USE THE CODE BELOW.
/** Class for storing high scores in an array in nondecreasing order. */ public class ScoreboardSLL { private int numEntries = 0; // number of actual entries private int maxEntries = 10; private SinglyLinkedList
/** Attempt to add a new score to the collection (if it is high enough) */ public void add(GameEntry e) { int newScore = e.getScore(); SinglyLinkedList
// --------- type your code here --------------- } } } /** Remove and return the high score at index i. */ public GameEntry remove(int i) throws IndexOutOfBoundsException { if (i < 0 || i >= numEntries) throw new IndexOutOfBoundsException("Invalid index: " + i); // -------- type your code here using SinglyLinkedList instead of array ------------ return temp; // return the removed object } /** Returns a string representation of the high scores list. */ public String toString() { StringBuilder sb = new StringBuilder("["); /*for (int j = 0; j < numEntries; j++) { if (j > 0) sb.append(", "); // separate entries by commas sb.append(board[j]); }*/ sb.append(board.toString()); sb.append("]"); return sb.toString(); }
public static void main(String[] args) { // The main method ScoreboardSLL highscores = new ScoreboardSLL(5); String[] names = {"Rob", "Mike", "Rose", "Jill", "Jack", "Anna", "Paul", "Bob"}; int[] scores = {750, 1105, 590, 740, 510, 660, 720, 400};
for (int i=0; i < names.length; i++) { GameEntry gE = new GameEntry(names[i], scores[i]); System.out.println("Adding " + gE); highscores.add(gE); System.out.println(" Scoreboard: " + highscores); } System.out.println("Removing score at index " + 3); highscores.remove(3); System.out.println(highscores); System.out.println("Removing score at index " + 0); highscores.remove(0); System.out.println(highscores); System.out.println("Removing score at index " + 1); highscores.remove(1); System.out.println(highscores); System.out.println("Removing score at index " + 1); highscores.remove(1); System.out.println(highscores); System.out.println("Removing score at index " + 0); highscores.remove(0); System.out.println(highscores); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
