I have written 3 classes. One is a Card class, representing a playing card:
public class Card
{
int theRank, theSuit, theCard;
public Card(int suit, int rank)
{
theRank = rank;
theSuit = suit;
}
public String getRank()
{
String rankString = "";
switch (theRank)
{
case 1:
rankString = "Ace";
case 2:
rankString = "2";
case 3:
rankString = "3";
case 4:
rankString = "4";
case 5:
rankString = "5";
case 6:
rankString = "6";
case 7:
rankString = "7";
case 8:
rankString = "8";
case 9:
rankString = "9";
case 10:
rankString = "10";
case 11:
rankString = "Jack";
case 12:
rankString = "Queen";
case 13:
rankString = "King";
}
return rankString;
}
public String getSuit()
{
String suitString = "";
switch(theSuit)
{
case 1:
suitString = "Diamonds";
case 2:
suitString = "Hearts";
case 3:
suitString = "Clubs";
case 4:
suitString = "Spades";
}
return suitString;
}
}
The other is a class Deck, that represents an arrayList of Card objects:
public class Deck
{
public ArrayList<Card> loadDeck(ArrayList<Card> deck)
{
for (int suit = 1; suit <= 4; suit++)
{
for (int rank = 1; rank <= 13; rank++)
{
deck.add(new Card(suit, rank));
}
}
return deck;
}
public void printDeck(ArrayList<Card> deck)
{
for (int i = 0; i <= deck.size(); i++)
{
Card temp = deck.get(i);
String rank = temp.getRank();
String suit = temp.getSuit();
System.out.println(rank + " of " + suit);
}
}
public void shuffleDeck(ArrayList<Card> deck)
{
Card temp;
int index;
Random rand = new Random();
for (int i = deck.size() - 1; i > 0; i--)
{
index = rand.nextInt(i + 1);
temp = deck.get(index);
deck.add(index, deck.get(i));
temp = deck.get(i);
}
}
public ArrayList dealHand(ArrayList<Card> deck)
{
Random rand = new Random();
ArrayList<Card> hand = new ArrayList<Card>(8);
for (int i = 0; i <= 6; i++)
{
int suit = rand.nextInt(3);
int rank = rand.nextInt(51);
Card temp = new Card(suit, rank);
hand.add(i, temp);
deck.remove(temp);
}
return hand;
}
public void printHand(ArrayList<Card> hand)
{
for (int i = 0; i <= 6; i++)
{
Card temp = hand.get(0);
System.out.println(temp.getRank() + " of " + temp.getSuit());
}
}
}
I then have a class GoFish that contains the main method:
import java.util.ArrayList;
public class GoFish
{
public static void main(String[] args)
{
Deck testDeck = new Deck();
ArrayList<Card> deck = new ArrayList<Card>();
deck = testDeck.loadDeck(deck);
testDeck.printDeck(deck);
}
}
The output I wanted was the rank and suit of every card in a standard 52 deck of playing cares. But there is a problem with either the filling of the arrayList or the printing of the list. Any suggestions?