Reputation: 327
For homework I was given the following code with the methods in blank. I have been working on it,but I still do not understand how the mutator setComparer or the Comparator comparer work in this program. I have researched online how to use Comparators,but the idea is still unclear. Can anyone give me some kind of guidance?.
Thanks
import java.util.*;
//Class to represent a "generic" hand of playing-cards
public class PlayingCardHand {
//Instance Variables
private int cardsInCompleteHand; //Maximum # cards in this hand
private ArrayList<PlayingCard> hand; //A hand of Playing-Cards
private Comparator comparer; //Client-provided comparison of PlayingCards
//Constructor
//Appropriate when PlayingCard compareTo() is to be used to compare PlayingCards
public PlayingCardHand(int handSize) {
cardsInCompleteHand = handSize;
hand = new ArrayList<PlayingCard>();
}
//Helper: Compare 2 PlayingCards
// if this.comparer is null, comparison uses compareTo()
// otherwise the Comparator is applied
private int compare(PlayingCard one, PlayingCard two) {
return 0;
}
//Accessor: return # of cards currently in this hand
public int getNumberOfCards() {
return cardsInCompleteHand;
}
public boolean isComplete() {
if (hand.size() == cardsInCompleteHand) {
return true;
}
return false;
}
//Accessor: return COPIES of the cards in this hand
public PlayingCard[] getCards() {
PlayingCard[] temp = new PlayingCard[hand.size()];
for (int i = 0; i < hand.size(); i++)//ch
{
temp[i] = hand.get(i);
}
return temp;
}
//Mutator: allows a client to provide a comparison method for PlayingCards
public void setComparer(Comparator comparer) {
}
//Mutator: Append a new card to this hand
public void appendCard(PlayingCard card) {
int counter = 0;
PlayingCard.Suit su = card.getSuit();
PlayingCard.Rank ra = card.getRank();
PlayingCard temp3 = new PlayingCard(su, ra);
//10 20 goes here 30 40 if insert 25
for (int i = 0; i < hand.size(); i++) {
PlayingCard temp4 = hand.get(i);
PlayingCard.Suit sui = temp4.getSuit();
PlayingCard.Rank ran = temp4.getRank();
if (su.ordinal() <= sui.ordinal() && ra.ordinal() <= ran.ordinal()) {
hand.add(i, temp3);
counter++;
}
}
while (counter == 0) {
hand.add(temp3);
}
}
Upvotes: 0
Views: 1502
Reputation: 6695
There are always 2 options available
1.Your Class Implements comparable Interface and thus providing implementation of compareTo() method.
2.You create your own comparator by creating your own comparator class which implements comparator interface and hence providing implementation of compare() method.
In the first case you simply need to call Collections.sort(your_collection) or Arrays.sort(your_array) and in the second case call as Collections.sort(your_collection,object of your_comparator) or Arrays.sort(you_array,object of your collection)
It is always better to use II'nd Version as it does not define the default sorting behavior for all collections of your class.
For more details refer Comparator and Comparable in Java
Upvotes: 1
Reputation: 1687
Basically you use Comparator when you want to have different types of comparison between objects of same type. for example you have
List<Integer> list = new ArrayList<Integer>();
you want to have both ascending sort and descending sort. what you do is you write to different classes which implement Comparator:
public class Ascending implements Comparator<Integer>{
@Override
public int compare(Integer o1, Integer o2) {
return (o1>o2 ? -1 : (o1==o2 ? 0 : 1));
}
}
public class Descending implements Comparator<Integer>{
@Override
public int compare(Integer o1, Integer o2) {
return (o1<o2 ? -1 : (o1==o2 ? 0 : 1));
}
}
Then you can sort the array:
Collections.sort(list, new Descending());
the answer to your question:
1- it depends on how you use the class PlayingCardHand. from what I see you need to initialize it.
2- the comment means that the code that is using PlayingCardHand will decide what sorting method to use.
Upvotes: 1