109k views
22 votes
A Card class has been defined with the following data fields. Notice that the rank of a Card only includes the values from Ace - 10 (face cards have been removed):

class Card {

private int rank; // values Ace (1) to 10
private int suit; // club - 0, diamond - 1, heart - 2, spade - 3

public Card(int rank, int suit) {
this.rank = rank;
this.suit = suit;
}
}

A deck of cards has been defined with the following array:

Card[] cards = new Card[40];

Which of the following for loops will populate cards so there is a Card object of each suit and rank (e.g: an ace of clubs, and ace of diamonds, an ace of hearts, an ace of spades, a 1 of clubs, etc)?

Note: This question is best answered after completion of the programming practice activity for this section.

a
int index = 0;
for (int suit = 1; suit < = 10; suit++) {
for (int rank = 0; rank < = 3; rank++) {
cards[index] = new Card (rank, suit);
index++;
}
}

b
int index = 0;
for (int suit = 0; suit < = 4; suit++) {
for (int rank = 0; rank < = 10; rank++) {
cards[index] = new Card (rank, suit);
index++;
}
}

c
int index = 0;
for (int rank = 1; rank <= 10; rank++) {
for (int suit = 0; suit <= 3; suit++) {
cards[index] = new Card (rank, suit);
index++;
}

d
int index = 0;
for (int suit = 0; suit < = 3; suit++) {
for (int rank = 1; rank < 10; rank++) {
cards[index] = new Card (rank, suit);
index++;
}
}

User Ryan Rich
by
4.2k points

1 Answer

14 votes

Answer: b

Explanation: i did this one!!!!!!!!!!

User RickMeasham
by
3.5k points