Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write the Java code for performing add(e) and remove(e) methods for the Scoreboard class, as shown below, except this time, dont maintain the game entries

Write the Java code for performing add(e) and remove(e) methods for the Scoreboard class, as shown below, except this time, dont maintain the game entries in order. Assume that we still need to keep n entries stored in indices 0 to n-1. You should be able to implement the methods without using any loop, so that the number of steps they perform does not depend on n. Create a Driver class to test your code.

public class Scoreboard {

private int numEntries = 0; private GameEntry[] board;

public Scoreboard(int capacity) { board = new GameEntry[capacity];

} public void add(GameEntry e) { int newScore = e.getScore();

if(numEntries < board.length || newScore > board[numEntries -1].getScore()){

if (numEntries < board.length)

numEntries++ ;

int j= numEntries -1;

while( j> 0 && board[j-1].getScore() < newScore) {

board[j] = board[j-1];

j--;

}

board[j] = e;

}

}

public GameEntry remove(int i) throws IndexOutOfBoundsException {

if (i< 0 || i >= numEntries)

throw new IndexOutOfBoundsException ("Invalid index: " + i);

GameEntry temp =board[i];

for(int j=i ; j< numEntries-1; j++)

board[j] = board[j+1];

board[numEntries-1] = null;

numEntries --;

return temp;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Access Patterns Database Interactions In Object Oriented Applications

Authors: Clifton Nock

1st Edition

0321555627, 978-0321555625

More Books

Students also viewed these Databases questions