In: Computer Science
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"); } }
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: