Anyone have any ideas why I having this error //there is more code on top with e
ID: 642133 • Letter: A
Question
Anyone have any ideas why I having this error
//there is more code on top with enum
//Deck
public class Deck {
private Random random;
private final int NUMBER_OF_CARDS = 51;
private Card[] cards;
private CardSuit [] suits;
private CardValue [] faces;
private int index;
public Deck()
{
suits = CardSuit.values();
faces = CardValue.values();
index = 0;
cards = new Card[NUMBER_OF_CARDS];
for(CardValue f: faces)
{
for(CardSuit s : suits)
{
cards[index] = new Card(s, f); //<- 8
index++;
}
}
random = new Random();
}
// draw method returns a random card
public Card draw()
{
int s = random.nextInt(suits.length);
int f = random.nextInt(faces.length);
Card card = new Card(suits[s], faces[f]);
return card;
}
}
//MAIN
static final int HANDS = 52;
public static void main(String[] args) {
Deck Player1 = new Deck(); //<- 27
Deck player2 = new Deck();
// int player1Score = 0, player2Score = 0;
for (int i = 0; i < 5; i++){
Card c1 = Player1.draw();
Card c2 = Player1.draw();
System.out.println("Card1: " + c1);
System.out.println("Card2: " + c2);
if(c1.winner(c2))
System.out.println("Winner: " + c1);
else
System.out.println("Winner: " + c2);
System.out.println();
}
}
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 51
at card.Deck.<init>(Deck.java:27)
at card.MainDriver.main(MainDriver.java:8)
the error are in the arrow
Explanation / Answer
Nothing as far as I can tell, this is the Card class.
Java Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Card
{
private String face; // face of card
private String suit; // suit of card
// two-argument constructor initializes card's face and suit
public Card( String cardFace, String cardSuit )
{
face = cardFace; // initialize face of card
suit = cardSuit; // initialize suit of card
} // end two-argument Card constructor
// return card face
public String getFace()
{
return face;
} // end method getFace
// return card suit
public String getSuit()
{
return suit;
} // end method getSuit
// return String representation of Card
public String toString()
{
return face + " of " + suit;
} // end method toString
} // end class Card
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Card
{
private String face; // face of card
private String suit; // suit of card
// two-argument constructor initializes card's face and suit
public Card( String cardFace, String cardSuit )
{
face = cardFace; // initialize face of card
suit = cardSuit; // initialize suit of card
} // end two-argument Card constructor
// return card face
public String getFace()
{
return face;
} // end method getFace
// return card suit
public String getSuit()
{
return suit;
} // end method getSuit
// return String representation of Card
public String toString()
{
return face + " of " + suit;
} // end method toString
} // end class Card
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.