Consider a program that simulates a card game, with multiple player hands and a
ID: 3535216 • Letter: C
Question
Consider a program that simulates a card game, with multiple player hands and a deck to draw from. Each hand can use an array to represent the cards it contains; sometimes it is helpful to also declare an additional variable for each hand (or deck) indicating exactly how many cards are present.
1) Describe a simple function that would manipulate both the array representing a hand and the number indicating the size of the hand.
2) Describe a simple function that might be able to manipulate the array without referring to the hand size variable at all.
3) Generally, if the array was passed as a parameter to a function, how often would the hand size be included as a parameter?
Explanation / Answer
package javacards;
import java.util.Random;
public class Deck {
private Card[] cards;
int i;
Deck()
{
i=51;
cards = new Card[52];
int x=0;
for (int a=0; a<=3; a++)
{
for (int b=0; b<=12; b++)
{
cards[x] = new Card(a,b);
x++;
}
}
}
public Card drawFromDeck()
{
Random generator = new Random();
int index=0;
do {
index = generator.nextInt( 52 );
} while (cards[index] == null);
i--;
Card temp = cards[index];
cards[index]= null;
return temp;
}
public int getTotalCards()
{
return i;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.