爲了做作業,我給出了下面的代碼,其中的方法爲空白。我一直在研究它,但我仍然不明白mutator setComparer或比較器比較器是如何在此程序中工作的。我在網上研究瞭如何使用比較器,但這個想法仍然不清楚。任何人都可以給我一些指導嗎?如何在卡片遊戲中使用比較器
- 是否必須初始化比較器比較器?如果是的話,我該怎麼做?
- 什麼上面私人詮釋的意見比較(遊戲牌之一,遊戲牌二)是什麼意思?(this.comparer = NULL)
感謝
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);
}
}
我試圖標記爲功課,但我不能! – jorgeAChacon
看起來你的程序是部分寫入的,'public void setComparer(Comparator comparer){未設置... ...你的任務是完成一個部分編寫的程序嗎? – CHEBURASHKA
想一想,卡類中的哪些變量需要進行比較以確定它們是否相等。如果它們是整數,那麼如果它們是字符串,則使用'==',執行'equals'。 – greedybuddha