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...
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);...
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...
In java 8. Modify the averageTopSpeed() method (in the class FormulaOne) using only lambdas and streams and print it to the terminal via the driver class: import java.util.*; public class Racer implements Comparable { private final String name; private final int year; private final int topSpeed; public Racer(String name, int year, int topSpeed){ this.name = name; this.year = year; this.topSpeed = topSpeed; } public String toString(){ return name + "-" + year + ", Top...
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...
The method m() of class B overrides the m() method of class A, true or false? class A int i; public void maint i) { this.is } } class B extends A{ public void m(Strings) { 1 Select one: True False For the following code, which statement is correct? public class Test { public static void main(String[] args) { Object al = new AC: Object a2 = new Object(); System.out.println(al); System.out.println(a): } } class A intx @Override public String toString()...
Fill in the find method and numMale method public class ComparableDemo { public void init(Object arr[]) { arr[0] = new Employee("Abby", 3000, 1, null, 'f'); arr[1] = new Employee("John", 2000, 2, (Employee)arr[0], 'm'); arr[2] = new Employee("Tim", 2000, 2, (Employee)arr[0], 'm'); arr[3] = new Employee("Tony", 1000, 3, (Employee)arr[0], 'm'); } //this method finds and returns the Employee object in the array whose name equals to //the...
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 { ...