title: Java - How to create a deck of cards (with a focus on static arrays)? - AP Computer Science Project
body: <p>this is my first question ever. Just to clarify, I did check to see if there were any questions that could have helped me before asking this. Apologies in advance if I do anything incorrectly, I'm new.</p>
  
  <p>Anyways, for my AP CS class I must make a deck a cards and print it out in the text window. I believe I am very close to being finished. As the title says, in general how do I create a deck of cards with a focus on static arrays? But furthermore (After the error I ask about below is solved), when looking at the code I have produced so far, is that the correct way to do it?</p>
  
  <p>Here is the given code (meaning this can not be changed):</p>
  
  <pre><code>public class Card
  {
     private String suit;
     private String rank;
     private int value;
  
     public Card(String s, String r, int v)
     {
        suit = s;
        rank = r;
        value = v;
     }
  
     public String getSuit()       { return suit; }
     public String getRank()       { return rank; }
     public int getValue()         { return value; }
  
     public void setSuit(String s) { suit = s; }
     public void setRank(String r) { rank = r; }
     public void setValue(int v)   { value = v; } 
  
     public String toString()
     {
        return "[" + suit + ", " + rank + ", " + value + "]";
     }
  }
  </code></pre>
  
  <p>And here is the what I have coded so far:</p>
  
  <pre><code>public class Lab11bst
  {
      public static void main(String[] args)
      {
        Deck deck = new Deck();
        System.out.println(deck);
      }
  }
  
  
  class Deck
  {
     private int numberOfCards;
     private Card [] cards;
     private String [] suits = {"Clubs","Diamonds","Hearts","Spades"};
     private String rank;
     private int value;
  
  
     public Deck() // This creates a deck of 52 playing cards.
     {
        numberOfCards = 52;
        cards = new Card[52];
        for ( int suit = 0; suit &lt;= 3; suit++ )
        {
           String [] ranks = {"Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"};
           for ( int rank = 1; rank &lt;= 13; rank++ )
           {
              if (rank == 1)
              {
                 this.rank = "Ace";
              }
              else if (rank == 11)
              {
                 this.rank = "Jack";
              }
              else if (rank == 12)
              {
                 this.rank = "Queen";
              }
              else if (rank == 13)
              {
                 this.rank = "King";
              }
              else
              {
                 this.rank = "" + ranks[rank];
              }
              for ( int value = 1; value &lt;= 10; value++ )
              {
                 if (this.rank == "Ace")
                 {
                    value = 1;
                 }
                 else if (this.rank == "Jack")
                 {
                    value = 10;
                 }
                 else if (this.rank == "Queen")
                 {
                    value = 10;
                 }
                 else if (this.rank == "King")
                 {
                    value = 10;
                 }
                 else
                 {
                    this.value = value;
                 }
                 cards [numberOfCards] = new Card(suits[suit],this.rank,value);
                 numberOfCards ++;
              }
           }
        }
     }
  }
  </code></pre>
  
  <p>Looking at my code, I'm pretty sure there is a better way to get rid of all those if statements and make it all more short and concise. My sort-of third question (which may be necessary/might help to solve before answering the main two questions) is how do I fix this error when I run the program?:</p>
  
  <pre><code>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 52
      at Deck.&lt;init&gt;(Lab11bst.java:89)
      at Lab11bst.main(Lab11bst.java:5)
  </code></pre>
  
  <p>Thank you very much in advance.</p>