Question: It looks like the current implementation may not handle the case where the first player ( head of the list ) needs to be replaced

It looks like the current implementation may not handle the case where the first player (head of the list) needs to be replaced properly, especially when inserting p3 before p1.
***test case
public void testA12(){
c = conceal(HigherScore.getInstance());
p1= new Player("joe1",10, Position.Quarterback);
p2= new Player("joe2",20, Position.Defensive_Line);
p3= new Player("joe",30, Position.Kicker);
p1.addInPriority(p3, c);
p1.addInPriority(p2, c);
testPlayers(p3, p2, p1);
}
**My method
public void addInPriority(Player p, Comparator priority){
// TODO: Implement this method. No loops, only recursion.
//
// NB: While Team happens to call this method only on the head of the list,
// we can't assume all classes that utilize Player will do so. That is why
// we must consider all scenarios, including those where this method is
// called on a player in the middle or end of the list.
int a = priority.compare(p, this);
/**
* Left of this and Immediate left is null
* add p left with mumtiples players
*/
if(a >0){
if(this.prev == null ){
p.next = this;
this.prev = p;
}
else {
this.prev.addInPriority(p, priority);
}
}
/**
* right of this, and immediate right null
* add p right with multiples players
*/
else if(a <0){
if(this.next ==null){
this.next = p;
p.prev = this;
}
else {
this.next.addInPriority(p, priority);
}
}
/**
* equal to this
*/
else {
if(this.next != null){
this.next.prev = p;
p.next = this.next;
}
this.next = p;
p.prev = this;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!