Arrays of Objects Questions- Coding A Card Game 

Suppose you wish to store information about cards in a Card deck. Each card has a value from 1 to 13 and a Suit (one of hearts, diamonds, spades, clubs).

 

public class Card

{

int value;

String suit;

}

The card deck will be stored in an array of 52 card objects. You will also have a global variable to store the index of the top of your card deck.

 

static int topOfDeck; //Global variable - Stores which card we are at.

public static void main (String args [])

{

Card Deck [] = new Card [52];

for (int i = 0 ; i < 52 ; i++)

{

Deck [i] = new Card ();

}

topOfDeck = 51; //The end of the array is the top of the deck

}

1. Write a method to print out one card. The output will look like this when it is finished:Diamonds 3 

public static void PrintOneCard (Card one)

2. Write a method to print out the entire deck. Call the method you wrote in the previous question to assist you. The output is shown in the following question. 

public static void PrintDeck (Card Deck [])

3. Write a method to initialize the deck using for loops. The card deck will hold the following values when InitializeDeck is finished. 

Hearts 1

Hearts 2

Hearts 3

Hearts 4

Hearts 5

Hearts 6

Hearts 7

Hearts 8

Hearts 9

Hearts 10

Hearts 11

Hearts 12

Hearts 13

Clubs 1

Clubs 2

Clubs 3

Clubs 4

Clubs 5

Clubs 6

Clubs 7

Clubs 8

Clubs 9

Clubs 10

Clubs 11

Clubs 12

Clubs 13

Spades 1

Spades 2

Spades 3

Spades 4

Spades 5

Spades 6

Spades 7

Spades 8

Spades 9

Spades 10

Spades 11

Spades 12

Spades 13

Diamonds 1

Diamonds 2

Diamonds 3

Diamonds 4

Diamonds 5

Diamonds 6

Diamonds 7

Diamonds 8

Diamonds 9

Diamonds 10

Diamonds 11

Diamonds 12

Diamonds 13

 

public static void InitializeDeck (Card Deck [])

 

4. Consider the following method:

public static Card DealOne (Card Deck [])

{

if (topOfDeck >= 0)

{ //if any cards left, return one & decrement the topOfDeck

topOfDeck -= 1;

return Deck [topOfDeck + 1];

}

else

{//if no cards left, return a 0 card and print error

System.out.println ("The card deck is empty");

Card temp = new Card ();

temp.value = -1;

temp.suit = "NONE";

return temp;

}

}

(a)Is this method used in an Applet or a Void Main program? How can you tell?

(b)What type of parameter used in this method?

(c)What does this method return?

(d)What is the name of the global variable used in this method?

(e)Name two distinct white box test cases you would use in this method

(f)Write a pre condition for this method.

(g)Write a post condition for this method.

 

5. Consider the following method: 

public static void Shuffle (Card Deck [])

{

//reset the top of the deck

topOfDeck = 51;

Card temp = new Card ();

int randomCard1, randomCard2;

for (int i = 0 ; i < 52 ; i++)

{//randomly swap pairs of cards in the deck

randomCard1 = (int) (Math.round (Math.random () * 51));

randomCard2 = (int) (Math.round (Math.random () * 51));

temp = Deck [randomCard1];

Deck [randomCard1] = Deck [randomCard2];

Deck [randomCard2] = temp;

}

}

(a)What does this method return?

(b)Why would testing this method be difficult using white and black box testing?

(c)Write a pre condition for this method.

(d)Write a post condition for this method.

 

6. Suppose that you wanted to deal each player of your game a hand of five cards. (10 marks)

(a)How would you declare and construct a Hand that had 5 cards in it?

(b)How would you construct each Card in the Hand?

(c)Write a method to find the total value of a Hand. Suits do not matter, return a total of the values of each card in the hand.

public static int TotalValue (Card Hand[])

(d)Write a method to get five cards from the Card Deck and add them to the Hand. Make sure to use the DealOne method from question 4 to help you.

public static void DealHand (Card Hand[], Card Deck[])