-3

My textbook gave me this code saying that it'll create a new set of 52 cards. I don't really understand what it does, because the methods I see on google is very different from this. I'm confused about what the "index" variable does and how can I print this method? I do have a printdeck method but how would I call that method if this method doesn't return any number?

 public static void buildDeck () {
    Card[] deck = new Card [52];
 int index = 0;
 for (int suit = 0; suit <=3; suit++) {
     for (int rank = 1; rank <= 13; rank++) {
         deck[index] = new Card (suit, rank);
         index++;
     }

}

//here is my printDeck method
public static void printCard (Card c) {
    String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
    String [] ranks = { "nart", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "Queen", "king" };
    System.out.println (ranks[c.rank] + " of " + suits[c.suit]);
}
public static void printDeck (Card[] deck) {
    for (int i = 0; i< deck.length; i++) {
        printCard (deck[i]);
    }
}

I'll appreciate any help, thanks!

  • 2
    Shouldn't your textbook also explain what the code does? Also it's a bit unclear where exactly your issue is - `index` is just an `int` variable (that is used to access elements in the array) – UnholySheep Nov 19 '17 at 12:26
  • 1
    That buildDeck method can't be right. As soon as the method is finished, the deck variable will be out of scope and inaccessible. – Herb Nov 19 '17 at 12:33
  • 2
    Build a private deck and immediately throw it away??? Your **textbook** gave you that amateur-hour garbage? I think you need a better textbook! – Kevin Anderson Nov 19 '17 at 12:38
  • @UnholySheep the textbook basically just says this is the method I should use, it doesn't really say more – celina tala Nov 19 '17 at 13:02
  • @celinatala, well, if it says you **should** use this, then I'd be pretty safe to say that you should ignore what that book says. – M. Prokhorov Nov 19 '17 at 13:59

3 Answers3

0

The index variable, like the name states, is used as a counting index for the deck.

The printDeck(..) method should print the whole to the console by calling the printCard(..) method and that method in return calling System.out.println

Rainer Wei
  • 1
  • 1
  • 4
0
  • The method printDeck doesn not really print something to the console. It takes an array of cards and calls for each card the method printCard (supplying the current card as argument).
  • The method printCard takes - as mentioned before - a single card and prints its rank /suite to the console.

    public static void printCard (Card c) { System.out.println (ranks[c.rank] + " of " + suits[c.suit]); }

The index variable is used e.g. to initialize the deck of cards.

  • Initially you have 52 "blank" cards in the array, or better said each entry in the array is null.
  • In each iteration you are creating a new Card and have to assign it to a position in the deck. This is done via the index and by incrementing it with each iteration.
Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33
  • When I print the buildDeck method, why does it only print the "club" suits? And there is an error in between 2 of clubs and 3 of clubs saying "Exception in thread "main" java.lang.NullPointerException"? – celina tala Nov 19 '17 at 13:07
  • I suggest that you use a debugger to figure out the cause of the NullPointerException. The exception could also explain why you only the the clubs. – Stefan Freitag Nov 19 '17 at 18:56
0

You should return card[] deck from your buildDeck method and then pass it to print method.

Vicky Desai
  • 223
  • 1
  • 3
  • 9