Hi
I created two classes. one Card class which basically specifies what type of data a Card object would have (Suit values and suit numbers). The other one is the Full Deck class which is basically meant to create 52 Card objects (which consist of all 52 cards in a Full Deck). I have used arrays in Full Deck.
BOTH CLASSES COMPILE FINE. WHEN I RUN THE APPLICATION (FULLDECK), it STOPS EXECUTING DUE TO A RUN TIME ERROR! I am a beginner in Java, so I am trying to figure out whats the cause.
Can someone please tell what the cause for the runtime error is and what can be done to correct it?
The Card class is as follows
Code:
//Importing JOptionPane
import javax.swing.JOptionPane;
//Class Declaration
public class Card
{
//Declaration of variables
private String suitValue = new String();
private String rankValue = new String();
//getter method for the Suit
public String getSuit()
{
return suitValue;
}
//getter method for the Rank
public String getRank()
{
return rankValue;
}
//setter method for the Suit
public void setSuit(int suit)
{
switch(suit)
{
case 1: suitValue= "Hearts";
break;
case 2: suitValue="Diamonds";
break;
case 3: suitValue="Spades";
break;
case 4: suitValue="Clubs";
break;
//If this displays, it means there is a bug in this program
default: JOptionPane.showMessageDialog(null, "Hmmm... This seems to be an invalid value");
}
}
//setter method for the Rank
public void setNumbers(int rank)
{
switch(rank)
{
case 1: rankValue="Ace";
break;
case 2: rankValue="2";
break;
case 3: rankValue="3";
break;
case 4: rankValue="4";
break;
case 5: rankValue="5";
break;
case 6: rankValue="6";
break;
case 7: rankValue="7";
break;
case 8: rankValue="8";
break;
case 9: rankValue="9";
break;
case 10: rankValue="10";
break;
case 11: rankValue="Jack";
break;
case 12: rankValue="Queen";
break;
case 13: rankValue="King";
break;
//If this displays, it means there is a bug in this program
default: JOptionPane.showMessageDialog(null, "Hmmm... This seems to be an invalid value");
}
}
}
The FullDeck class is as follows:
Code:
//Importing JOptionPane
import javax.swing.JOptionPane;
//Class Declaration
public class FullDeck
{
//Main Funtion
public static void main(String[] args)
{
//Declaration of variables
int suit, suit2;
String userCard = new String();
String computerCard = new String();
final int CARDS_IN_SUIT = 13;
final int RANDOM_SUIT = 4;
Card[] cardDeck = new Card[52];
int x;
for(x=0;x<52;++x)
{
for(suit=1;suit<=13;++suit)
{
cardDeck[x].setSuit(suit);
for(int value=1;value<=CARDS_IN_SUIT;++value)
cardDeck[x].setNumbers(suit);
}
}
for(int y=0;y<52;++y)
System.out.println(cardDeck[y].getSuit() + cardDeck[y].getRank());
}
}