-2

So this is my main class: It creates a CradStack Object and uses the mixCards method, which you will see in the next class block.

package BlackJack;


public class BlackJack {

public static void main(String[] args) {
    CardStack cs = new CardStack();
    cs.mixCards();
    //cs.pop();
}

public BlackJack() {

}

public void calculateHand() {

}

public void compareHands() {

}


}

This is the CardStack class: It extends Stack and till now has only the method mix Cards which fills the Stack with 312 Cards (6decks à 13 types à 4 suites and than mixes it with the Collections.shuffle method. This should work.

package BlackJack;

import java.util.Collections;
import java.util.Stack;


public class CardStack extends Stack {

public CardStack() {

}



public void mixCards() {
    Stack st = new Stack();

    for (int i = 0; i < 6; i++) {                                           
        for (int k = 0; k < 13; k++) {                                      
            for (int l = 0; l < 4; l++) {                                   
                Card cd = new Card();
                cd.setValue(k);
                cd.setColor(l);
                st.push(cd);
            }
        }
    }
    Collections.shuffle(st);
}


}

This is the Card Class: I will implement the suite/color as an enum later on

package BlackJack;

public class Card {

private int value;
private String color;
//enum Suit { HEARTS, DIAMONDS, CLUBS, SPADES };

public void setValue(int value) {
    this.value = value;
}

public void setColor(int l) {
    if (l == 0) {
        this.color="Hearts";
    }
    if (l == 1) {
        this.color="Clubs";
    }
    if (l == 2) {
        this.color="Diamonds";
    }
    if (l == 3) {
        this.color="Spades";
    }
}

public int getValue() {
    return value;
}

public String getColor() {
    return color;
}

}

Can someone explain why i do get an EmptyStackException when i use cs.pop ?

helio
  • 11

1 Answers1

0

You don't need to create separate Stack in mixCards method since your CardStack IS a Stack (since it extends it).
So simply use this.push (you can also skip this. part since it will be added by compiler anyway) to add cards to card stack represented by current instance of CardStack.

BTW. Avoid raw types. If your class is meant to hold cards then you should declare it as

public class CardStack extends Stack<Card> {
   ...
}
Community
  • 1
  • 1
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • If you are referring to post right above then it is an *answer*. What you posted now is a *comment*. Anyway you are welcome :) – Pshemo Dec 15 '15 at 00:13