Below is a copy of the files needed to start: and lastly the text file info Desc
ID: 3742951 • Letter: B
Question
Below is a copy of the files needed to start:
and lastly the text file info
Description: In this assignment, you will get yourself familiar with array of objects and define new methods on the ScoreBoard class to simulate the ScoreBoard of a game. You are given a tester file called player.txt which contains player information. ScoreBoard.java and GameEntry.java, and the template of the tester code are also given. You are asked to Implement the following algorithms on the ScoreBoard class: double average() /* calculate the average of the scores/ int search(String name)/return the void printBoard)/ print the name and score of each user on a new line/ void sortByName()extra credit */ Implement your tester code which 1. index of the GameEntry with the name */ 2 read player information from a file and add them into the Scoreboard, print the Scoreboard print the average score find user "simon" remove him from the board print the Scoreboard 1. 2. 3. 4. 6.Explanation / Answer
package ClassProg;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
// Class Tester definition
public class Tester
{
// main method definition
public static void main(String [] args) throws FileNotFoundException
{
// Scoreboard class object declared with size 50 using parameterized constructor
Scoreboard gameBoard = new Scoreboard(50);
// File object created with file named "player.txt".
File file = new File("player.txt");
// Scanner class object created to read the file contents.
Scanner readFile = new Scanner(file);
// Loops till end of the file.
while(readFile.hasNext())
{
/**
* P1: Add players to scoreboard from file
*/
gameBoard.add(new GameEntry(readFile.next(), readFile.nextInt()));
}// End of while loop
/**
* P2: Print score board
*/
gameBoard.printBoard();
/**
* P3: Print average
*/
System.out.print(" Average: " + gameBoard.average());
/**
* P4: Find simon index
*/
/** Call the function to find name "simon" and stores the found index position
* Stores -1 if not found
*/
int index = gameBoard.find("Simon");
// Checks if the return index position is -1 display not found message.
if(index == -1)
System.out.print(" Not found. ");
// Otherwise display found index position.
else
System.out.print(" Found at index position: " + index + " ");
/**
* P5: Remove simon
*/
/** Checks if the returned index position is not -1
* Then call the method to remove the record
*/
if(index != -1)
System.out.println(" Removed Data: " + gameBoard.remove(index));
/**
* P6: Print score board
*/
gameBoard.printBoard();
/**
* P2: Sort by name
*/
gameBoard.sortByName();
System.out.println(" After sorting by name ");
/** Calls the method to print score board.*/
gameBoard.printBoard();
}// End of main method
}// End of class
// Class GameEntry definition
class GameEntry
{
// Instance variable to store data
private String name;
private int score;
/** Parameterized constructor to assign parameter data to instance variable. */
public GameEntry(String n, int s)
{
name = n;
score = s;
}// End of parameterized constructor
/** Returns the name field. */
public String getName()
{
return name;
}// End of method
/** Returns the score field. */
public int getScore()
{
return score;
}// End of method
/** Returns a toString of the name and score. */
public String toString()
{
return "(" + name + ", " + score + ")";
}// End of method
}// End of class
// Class Scoreboard definition
class Scoreboard
{
private int numEntries = 0; // Number of actual entries
// Declares an array of object of class GameEntry
private GameEntry[] board;
private int cap;
// Array of game entries (name and scores)
/** Constructs an empty scoreboard with given capacity for storing entries. */
public Scoreboard(int capacity)
{
cap = capacity;
board = new GameEntry[capacity];
}// End of parameterized constructor
/** Method to add a game entry to board */
public void add(GameEntry e)
{
int newScore = e.getScore();
// Is the new entry e really a high score?
if(numEntries < board.length || newScore > board[numEntries - 1].getScore())
{
if(numEntries < board.length) // No score drops from the board
numEntries++; // So overal number increases
// Shift any lower scores rightward to make room for the new entry
int j = numEntries - 1;
while(j > 0 && board[j - 1].getScore() < newScore)
{
board[j] = board[j - 1]; // Shift entry from j - 1 to j
j--; // and decrement j
}// End of while loop
board[j] = e; // When done, add new entry
}// End of if condition
}// End of method
/** Remove and return the hight score at index i.*/
public GameEntry remove(int i) throws IndexOutOfBoundsException
{
if(i < 0 || i >= numEntries)
throw new IndexOutOfBoundsException("Invalid index: " + i);
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; // Returns the removed object
}// End of method
/** Method to return average of scores.*/
public double average()
{
// To store total
double total = 0;
// Loops till number of entries
for(int j = 0; j < numEntries; j++)
// Calculates total
total += board[j].getScore();
// Calculates and returns average
return (total / board.length);
}// End of method
/** Method to print the board.*/
public void printBoard()
{
// Loops till number of entries
for(int j = 0; j < numEntries; j++)
// Displays the board
System.out.println(board[j]);
}// End of method
/** Method to sort the names in ascending order.*/
public void sortByName()
{
// Declares a temporary object of class GameEntry to swap
GameEntry temp;
// Loops till number of entries
for(int i = 0; i < numEntries; i++)
{
// Loops till number of entries minus outer loop variable i and minus one
// Outer loop variable is subtracted because after completion of each inner loop
// One value is sorted
for(int j = 0; j < numEntries - i - 1; j++)
{
// Checks if the j index position name is greater than the next index position of j name
if(board[j].getName().compareTo(board[j + 1].getName()) > 0)
{
// Swapping process
temp = board[j];
board[j] = board[j + 1];
board[j + 1] = temp;
}// End of if condition
}// End of inner for loop
}// End of outer for loop
}// End of method0
/** Method to search a name.
* Returns found index position
* Returns -1 if not found,
*/
public int find(String name)
{
// Initializes found index position to -1 for not found
int index = -1;
// Loops till number of entries
for(int j = 0; j < numEntries; j++)
{
// Checks if the j index position name is equals to the name passed as parameter
if(board[j].getName().compareToIgnoreCase(name) == 0)
{
// Returns the found index position as j
return j;
}// End of if condition
}// End of for loop
// Returns index position as not found
return index;
}// End of method
}// End of class
Sample Output:
(Jayson, 149)
(Vicenta, 148)
(Sona, 144)
(Manda, 140)
(Olin, 137)
(Vanna, 135)
(Garnet, 130)
(Joe, 130)
(Shellie, 129)
(Irish, 128)
(Mariko, 126)
(Leonie, 123)
(Cathey, 123)
(Zonia, 111)
(Nieves, 104)
(Rhoda, 103)
(Jene, 101)
(Belia, 96)
(Nichelle, 94)
(Doyle, 86)
(Art, 85)
(Rikki, 83)
(Jeffie, 80)
(Gwyneth, 80)
(Normand, 79)
(Ranae, 78)
(Meryl, 76)
(Jonell, 75)
(Stevie, 71)
(Georgia, 71)
(Gilda, 70)
(Verlene, 67)
(Fernado, 67)
(Simon, 65)
(Galen, 64)
(Keri, 61)
(Corey, 61)
(Oma, 59)
(Signe, 57)
(Latina, 45)
(Gidget, 43)
(Mistie, 26)
(Arnette, 22)
(Treasa, 18)
(Scotty, 13)
(Annmarie, 9)
(Lorita, 9)
(Rosie, 9)
(Josephina, 7)
(Lue, 4)
Average: 79.82
Found at index position: 33
Removed Data: (Simon, 65)
(Jayson, 149)
(Vicenta, 148)
(Sona, 144)
(Manda, 140)
(Olin, 137)
(Vanna, 135)
(Garnet, 130)
(Joe, 130)
(Shellie, 129)
(Irish, 128)
(Mariko, 126)
(Leonie, 123)
(Cathey, 123)
(Zonia, 111)
(Nieves, 104)
(Rhoda, 103)
(Jene, 101)
(Belia, 96)
(Nichelle, 94)
(Doyle, 86)
(Art, 85)
(Rikki, 83)
(Jeffie, 80)
(Gwyneth, 80)
(Normand, 79)
(Ranae, 78)
(Meryl, 76)
(Jonell, 75)
(Stevie, 71)
(Georgia, 71)
(Gilda, 70)
(Verlene, 67)
(Fernado, 67)
(Galen, 64)
(Keri, 61)
(Corey, 61)
(Oma, 59)
(Signe, 57)
(Latina, 45)
(Gidget, 43)
(Mistie, 26)
(Arnette, 22)
(Treasa, 18)
(Scotty, 13)
(Annmarie, 9)
(Lorita, 9)
(Rosie, 9)
(Josephina, 7)
(Lue, 4)
After sorting by name
(Annmarie, 9)
(Arnette, 22)
(Art, 85)
(Belia, 96)
(Cathey, 123)
(Corey, 61)
(Doyle, 86)
(Fernado, 67)
(Galen, 64)
(Garnet, 130)
(Georgia, 71)
(Gidget, 43)
(Gilda, 70)
(Gwyneth, 80)
(Irish, 128)
(Jayson, 149)
(Jeffie, 80)
(Jene, 101)
(Joe, 130)
(Jonell, 75)
(Josephina, 7)
(Keri, 61)
(Latina, 45)
(Leonie, 123)
(Lorita, 9)
(Lue, 4)
(Manda, 140)
(Mariko, 126)
(Meryl, 76)
(Mistie, 26)
(Nichelle, 94)
(Nieves, 104)
(Normand, 79)
(Olin, 137)
(Oma, 59)
(Ranae, 78)
(Rhoda, 103)
(Rikki, 83)
(Rosie, 9)
(Scotty, 13)
(Shellie, 129)
(Signe, 57)
(Sona, 144)
(Stevie, 71)
(Treasa, 18)
(Vanna, 135)
(Verlene, 67)
(Vicenta, 148)
(Zonia, 111)
player.txt file contents
Nieves 104
Vanna 135
Josephina 7
Mistie 26
Jonell 75
Meryl 76
Stevie 71
Leonie 123
Manda 140
Rhoda 103
Annmarie 9
Jayson 149
Lorita 9
Ranae 78
Treasa 18
Irish 128
Simon 65
Jene 101
Gidget 43
Latina 45
Vicenta 148
Art 85
Sona 144
Garnet 130
Belia 96
Nichelle 94
Olin 137
Verlene 67
Signe 57
Rosie 9
Zonia 111
Keri 61
Doyle 86
Gilda 70
Georgia 71
Arnette 22
Galen 64
Oma 59
Fernado 67
Joe 130
Mariko 126
Cathey 123
Scotty 13
Jeffie 80
Corey 61
Normand 79
Gwyneth 80
Rikki 83
Shellie 129
Lue 4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.