Question

Override toString() for the class HighScores. It should return a String that contains all of the...

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 player earning this score
protected int score;
public Score(String n, int s)
{
name = n; score = s;
}
public String getName()
  {
  return name;
}
public int getScore()
{
  return score;
}
@Override public String toString()
{
  return "(" + name + ", " + score + ")";
}
}

HighScores.java

public class HighScores
{
    public static final int maxEntries = 10;
    protected int numEntries;
    protected Score[] entries;
    public HighScores()
    {
        entries = new Score[maxEntries];
        numEntries = 0;
    }
    public String toString()
    {
        //TODO
        return null;
    }
    // methods for updating the set of high scores go here
    public void add(Score s)
    {
        int newScore = s.getScore();
        if(numEntries == maxEntries)
        {
            if(newScore <= entries[numEntries - 1].getScore())
                return; // the new entry is not a high score
        }
        else
            numEntries++;
        int i = numEntries - 1;
        for (; (i >= 1) && newScore > entries[i - 1].getScore(); i--)
        {
            entries[i] = entries[i - 1]; // move entry[i - 1] one to the right
        }
        entries[i] = s;
    }
    public Score remove(int i) throws IndexOutOfBoundsException
    {
        if((i < 0) || (i >= numEntries))
        {
            throw new IndexOutOfBoundsException("Invalid index: " + i);
        }
        Score temp = entries[i];
        for(int j = i; j < numEntries - 1; j++)
        {
            entries[j] = entries[j + 1]; // move entries[j + 1] one to the left
        }
        entries[numEntries - 1] = null; // clear the previous final score
        numEntries--;
        return temp; // return the removed object
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
class Score {
    protected String name; // name of the player earning this score
    protected int score;

    public Score(String n, int s) {
        name = n;
        score = s;
    }

    public String getName() {
        return name;
    }

    public int getScore() {
        return score;
    }

    @Override
    public String toString() {
        return "(" + name + ", " + score + ")";
    }
}

class HighScores {
    public static final int maxEntries = 10;
    protected int numEntries;
    protected Score[] entries;

    public HighScores() {
        entries = new Score[maxEntries];
        numEntries = 0;
    }

    public String toString() {
        String s = "";
        for(int i=0; i<numEntries; i++) {
            s += entries[i].toString() + " ";
        }
        return s;
    }

    // methods for updating the set of high scores go here
    public void add(Score s) {
        int newScore = s.getScore();
        if (numEntries == maxEntries) {
            if (newScore <= entries[numEntries - 1].getScore())
                return; // the new entry is not a high score
        } else
            numEntries++;
        int i = numEntries - 1;
        for (; (i >= 1) && newScore > entries[i - 1].getScore(); i--) {
            entries[i] = entries[i - 1]; // move entry[i - 1] one to the right
        }
        entries[i] = s;
    }

    public Score remove(int i) throws IndexOutOfBoundsException {
        if ((i < 0) || (i >= numEntries)) {
            throw new IndexOutOfBoundsException("Invalid index: " + i);
        }
        Score temp = entries[i];
        for (int j = i; j < numEntries - 1; j++) {
            entries[j] = entries[j + 1]; // move entries[j + 1] one to the left
        }
        entries[numEntries - 1] = null; // clear the previous final score
        numEntries--;
        return temp; // return the removed object
    }
}

public class Main {

    public static void main(String[] args) {
        HighScores highScores = new HighScores();
        highScores.add(new Score("P1", 50));
        highScores.add(new Score("P2", 55));
        highScores.add(new Score("P3", 40));
        highScores.add(new Score("P4", 43));
        highScores.add(new Score("P5", 77));
        highScores.add(new Score("P6", 66));

        System.out.println(highScores);

        highScores.remove(3);
        System.out.println(highScores);

        highScores.add(new Score("P7", 85));
        highScores.add(new Score("P8", 58));
        highScores.add(new Score("P9", 92));
        highScores.add(new Score("P10", 67));
        highScores.add(new Score("P11", 79));
        System.out.println(highScores);

        // adding when full
        highScores.add(new Score("P12", 99));
        System.out.println(highScores);

    }

}

X , ika gu 리 d -e曰▼ ▼ロ日 Testjava 团ComputeAreas.java D RandomcardsJava D Main.javaX Search Console <terminated> Main (5) Java Application] CProgram FilesJavajdk1.8.0 73 binavaw.exe (P5, 77) (P6, 66) (P2, 55) (P1, 50) (P4, 43) (P3, 49) (P5, 77) (P6, 66) (P2, 55) (P4, 43) (P3, 48) (P9, 92) (P7, 85) (P11, 79) (P5, 77) (P10, 67) (P6, 66) (P8, 58 (P12, 99) (P9, 92) (P7, 85) (P11, 79) (P5, 77) (P10, 67) (P6, 6 57 public Score remove(int i) throws IndexoutofBoundsException if ((İ < 0) | | (i >= numEntries)) { 58 59 throw new Index0utofBoundsException(Invalid index: +); 61 62 63 64 Score tempentries[i]: for (int j !; j < num Entries - 1; j++) { entries[j] - entries[j1] move entries[j 1] one to the left entries[numEntries 1null; // clear the previous final score numEntries--; return temp; // return the removed object 67 69 71 public class Main 72 73 public static void main (String[] args) ( 74 75 76 Highscores highscores = new Highscores(); highScores.add(new Score(P1, 50)); highScores.add (new Score(P2, 55)); highScores.add(new Score(РЗ, 40)); highScores.add (new Score(P4 highScores.add (new Score(PS, 77)) highScores.add (new Score(P6, 66)); , 43); 79 80 81 82 83 84 85 86 87 System.out.println(highScores); highScores.remove(3); System.out.println(highScores); 89 90 91 92 93 94 95 highScores.add (new Score(P7, 85)); highScores.add (new Score(P8, 58)); highScores.add (new Score(P9, 92) highScores.add (new Score P10, 67) highScores.add (new Score( P11, 79)); System.out.println(highScores): // adding when full highScores.add (new Score(P12 System.out.println(high5cores); , 99)); 97 98

Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
Override toString() for the class HighScores. It should return a String that contains all of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a toString method for the Scores class. It should print the Scores object as follows:...

    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 =...

  • JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has...

    JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1.   You are given a list of phrases each ending with a pound sign: ‘#’. 2.   Create a single String object from this list. 3.   Then, split the String of phrases into an array of phrases...

  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • Define a toString method in Post and override it in EventPost. EventPost Class: /** * This...

    Define a toString method in Post and override it in EventPost. EventPost Class: /** * This class stores information about a post in a social network news feed. * The main part of the post consists of events. * Other data, such as author and type of event, are also stored. * * @author Matthieu Bourbeau * @version (1.0) March 12, 2020 */ public class EventPost extends Post { // instance variables - replace the example below with your own...

  • Write a full class definition for a class named Player , and containing the following members: A data member name of t...

    Write a full class definition for a class named Player , and containing the following members: A data member name of type string . A data member score of type int . A member function called setName that accepts a parameter and assigns it to name . The function returns no value. A member function called setScore that accepts a parameter and assigns it to score . The function returns no value. A member function called getName that accepts no...

  • Improve the speed of public T get(int i) by having it work backward from the end...

    Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> {    private static class Node<T> {        public Node<T> prev, next;...

  • Java programming help My program wont run. could someone tell me why. everything seems correct to...

    Java programming help My program wont run. could someone tell me why. everything seems correct to me... giving me the error: Exception in thread "main" java.lang.NumberFormatException: For input string: "Stud" at java.base/java.lang.NumberFormatException.forInputString(Unknown Source) at java.base/java.lang.Integer.parseInt(Unknown Source) at java.base/java.lang.Integer.parseInt(Unknown Source) at Util.readFile(Util.java:35) at Driver.main(Driver.java:8) _________________________________________________________________________________________________________________________ Program Prompt: Write a program to perform statistical analysis of scores for a class of students.The class may have up to 40 students.There are five quizzes during the term. Each student is identified by a four-digit...

  • Plz help me with the code. And here are the requirement. Thanks!! You are required to...

    Plz help me with the code. And here are the requirement. Thanks!! You are required to design and implement a circular list using provided class and interface. Please filling the blank in CircularList.java. This circular list has a tail node which points to the end of the list and a number indicating how many elements in the list. And fill out the blank of the code below. public class CircularList<T> implements ListInterface<T> { protected CLNode<T> tail; // tail node that...

  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    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;    }   ...

  • cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /**...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT