Question

So I have a lab (we are allowed to seek outside help on it, so here I am after lots of head scratching) where we have to implement a deck of cards. We have to use the enum class to create num

For Suits:

public enum Suits {
CLUBS, HEARTS, DIAMONDS, SPADES

}

For Numerals:

public enum Numerals {
DEUCE(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), 
TEN(10), JACK(11), QUEEN(12), KING(13), ACE(14);

}

My card class is pretty straightforward, but I'm not sure about these two blocks of code:

    public int compareTo (Card aCard){
    if (aCard.aNumeral.equals(this.aNumeral) && aCard.aSuit.equals(this.aSuit)){
        return 0;
    }
    else {
        return -1;
    }
}

and

    public boolean equals (Card aCard){
    if (this.compareTo(aCard) == 0){
        return true;
    }
    else {
        return false;
    }
}

Now for the tricky part...the Deck...

So we have to implement the deck using Cloneable, Iterable and Comparator, so here is what I have so far and just can't figure out what to.

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;

public class Deck implements Cloneable, Iterable<Card>, Comparator<Card> {

private ArrayList<Card> cards;

public Deck (){
    for (Card c : cards){


    }

}

I'm struggling to even put together the constructor. I'm planning on using an ArrayList to essentially "hold" 52 sorted cards (as you can see); but we have to ultimately return a sorted deck. Any suggestions on where to go?

Était-ce utile?

La solution

To answer the question about compareTo: a.compareTo(b) should return something negative if a is less than b, positive if a is greater than b, and 0 if they're equal. Also, if you're ordering the objects, one of the rules the ordering should follow is that if a.compareTo(b) < 0, then b.compareTo(a) > 0. (You can't have both "a is less than b" and "b is less than a"). Your compareTo, which just returns -1 anytime the cards aren't equal, doesn't follow this rule. To fix this, you'll need to decide on the ordering. What does a "sorted deck" look like? Probably all the clubs are together, followed by all the diamonds, etc., which means that any club card will be less than any diamond card. To do this correctly, you'll need to compare the suits first, and the ranks only if the suits are equal:

public int compareTo (Card aCard){
    int suitCompare = this.aSuit.compareTo(aCard.aSuit);
    if (suitCompare != 0)  {
        return suitCompare;
    }
    return this.aNumeral.compareTo(aCard.aNumeral);
}

The compareTo on each enum will return <0, 0, or >0. So you can compare the suits, return a value that is <0 or >0, and then compare the ranks if the suits are equal. This is the general approach for writing any compareTo method where multiple pieces of data need to be checked.

Autres conseils

if you use an ide like eclipse, it will tell you what methods you need to implement for the interfaces:

import java.util.*;
class Card {}
class Deck implements Cloneable,Iterable<Card>,Comparator<Card> {
    public Deck() {}
    @Override public int compare(Card arg0,Card arg1) {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override public Iterator<Card> iterator() {
        // TODO Auto-generated method stub
        return null;
    }
    private ArrayList<Card> cards;
}

the iterator is easy, since array list has one. look for examples of comparable. you will have to initialize the deck somehow. i put a static final array in my card class that contained all of the cards. you can do an add all from that array using arrays as list.

First of all, if your enum has a parameter, you should declare a constructor with a paramater:

public enum Numerals {
    DEUCE(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(
            10), JACK(11), QUEEN(12), KING(13), ACE(14);
    private Numerals(int i) {
        this.value = i;
    }

    int value;
}

To create yor deck, you must iterate your suits and your numerals and create a card for each pair:

public Deck() {
    for (Suits s : Suits.values()) {
        for (Numerals n : Numerals.values()) {
            cards.add(new Card(s, n));
        }
    }
}

From compareTo javadoc: Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

So you must declare an order for your Card class to implement compareTo (using ordinals)

public int compareTo(Card o) {
    if(this.suit.ordinal() == o.suit.ordinal()){
        return this.numeral.ordinal() - o.numeral.ordinal();
    }
    return this.suit.ordinal() - o.suit.ordinal();
}

Hope it helps.

PS: be careful overriding equals method take a look here

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top