Question: Using Java, Create a class called SimpleSorterLASTNAME.java that defines objects which can sort arrays of a fixed size. This class should satisfy the following criteria:
Using Java, Create a class called SimpleSorterLASTNAME.java that defines objects which can sort arrays of a fixed size. This class should satisfy the following criteria:
- The constructor should take in an integer array and store it as an attribute called numArray
- The method descending() should create an array with the same entries as numArray in *descending* order (based on the add method in Scoreboard.java)
- The method ascending() should create an array with the same entries as numArray in *ascending* order (based on the add method in Scoreboard.java)
- The methods remove() and add() that removes and adds an element for each array in the attributes section.
You do not need to include a driver class. Code should be well documented, include a heading and comments marking each method. Try your best to use a minimal number of variables and be judicious about your commands. As much as you can, save both time and memory.
Below is the Scoreboard.java code and its add method (in bold) referenced above:
/** * Scoreboard.java * @description Manages a hypothetical high score board for some game */
public class Scoreboard{
//attributes private GameEntry board[]; private Integer numEntries; // number of entries present in board
//methods
/** * Constructs an empty score board with given capacity * @param capacity number of top players (e.g. "10") */ public Scoreboard(int capacity){ //capacity is the length of the array board = new GameEntry[capacity]; //created the array numEntries = 0; // board starts empty }
/** * Attempt to add a new score to collection (if high enough) * @param e GameEntry */
public void add(GameEntry e){ // get numerical score int newScore = e.getScore();
if( board.length > numEntries || 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;
}
}//end of method
/** * Remove and return high score at index i * @param i index of scoreboard (e.g., "7") * @throws index out of bounds of score board */ public GameEntry remove(int i) throws IndexOutOfBoundsException{ //note: it is possible to try to remove an entry not actually in the board
//code dealing with exception if(i < 0 || i >= numEntries) throw new IndexOutOfBoundsException("remove() in Scoreboard error; " + "Invalid index: " + i);
//if not exception // [entry0, entry1, entry2, entry3, entry4], i = 4 // [entry0, entry1, __, entry3, entry4] // [entry0, entry1, entry3, __, entry4] // [entry0, entry1, entry3, entry4, __] // return entry2
GameEntry entry2 = board[i]; //
for (int j = i; j < numEntries - 1; j++) board[j] = board[j+1]; // [entry0, entry1, entry3, entry3, entry4] j = 2 // [entry0, entry1, entry3, entry4, entry4] j = 3
board[numEntries - 1] = null; // [entry0, entry1, entry3, entry4, null] // null is like ___ numEntries --;
return entry2;
} // [entry0, entry1, entry2, entry3, entry4], i = 2 // paper = entry2 // [entry0, entry1, null, entry3, entry4] // [entry0, entry1, entry3, entry3, entry4] // [entry0, entry1, entry3, null, entry4] // [entry0, entry1, entry3, entry4, entry4] // [entry0, entry1, entry3, entry4, null]
/** * Returns string representation of board * @return board[] as string */ public String toString(){
String highScores = ""; // int place = 1; // // for(GameEntry entry : board){ // highScores += place + ": " + entry.toString() + " "; // place++; // } // // return highScores;
for(int i = 1; i <= numEntries; i++){ highScores += i + ": " + board[i-1].toString() + " "; }
return highScores;
}
}
Please clarify what exactly is not clear, I tried my best to clear it up more by removing unecessary comments and bolding the important part in the code provided for reference.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
