Question

In: Computer Science

My question is on this program I have difficulty i marked by ??? public class SortedSet...

My question is on this program I have difficulty i marked by ???

public class SortedSet { private Player[] players; private int count;

public SortedSet() { this.players = new Player[10]; }

/** * Adds the given player to the set in sorted order. Does not add

* the player if the case-insensitive name already exists in the set.

* Calls growArray() if the addition of the player will exceed the size

* of the current array. Uses an insertion sort algorithm to place the

* new player in the correct position in the set, taking advantage of

* the private swapPlayers method in this class.

* * @param player the player to add

* @return the index where the player was added, or -1 if not added

*/

public int add(Player player) {

My question is how to add sorted order with the player in the case-insensitive

????????????????????????????????????????????????????? and

return -1;

}

/**

* @param name the name of the player to remove

* @return true if removed, false if not found

*/

public boolean remove(String name) {

??????????????????????????????????????????????????????????????????????

How to removes the player with the given case-insensitive name from the set. return true;

}

/**

* @param name the player's name

* @return the index where the player is stored, or -1 if not found

*/

public int find(String name) {

?????????????????????????????????????????????????????????

How to Locates the player with the given case-insensitive name in the set. return 0;

}

/**

* @param index the index from which to retrieve the player

* @return the player object, or null if index is out of bounds.

*/

public Player get(int index) {

?????????????????????????????????????????????????????????

How to returns the player object stored at the given index. return null;

}

/**

* Provides access to the number of players currently in the set.

* @return the number of players */ public int size() { return count;

}

/**

* Provides access to the current capacity of the underlying array.

* @return the capacity of the array

*/

public int capacity() { return players.length;

}

/** Provides a default string representation of th sorted set. Takes

* advantage of Player's toString method to provide a single line String.

* Example: [ (Player: joe, Score: 100) (Player: fred, Score: 98) ]

* @return the string representing the entire set

*/ @Override

public String toString() {

??????????????????????????????????????

How to provides a default string ?????????

return null;

}

/**

* @param i the first index

* @param j the second index

*/

private void swapPlayers(int i, int j) {

??????????????????????????????????????????????????????

How to private method used during sorting to swap players in the underlying array.

}

private void growArray() {

???????????????????????????????????????????????????????????

How to private method used to double the array if adding a new player will exceed the size of the current array.

}

}

//----------------------------------------------------------------------------------------------------------------------------// //

players Classes public class Player implements Comparable {

//

fields private String name; private int score;

/**

* Full constructor.

* @param name the player's name

* @param score the player's highest score

*/

public Player(String name, int score) {

this.name = name; this.score = score;

}

/**

* Provides access to the player's name.

* @return the player's name

*/

public String getName() {

return name;

}

/**

* Allows the player's name to be set.

* @param name the player's name

*/

public void setName(String name) {

this.name = name;

}

/**

* Provides access to the player's highest score.

* @return the player's highest score

*/

public int getScore() {

return score;

}

/**

* Allows the player's highest score to be set.

* @param score the player's highest score

*/

public void setScore(int score) {

this.score = score;

}

/**

* Provides a default string representation of an object of this class.

* @return */ @Override public String toString() {

return "Player: " + name + ", Score: " + score;

}

/** * Provides a unique hash code for this object,

* based on the case-insensitive player name.

* @return the hash code

*/

@Override public int hashCode() {

int hash = 3;

hash = 83 * hash + Objects.hashCode(this.name.toLowerCase());

return hash;

}

/** * Reports if the given object is equal to this object,

* based on the case-insensitive player name.

* * @param obj the object to compare to this one

* @return true if the names are the same, false if not.

*/ @Override

public boolean equals(Object obj) {

if (this == obj) {

return true;

}

if (obj == null) {

return false;

}

if (getClass() != obj.getClass()) {

return false;

}

final Player other = (Player) obj;

return this.name.equalsIgnoreCase(other.name);

}

/** * Compares the given object with this one to determine sort order.

* * @param other the other object to compare to this one

* @return a negative value if this object should come before the other one,

* a positive value if it should come after, or zero if they are the same

*/ @Override public int compareTo(Player other) { return other.score - this.score; } }

//----------------------------------------------------------------------------------------------------------------------------//

// Main class

public class Lab1 {

/** * All tests performed here in main method.

* * @param args the command line arguments

*/

public static void main(String[] args) {

SortedSet set = new SortedSet();

//test insertion for (int i = 0; i < 10; i++) {

if (set.add(new Player(String.valueOf((char) (i + 97)), i + 10)) != 0) {

System.out.println("INSERTION FAIL"); return; }

}

System.out.println("INSERTION PASS");

//test growing array

if (set.add(new Player("k", 9)) != 10) {

System.out.println("GROW FAIL"); return;

}

System.out.println("GROW PASS");

//test duplicate

if (set.add(new Player("D", 5)) != -1) {

System.out.println("DUPLICATE FAIL");

return;

}

System.out.println("DUPLICATE PASS");

//test valid remove

if (!set.remove("c")) {

System.out.println("VALID REMOVE FAIL");

return;

}

System.out.println("VALID REMOVE PASS");

//test invalid remove if (set.remove("z")) {

System.out.println("INVALID REMOVE FAIL");

return;

}

System.out.println("INVALID REMOVE PASS");

//test valid find

if (set.find("g") != 3) {

System.out.println("VALID FIND FAIL");

return;

}

System.out.println("VALID FIND PASS");

//test invalid find

if (set.find("z") != -1) {

System.out.println("INVALID FIND FAIL");

return;

}

System.out.println("INVALID FIND PASS");

//test valid get

if (set.get(0).getScore() != 19) {

System.out.println("VALID GET FAIL");

return;

}

System.out.println("VALID GET PASS");

//test invalid

get if (set.get(100) != null) {

System.out.println("INVALID GET FAIL");

return;

}

System.out.println("INVALID GET PASS");

//test toString method try { String str = set.toString();

if (str.equals("[ (Player: j, Score: 19) (Player: i, Score: 18) " + "(Player: h, Score: 17) (Player: g, Score: 16) " + "(Player: f, Score: 15) (Player: e, Score: 14) " + "(Player: d, Score: 13) (Player: b, Score: 11) " + "(Player: a, Score: 10) (Player: k, Score: 9) ]")) { System.out.println("TOSTRING PASS"); } else { System.out.println("TOSTRING FAIL"); } } catch (Exception e) { System.out.println("TOSTRING FAIL"); } //test proper capacity of array if (set.capacity() != 20) { System.out.println("SIMPLE CAPACITY FAIL"); return; } System.out.println("SIMPLE CAPACITY PASS"); for (int i = 0; i < 100; i++) { set.add(new Player((String.valueOf((char) (i + 97))) + i, i)); } if (set.capacity() != 160) { System.out.println("COMPLEX CAPACITY FAIL"); return; } System.out.println("COMPLEX CAPACITY PASS"); } }

Solutions

Expert Solution

import java.util.Objects;

//----------------------------------------------------------------------------------------------------------------------------//

//players

public class Player implements Comparable<Player>{

       //fields

      

       private String name;

       private int score;

       /**

       * Full constructor.

       * @param name the player's name

       * @param score the player's highest score

       */

      

       public Player(String name, int score) {

             this.name = name; this.score = score;

             }

             /**

             * Provides access to the player's name.

             * @return the player's name

             */

             public String getName() {

             return name;

             }

            

             /**

             * Allows the player's name to be set.

             * @param name the player's name

             */

             public void setName(String name) {

             this.name = name;

             }

            

             /**

             * Provides access to the player's highest score.

             * @return the player's highest score

             */

             public int getScore() {

             return score;

             }

             /**

             * Allows the player's highest score to be set.

             * @param score the player's highest score

             */

             public void setScore(int score) {

             this.score = score;

             }

            

             /**

             * Provides a default string representation of an object of this class.

             * @return */ @Override public String toString() {

             return "Player: " + name + ", Score: " + score;

             }

             /** * Provides a unique hash code for this object,

             * based on the case-insensitive player name.

             * @return the hash code

             */

             @Override public int hashCode() {

             int hash = 3;

             hash = 83 * hash + Objects.hash(this.name.toLowerCase());

             return hash;

             }

            

             /** * Reports if the given object is equal to this object,

             * based on the case-insensitive player name.

             * * @param obj the object to compare to this one

             * @return true if the names are the same, false if not.

             */ @Override

             public boolean equals(Object obj) {

             if (this == obj) {

             return true;

             }

             if (obj == null) {

             return false;

             }

             if (getClass() != obj.getClass()) {

             return false;

             }

             final Player other = (Player) obj;

             return this.name.equalsIgnoreCase(other.name);

             }

            

             /** * Compares the given object with this one to determine sort order.

             * * @param other the other object to compare to this one

             * @return a negative value if this object should come before the other one,

             * a positive value if it should come after, or zero if they are the same

             */

            

             @Override

             public int compareTo(Player other)

            

             { return other.score - this.score; }

}

//end of Player.java

//SortedSet.java

public class SortedSet {

      

       private Player[] players;

       private int count;

      

       public SortedSet() {

             this.players = new Player[10];

             count = 0;

       }

      

       /** * Adds the given player to the set in sorted order. Does not add

       * the player if the case-insensitive name already exists in the set.

       * Calls growArray() if the addition of the player will exceed the size

       * of the current array. Uses an insertion sort algorithm to place the

       * new player in the correct position in the set, taking advantage of

       * the private swapPlayers method in this class.

       * * @param player the player to add

       * @return the index where the player was added, or -1 if not added

       */

       public int add(Player player) {

             // check if player exists in the array, then return -1

             for(int i=0;i<count;i++)

             {

                    if(players[i].equals(player))

                           return -1;

             }

            

             // check if array is full, then expand the array

             if(count == players.length)

             {

                    growArray();

             }

            

             // loop to insert the player in the correct position, in descending order by score

             for(int i=0;i<count;i++)

             {

                    if(players[i].compareTo(player) > 0)

                    {

                           for(int j=count;j>i;j--)

                           {

                                 swapPlayers(j,j-1);

                           }

                          

                           players[i] = player;

                           count++;

                           return i;

                    }

             }

            

             players[count] = player;

             count++;

             return count-1;

       }

       /**

       * @param name the name of the player to remove

       * @return true if removed, false if not found

       */

       public boolean remove(String name) {

             int index = find(name);

             if(index == -1)

                    return false;

             else

             {

                    for(int i=index;i<count-1;i++)

                    {

                           swapPlayers(i,i+1);

                    }

                    count--;

                   

                    return true;

             }

       }

      

       /**

       * @param name the player's name

       * @return the index where the player is stored, or -1 if not found

       */

       public int find(String name) {

             for(int i=0;i<size();i++)

             {

                    if(players[i].getName().equalsIgnoreCase(name))

                           return i;

             }

            

             return -1;

       }

      

      

       /**

       * @param index the index from which to retrieve the player

       * @return the player object, or null if index is out of bounds.

       */

       public Player get(int index) {

             if(index >=0 && index <size())

                    return players[index];

             return null;

       }

      

       /**

       * Provides access to the number of players currently in the set.

       * @return the number of players */

       public int size() {

             return count;

       }

      

       /**

       * Provides access to the current capacity of the underlying array.

       * @return the capacity of the array

       */

       public int capacity() {

             return players.length;

       }

      

       /** Provides a default string representation of th sorted set. Takes

       * advantage of Player's toString method to provide a single line String.

       * Example: [ (Player: joe, Score: 100) (Player: fred, Score: 98) ]

       * @return the string representing the entire set

       */

       @Override

       public String toString() {

            

             String str="[ ";

             for(int i=0;i<count;i++)

             {

                    str+= "("+players[i].toString()+") ";

             }

            

             str+="]";

            

             return str;

       }

      

       /**

       * @param i the first index

       * @param j the second index

       */

       private void swapPlayers(int i, int j) {

             Player temp = players[i];

             players[i] = players[j];

             players[j] = temp;

       }

      

       private void growArray() {

            

             Player temp[] = new Player[2*count];

             for(int i=0;i<players.length;i++)

                    temp[i] = players[i];

             players = temp;

       }

      

}

//end of SortedSet.java

// Lab1 .java

public class Lab1 {     

      

       /** * All tests performed here in main method.

       * * @param args the command line arguments

       */

       public static void main(String[] args) {

             SortedSet set = new SortedSet();

             //test insertion

            

             for (int i = 0; i < 10; i++) {

                    if (set.add(new Player(String.valueOf((char) (i + 97)), i + 10)) != 0) {

                   

                           System.out.println("INSERTION FAIL");

                           return;

                    }

             }

             System.out.println("INSERTION PASS");

             //test growing array

             if (set.add(new Player("k", 9)) != 10) {

             System.out.println("GROW FAIL"); return;

             }

             System.out.println("GROW PASS");

             //test duplicate

             if (set.add(new Player("D", 5)) != -1) {

             System.out.println("DUPLICATE FAIL");

             return;

             }

             System.out.println("DUPLICATE PASS");

             //test valid remove

             if (!set.remove("c")) {

             System.out.println("VALID REMOVE FAIL");

             return;

             }

             System.out.println("VALID REMOVE PASS");

             //test invalid remove

             if (set.remove("z")) {

             System.out.println("INVALID REMOVE FAIL");

             return;

             }

             System.out.println("INVALID REMOVE PASS");

             //test valid find

             if (set.find("g") != 3) {

             System.out.println("VALID FIND FAIL");

             return;

             }

             System.out.println("VALID FIND PASS");

             //test invalid find

             if (set.find("z") != -1) {

             System.out.println("INVALID FIND FAIL");

             return;

             }

             System.out.println("INVALID FIND PASS");

             //test valid get

             if (set.get(0).getScore() != 19) {

             System.out.println("VALID GET FAIL");

             return;

             }

             System.out.println("VALID GET PASS");

             //test invalid get

             if (set.get(100) != null) {

             System.out.println("INVALID GET FAIL");

             return;

             }

             System.out.println("INVALID GET PASS");

             //test toString method

             try {

                    String str = set.toString();

                   

                    if (str.equals("[ (Player: j, Score: 19) (Player: i, Score: 18) " + "(Player: h, Score: 17) (Player: g, Score: 16) " + "(Player: f, Score: 15) (Player: e, Score: 14) " + "(Player: d, Score: 13) (Player: b, Score: 11) " + "(Player: a, Score: 10) (Player: k, Score: 9) ]"))

                    {

                           System.out.println("TOSTRING PASS");

                   

                    } else {

                           System.out.println("TOSTRING FAIL"); }

             } catch (Exception e)

             {

                    System.out.println("TOSTRING FAIL");

                   

             } //test proper capacity of array

            

             if (set.capacity() != 20) {

                    System.out.println("SIMPLE CAPACITY FAIL");

                    return;

             }

            

             System.out.println("SIMPLE CAPACITY PASS");

             for (int i = 0; i < 100; i++) {

                    set.add(new Player((String.valueOf((char) (i + 97))) + i, i));

             }

             if (set.capacity() != 160) {

                    System.out.println("COMPLEX CAPACITY FAIL");

                    return;

             }

            

             System.out.println("COMPLEX CAPACITY PASS");

            

       }

}

//end of Lab1.java

Output:


Related Solutions

I have written this paper for my public speaking class,and I would appreciate it if someone...
I have written this paper for my public speaking class,and I would appreciate it if someone revised it for me to make sure my punctuations are correct as well as everything else.Thank you, Public Speaking…Dealing with It Often when a person thinks of public speaking the thoughts a person may have will vary from the next, because while one person may have experience the other person may not. Being able to communicate effectively requires that a person acknowledges the frame...
I have a quick question about this C ++ problem in my intro class. I have...
I have a quick question about this C ++ problem in my intro class. I have been able to get the first answer consistently when I test it, but my second seems to either be high or low no matter how I change it. Can you show me how you would do this problem with the setprecision(2) Paula and Danny want to plant evergreen trees along the back side of their yard. They do not want to have an excessive...
This is a question for my biochemistry class. I have copied and pasted it below. This...
This is a question for my biochemistry class. I have copied and pasted it below. This is all that was given to me. Diagram the flow of genetic material in a cell.
I have this question in my nursing class I just need someone to answer it for...
I have this question in my nursing class I just need someone to answer it for me Reflect on an occasion where you experienced ineffective leadership (doesn't have to be in the hospital/healthcare). What behaviors did they display? What factors may have influenced their leadership style?
I have a program to code for my computer science class, and Im not sure how...
I have a program to code for my computer science class, and Im not sure how to do it. If someone can explain it step by step I would appreciate it. Problem Description: Define the GeometricObject2D class that contains the properties color and filled and their appropriate getter and setter methods. This class also contains the dateCreated property and the getDateCreated() and toString() methods. The toString() method returns a string representation of the object. Define the Rectangle2D class that extends...
I have a term paper that I have to write for my microeconomics class. Economics is...
I have a term paper that I have to write for my microeconomics class. Economics is super new to me, this is the first class I've ever taken related to it. My subject is the economic impacts of the opioid epidemic. My outline is due next week and I have no idea what to even do research on or which direction to go. Where do I even start?
I was wondering why my merger class isn't compiling - Java -------------------------------------------------------------------------------------------------------------------- public class Person{ private...
I was wondering why my merger class isn't compiling - Java -------------------------------------------------------------------------------------------------------------------- public class Person{ private String firstName; private String lastName; private int age; private String email; private String phone; private String address;       public Person(String firstName, String lastName, int age, String email, String phone, String address){ setFirstName(firstName); setLastName(lastName); setAge(age); setEmail(email); setPhone(phone); setAddress(address); } public Person(String firstName, String lastName, String email){ setFirstName(firstName); setLastName(lastName); setEmail(email); }    public String getFirstName(){ return firstName; }    public String getLastName(){ return lastName; }...
I have 5 multiple choice question related to my accounting class. 1. In the "time interest...
I have 5 multiple choice question related to my accounting class. 1. In the "time interest earned" ratio, the numerator should be income before deducting (select the best answer): A. interest Expense B. Income tax expense C. Both A & B D. None of the above 2. A lender accepts real estate with a fair value of $15,000,000 in full settlement of a $20,000,000 loan. the real estate has a carrying value of $19,000,000 on the bond of the borrower....
how to sorted the list from another class!!! example i have a class public meso public...
how to sorted the list from another class!!! example i have a class public meso public meso(string id) public hashmap<string, integer> neou{ this class print out b d e c a now create another class public mesono public mesono(hashmap<string, integer> nei) //want to sorted meso class print list to this class!! // print out a b c d e } ( -------Java------)   
Hello, I am having difficulty with this assignment. I chose Amazon as my stock, but I...
Hello, I am having difficulty with this assignment. I chose Amazon as my stock, but I am confused on what they're asking :             Choose a stock that interests you. Utilizing Bloomberg (or other financial websites) as a source of data, collect the following      information:                         a. The stock’s Beta                         b. The rate of return on the market (S&P 500 Index)                         c. The risk-free rate (rRF)                         d. The last dividend paid (D0)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT