Write a toString method for the Scores class. It should print the Scores object as follows:
[(EntryName, EntryScore), (EntryName, EntryScore), (EntryName, EntryScore) , (EntryName, EntryScore),
(EntryName, EntryScore), (EntryName, EntryScore) ,(EntryName, EntryScore), (EntryName, EntryScore),
(EntryName, EntryScore) , (EntryName, EntryScore)]
GameEntry.java
public class GameEntry
{
protected String name; // name of the person earning this score
protected int score; // the score value
/** Constructor to create a game entry */
public GameEntry(String n, int s)
{
name = n;
score = s;
}
/** Retrieves the name field */
public String getName()
{
return name;
}
/** Retrieves the score field */
public int getScore()
{
return score;
}
/** Returns a string representation of this entry */
public String toString()
{
return "(" + name + ", " + score + ")";
}
}
Score.java
public class Scores
{
public static final int maxEntries = 10; // number of high scores we keep
private int numEntries; // number of actual entries
private GameEntry[] entries; // array of game entries (names & scores)
/** Default constructor */
public Scores()
{
entries = new GameEntry[maxEntries];
numEntries = 0;
}
/** Attempt to add a new score to the collection (if it is high enough) */
public void add(GameEntry e)
{
int newScore = e.getScore();
// is the new entry e really a high score?
if (numEntries == maxEntries) // the array is full
{
if (newScore <= entries[numEntries-1].getScore()) // the new entry, e, is not a high score in this case
{
return;
}
}
else // the array is not full
{
numEntries++;
}
// Locate the place that the new (high score) entry e belongs
int i;
for (i = numEntries-1 ; (i >= 1) && (newScore > entries[i-1].getScore()); i--)
{
System.out.println("Moving element at position "+(i-1)+" ("+ entries[i-1] + ") to the right");
entries[i] = entries[i - 1]; // move entry i one to the right
}
// add the new score to entries
System.out.println("Moving new element into position "+i+" ("+ e + ")");
entries[i] = e;
}
/** 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);
}
GameEntry temp = entries[i]; // temporarily save the object to be removed
for (int j = i; j < numEntries-1; j++) // count up from i (not down)
{
entries[j] = entries[j+1]; // move one cell to the left
}
entries[numEntries -1 ] = null; // null out the old last score
numEntries--;
return temp; // return the removed object
}
}
public class GameEntry
{
protected String name; // name of the person earning this score
protected int score; // the score value
/** Constructor to create a game entry */
public GameEntry(String n, int s)
{
name = n;
score = s;
}
/** Retrieves the name field */
public String getName()
{
return name;
}
/** Retrieves the score field */
public int getScore()
{
return score;
}
/** Returns a string representation of this entry */
public String toString()
{
return "(" + name + ", " + score + ")";
}
}
public class Scores
{
public static final int maxEntries = 10; // number of high scores we keep
private int numEntries; // number of actual entries
private GameEntry[] entries; // array of game entries (names & scores)
/** Default constructor */
public Scores()
{
entries = new GameEntry[maxEntries];
numEntries = 0;
}
/** Attempt to add a new score to the collection (if it is high enough) */
public void add(GameEntry e)
{
int newScore = e.getScore();
// is the new entry e really a high score?
if (numEntries == maxEntries) // the array is full
{
if (newScore <= entries[numEntries - 1].getScore()) // the new
// entry, e, is
// not a high
// score in this
// case
{
return;
}
}
else // the array is not full
{
numEntries++;
}
// Locate the place that the new (high score) entry e belongs
int i;
for (i = numEntries - 1; (i >= 1) && (newScore > entries[i - 1].getScore()); i--)
{
System.out.println("Moving element at position " + (i - 1) + " (" + entries[i - 1] + ") to the right");
entries[i] = entries[i - 1]; // move entry i one to the right
}
// add the new score to entries
System.out.println("Moving new element into position " + i + " (" + e + ")");
entries[i] = e;
}
/** 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);
}
GameEntry temp = entries[i]; // temporarily save the object to be
// removed
for (int j = i; j < numEntries - 1; j++) // count up from i (not down)
{
entries[j] = entries[j + 1]; // move one cell to the left
}
entries[numEntries - 1] = null; // null out the old last score
numEntries--;
return temp; // return the removed object
}
public String toString(){
StringBuffer sb = new StringBuffer("[");
for(int i=0;i<numEntries;i++){
sb.append(entries[i].toString()+",");
}
if(sb.length()==1)
return "[]";
sb.deleteCharAt(sb.length()-1);
sb.append("]");
return new String(sb);
}
}
Notes: used stringbuffer to append the string , and after coming out of the loop we need to remove the after coming out of the loop and finally converting stringbuffer to string and returning
Note: Please comment below if you face any issue regarding the solution, I am ready to help you
Write a toString method for the Scores class. It should print the Scores object as follows:...
Override toString() for the class HighScores. It should return a String that contains all of the high scores in entries, formatted nicely. Note that toString is already implemented for the Score class. Then, write a driver program (in a file named Main.java) that thoroughly tests HighScores (i.e. add and remove scores from the list, add scores to an already full list, make sure the list remains sorted, etc). Score.java public class Score { protected String name; // name of the...
Can you help with the merge method? This method should create and return an ArrayBagcontaining one occurrence of any item that is found in either the called object or the parameter other. For full credit, the resulting bag should not include any duplicates. Give the new ArrayBag a maximum size that is the sum of the two bag’s maximum sizes. import java.util.*; /** * An implementation of a bag data structure using an array. */ public class ArrayBag { /**...
Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the first item containing a given value from a doubly linked list. The header of the method is as follows: public boolean removeValue(T aValue) where T is the general type of the objects in the list and the methods returns true if such an item is found and deleted. Include testing of the method in a main method of the DoublyLList class. ------------------------------------------------------------------------------------- /** A...
cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /** * default constructor -- * Creates an matrix that has 2 rows and 2 columns */ public Matrix(int [][] m) { boolean valid = true; for(int r = 1; r < m.length && valid; r++) { if(m[r].length != m[0].length) valid = false; } if(valid) matrix = m; else matrix = null; } public String toString() { String output = "[ "; for (int i...
I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student { private String firstName; private String lastName; private String id; private boolean tuitionPaid; public Student(String firstName, String lastName, String id, boolean tuitionPaid) { this.firstName=firstName; this.lastName=lastName; this.id=id; this.tuitionPaid=tuitionPaid; } ...
I have to create two classes one class that accepts an object, stores the object in an array. I have another that has a constructor that creates an object. Here is my first class named "ShoppingList": import java.util.*; public class ShoppingList { private ShoppingItem [] list; private int amtItems = 0; public ShoppingList() { list=new ShoppingItem[8]; } public void add(ShoppingItem item) { if(amtItems<8) { list[amtItems] = ShoppingItem(item);...
Java.
Must not use Java API java.util.Stack
/**
A class of stacks whose entries are stored in an array.
Implement all methods in ArrayStack class using resizable
array strategy, i.e. usedoubleArray()
Must throw StackException during exception events in methods:
peek(), pop(), ArrayStack(int initialCapacity)
Do not change or add data fields
Do not add new methods
*/
import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private static...
Java programming question: Here is the feedback I received "sort fails. Use Arrays class method to sort." The actual question: Write a program as follows: Declare a five element array of Strings. Use a for loop and keyboard input to populate the array with the names of five friends. Sort the array in alphabetical order. Use a foreach loop to print the sorted array of friends, all on one line. Sample Output (inputs in italics) Enter the names of five...
please answer correctly
public class Eve public void a print("Eve a ": public void bo print("Eveb "; public String toString() { return "Eve ts": public class Sam extends Eve public void bo a(): print("Sanb ": public String toString() { return "Sants": public class Lucas extends Sam public void a() { print("Lucas a ": print (toString() + " "); public String toString() { String sup = super.toString): return sup""+ sup: public class Josh extends Lucas public void b) { print("Josh b...
Can anyone helps to create a Test.java for the following classes please? Where the Test.java will have a Scanner roster = new Scanner(new FileReader(“roster.txt”); will be needed in this main method to read the roster.txt. public interface List { public int size(); public boolean isEmpty(); public Object get(int i) throws OutOfRangeException; public void set(int i, Object e) throws OutOfRangeException; public void add(int i, Object e) throws OutOfRangeException; public Object remove(int i) throws OutOfRangeException; } public class ArrayList implements List { ...