java - how to use Comparator in a card game -


for homework given following code methods in blank. have been working on it,but still not understand how mutator setcomparer or comparator comparer work in program. have researched online how use comparators,but idea still unclear. can give me kind of guidance?.

  1. do have initialize comparator comparer? if so, how do it?
  2. what comments above private int compare(playingcard one, playingcard two) mean?(this.comparer=null)

thanks

import java.util.*; //class represent "generic" hand of playing-cards public class playingcardhand { //instance variables  private int cardsincompletehand;     //maximum # cards in hand private arraylist<playingcard> hand; //a hand of playing-cards private comparator comparer;         //client-provided comparison of playingcards  //constructor //appropriate when playingcard compareto() used compare playingcards public playingcardhand(int handsize) {     cardsincompletehand = handsize;     hand = new arraylist<playingcard>();  }  //helper: compare 2 playingcards // if this.comparer null, comparison uses compareto() //                           otherwise comparator applied private int compare(playingcard one, playingcard two) {      return 0; }  //accessor: return # of cards in hand public int getnumberofcards() {     return cardsincompletehand; }  public boolean iscomplete() {     if (hand.size() == cardsincompletehand) {         return true;     }     return false; }  //accessor: return copies of cards in hand public playingcard[] getcards() {     playingcard[] temp = new playingcard[hand.size()];      (int = 0; < hand.size(); i++)//ch     {          temp[i] = hand.get(i);     }     return temp; }  //mutator: allows client provide comparison method playingcards public void setcomparer(comparator comparer) { }  //mutator: append new card 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     (int = 0; < 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);     } } 

basically use comparator when want have different types of comparison between objects of same type. example have

list<integer> list = new arraylist<integer>(); 

you want have both ascending sort , descending sort. write different classes 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 can sort array:

collections.sort(list, new descending()); 

the answer question:

1- depends on how use class playingcardhand. see need initialize it.

2- comment means code using playingcardhand decide sorting method use.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -