Question
I need help with my code. In bold are the comments that tell you what to do. I am lost and need help on this
I need help with my code. In bold are the comments that tell you what to do. I am lost and need help on this question for data structures with java. Just need to add to the code.
/* * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser * * Developed for use with the book: * * Data Structures and Algorithms in Java, Sixth Edition * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser * John Wiley & Sons, 2014 * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see
/** 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 ---------------- while ( newScore < board.first().getScore()) { tempList.addLast(board.removeFirst()); } tempList.addLast(e); tempList.concatenate(board); } } }
/** 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 ------------
/* GameEntry temp = board[i]; // save the object to be removed for (int j = i; j < numEntries - 1; j++) // count up from i (not down) board[j] = board[j+1]; // move one cell to the left board[numEntries -1 ] = null; // null out the old last score numEntries--; */
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 Scoreboard_1 highscores = new Scoreboard_1(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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started