2013-05-19 39 views
0

爲了做作業,我給出了下面的代碼,其中的方法爲空白。我一直在研究它,但我仍然不明白mutator setComparer或比較器比較器是如何在此程序中工作的。我在網上研究瞭如何使用比較器,但這個想法仍然不清楚。任何人都可以給我一些指導嗎?如何在卡片遊戲中使用比較器

  1. 是否必須初始化比較器比較器?如果是的話,我該怎麼做?
  2. 什麼上面私人詮釋的意見比較(遊戲牌之一,遊戲牌二)是什麼意思?(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); 
    } 
} 
+0

我試圖標記爲功課,但我不能! – jorgeAChacon

+0

看起來你的程序是部分寫入的,'public void setComparer(Comparator comparer){未設置... ...你的任務是完成一個部分編寫的程序嗎? – CHEBURASHKA

+0

想一想,卡類中的哪些變量需要進行比較以確定它們是否相等。如果它們是整數,那麼如果它們是字符串,則使用'==',執行'equals'。 – greedybuddha

回答

1

基本上當你想有你使用比較不同類型的相同類型的對象之間的比較。例如,你有

List<Integer> list = new ArrayList<Integer>(); 

你想擁有既升序和降序排序。你做的是你寫的,其實現比較不同類別:

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)); 
    } 
} 

然後你就可以對數組進行排序:

Collections.sort(list, new Descending()); 

的回答你的問題:

1 - 這取決於如何你使用類PlayingCardHand。從我看到你需要初始化它。

2-註釋意味着使用PlayingCardHand的代碼將決定使用什麼排序方法。

1

有總是可用

1.您的類實現可比接口2點的選擇和因此提供實施的compareTo()方法。

2.您可以通過創建自己的比較器類來創建自己的比較器,該比較器類實現了比較器接口,因此提供了比較()方法的實現方法

在第一種情況下,你只需要調用Collections.sort(your_collection)或Arrays.sort(your_array)和在第二種情況下呼叫作爲Collections.sort(your_collection,your_comparator的對象)或陣列。排序(you_array,您的集合的對象)

使用II'nd版本總是更好,因爲它沒有爲您的類的所有集合定義默認排序行爲。

欲瞭解更多詳情,請參閱Comparator and Comparable in Java