Print Page Print Page

CH12 – Arrays of Cards

  1. Create a new class named CardDriverTwo.
  2. Add a main method with the following code.
public class CardDriverTwo {
  public static void main(String[] args){
    Card[] cards = new Card[52];
    int index = 0;
    for(int suit = 0; suit < 4; suit++)
    {
      for(int rank=1; rank <= 13; rank++)
      {
        cards[index] = new Card(rank,suit);
        index++;
      }
    }
    System.out.println(cards[0]);
    System.out.println(cards[51]);
  }
}
  1. Compile and run the program.
$ javac CardDriverTwo.java
$ java CardDriverTwo
Ace of Clubs
King of Spades