0

I am creating a deck of 20 cards. Each of them are assigned Integer president from 1-10. The deck should be as follows: 1, 1, 2, 2, 3, 3, ... 17, 17, 18, 18, 19, 19, 20, 20

The contains search says that it is a new card in the deck every time. I believe there may be something wrong with my equals() method, but I am not sure. Any ideas?

//Class MainClass

public void createDeck() {
    cards = new ArrayList<President>();
    President temp;
    for (int i = 1; i <= 20; i++) {
        do {
            temp = new President(i, rand(20));
            System.out.println(cards.contains(temp));
        } while (cards.contains(temp));
        cards.add(temp);
        System.out.println(cards.size());
    }
    for(President p : cards){
        while(p.getPresident() > 10){
            p.setPresident(p.getPresident() - 10);
        }
        System.out.println("" + p.getPresident());
    }

}

//Class President

public class President {

private int president;
private int card;

public President(int card, int president) {
    super();
    this.card = card;
    this.president = president;

}

@Override
public boolean equals(Object o) {
    if(o instanceof President){
        President p = (President) o;
        if(p.getPresident() == this.president && p.getCard() == this.card){
            return true;
        }
    }
    return false;

}

private int getCard() {
    // TODO Auto-generated method stub
    return card;
}

public int getPresident() {
    // TODO Auto-generated method stub
    return president;
}

public void setPresident(int president) {
    // TODO Auto-generated method stub
    this.president = president;
}

}

7
  • Can you show the rest of the President class, or at least its fields? Commented Sep 7, 2013 at 5:53
  • k. i posted the entire class above Commented Sep 7, 2013 at 5:55
  • do while should be avoided is anti-natural i hate it Commented Sep 7, 2013 at 5:59
  • i am open to suggestions nachokk. it seemed like the best choice for what i was trying to do here. or so i thought Commented Sep 7, 2013 at 6:01
  • no, but feel free to ask me any questions about it. its saying that temp is never in the list in my do while loop. thats the problem. dont know whats causing it tho. you shouldnt need any more code. nothing up there connects to anything else Commented Sep 7, 2013 at 6:06

3 Answers 3

3

Your equals is perfectly fine. Looks like a logical mistake to me:

 for (int i = 1; i <= 20; i++) {
        do {
            temp = new President(i, rand(20));
            System.out.println(cards.contains(temp));
        } while (cards.contains(temp));
        cards.add(temp);
        System.out.println(cards.size());
    }

This will generate 20/4000 possible combinations. If you want (1,1)(2,2)....(20,20), your current loop may generate (1,11)(2,13)...(20,5).

Also, cards.contains(temp) will never be true, because i is changed each time.

I am still not sure what exactly are you trying to do? If its just shuffle the deck @PaulBellora's answer seems correct.

Sign up to request clarification or add additional context in comments.

Comments

2

Rather than going about it this way, I recommend populating the deck in order, then shuffling it:

cards = new ArrayList<President>(20);
for (int i = 1; i <= 20; i++) {
    cards.add(new President(i, i));
}
Collections.shuffle(cards);

From the Collections.shuffle documentation:

Randomly permutes the specified list using a default source of randomness. All permutations occur with approximately equal likelihood.

5 Comments

its not looping forever. its running exactly 20 times (once for each loop). it is while cards DOES contain temp
My mistake. Please see my updated answer - does that work for you?
What does it shuffle? Does it just always shuffle the first variable?
I ended up having to not use this. It shuffled the order of my cards which broke my code down the road. i only wanted presidents shuffled. thanks though
No problem - you should accept rocketboy's answer instead! But do note that you could just remove the card field from President since the position in the list already represents a card's order.
2

cards.contains(temp) is never "true" in the do-while loop becaues every President have a other card number.

1 Comment

@Evorlor but you also compare the card number which is i

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.