Answer in eclispe please!!! Do the following: 1. Create a package named, “prob3”
ID: 3693417 • Letter: A
Question
Answer in eclispe please!!!
Do the following:
1. Create a package named, “prob3” and a class named, “Games2”.
2. Create a Player class and add this code:
public class Player {
private String name;
private int points;
public Player(String name, int points) {
this.name = name; this.points = points;
}
public String getName() {
return name;
}
public int getPoints() {
return points;
}
@Override
public String toString() {
return "name=" + name + ", points=" + points;
}
}
3. Define a TreeSet in main to hold Player objects sorted by name, then points.
Test with this code:
Player p1 = new Player("Benito", 33);
Player p2 = new Player("Quincy", 14);
Player p3 = new Player("Lean", 22);
Player p4 = new Player("Carly", 41);
Player p5 = new Player("Pepper", 18);
// Add your code to define TreeSet named "team" here
team.addAll(Arrays.asList(p1,p2,p3,p4,p5));
System.out.println(" Players sorted by name:");
for(Player p : team) System.out.println(p);
Hint: You need to implement and use a Comparator, NameComparator, to sort players by name, then points, in a TreeSet
4. Write a method named getPlayersAbove that accepts a TreeSet of Player objects and an integer, val. The method should return a set of Players containing only the players with points at or above val.
Hints:
(1) You need to implement and use a Comparator, PointsComparator, to sort players by points, then name, in a TreeSet
(2) Create a “dummy” player with points val and use the method tailSet on TreeSet to return a set of all players that are larger than or equal to the “dummy” player in terms of points.
5. Add the code below to the bottom of main to test:
Set big = getPlayersAbove(team,20);
System.out.println(" Players with a lot of points:");
for(Player p : big) System.out.println(p);
The excepted outcome is as follows:
Players sorted by name:
name=Benito, points=33
name=Carly, points=41
name=Lean, points=22
name=Pepper, points=18
name=Quincy, points=14
Players sorted by points:
name=Quincy, points=14
name=Pepper, points=18
name=Lean, points=22
name=Benito, points=33
name=Carly, points=41
Players with a lot of points:
name=Lean, points=22
name=Benito, points=33
name=Carly, points=41
Explanation / Answer
PlayerMainTest.java
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
public class PlayerMainTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Player p1 = new Player("Benito", 33);
Player p2 = new Player("Quincy", 14);
Player p3 = new Player("Lean", 22);
Player p4 = new Player("Carly", 41);
Player p5 = new Player("Pepper", 18);
NameComparator nameComparator = new NameComparator();
TreeSet<Player> team = new TreeSet<Player>(nameComparator);
team.addAll(Arrays.asList(p1,p2,p3,p4,p5));
System.out.println(" Players sorted by name:");
for(Player p : team) System.out.println(p);
PointsComparator pointsComparator = new PointsComparator();
team = new TreeSet<Player>(pointsComparator);
team.addAll(Arrays.asList(p1,p2,p3,p4,p5));
System.out.println(" Players sorted by points:");
for(Player p : team) System.out.println(p);
Set<Player> big = getPlayersAbove(team,20);
System.out.println(" Players with a lot of points:");
for(Player p : big) System.out.println(p);
}
public static TreeSet getPlayersAbove(TreeSet<Player> team, int val){
PointsComparator pointsComparator = new PointsComparator();
TreeSet<Player> tailSet = new TreeSet<Player>(pointsComparator);
for(Player dummy: team){
if(dummy.getPoints() >= val){
tailSet.add(dummy);
}
}
return tailSet;
}
}
NameComparator.java
import java.util.Comparator;
public class NameComparator implements Comparator<Player> {
public int compare(Player p1, Player p2){
return p1.getName().compareTo(p2.getName());
}
}
PointsComparator.java
import java.util.Comparator;
public class PointsComparator implements Comparator<Player> {
public int compare(Player p1, Player p2){
return p1.getPoints() - p2.getPoints();
}
}
Output:
Players sorted by name:
name=Benito, points=33
name=Carly, points=41
name=Lean, points=22
name=Pepper, points=18
name=Quincy, points=14
Players sorted by points:
name=Quincy, points=14
name=Pepper, points=18
name=Lean, points=22
name=Benito, points=33
name=Carly, points=41
Players with a lot of points:
name=Lean, points=22
name=Benito, points=33
name=Carly, points=41
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.