Question
Hi there, I need help fixing my code. I am receiving the error Exception in thread main java.lang.ArrayIndexOutOfBoundsException: Index 62 out of bounds for length
Hi there, I need help fixing my code. I am receiving the error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 62 out of bounds for length 60
at DisplayHand.main(DisplayHand.java:31)
"
The goal of this milestone is to design software that represents the objects needed for the UNO card game and to instantiate and test these objects.
I need to write the code for the object classes needed for my UNO game. I need a minimum of 3 classes: a. A Card class (models an individual UNO card) b. A Hand class (models a player's hand) c. A Deck class (models the entire UNO deck)
Then I need to test my program by writing a console application (a driver program) that creates a deck of UNO cards, deals the cards to two or more players, and displays the contents of each player's hand.
I need to Have at least three UNO classes (Card, Deck and Hand class)and a Driver/Test class (with main() method)
- Define a Card class
- Define a Deck class
Add 108 cards (Card objects) to a declared Deck object
Deck class will require a Card Array or (preferably) ArrayList property
3. Define a Hand Class
Declare at leastTWOHand objects and assign eight cardsfrom the Deck objectto each Hand object.
Hand class will also require an Array or ArrayList of Card property
4.Display all cards in each Hand object
Here is my logic:
public class DisplayHand {
public static void main(String[] args) {
String[] Values = {
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"Wild", "Reverse", "Skip", "Draw2", "Draw4"
};
String[] Color = {
"Red", "Green", "Yellow", "Blue"
};
//initialize the deck
int n = Color.length * Values.length;
String[] deck = new String[n];
for (int i = 0; i < Values.length; i++) {
for (int j = 0; j < Color.length; j++) {
deck[Color.length * i + j] = Color[j] + " " + Values[i];
}
}
//shuffle the deck
for (int i = 0; i < n; i++) {
int r = i + (int) (Math.random() * (n-1)); //shuffle and deal class for the game
String temp = deck[r];
deck[r] = deck[i];
deck[i] = temp;
}
//print shuffled deck
for (int i = 0; i < 2; i++) {
System.out.println(" UNO Player " + (i + 1 +" "));
for (int j = 0; j < 7; j++)
System.out.println(deck[i + j * 2] + " (Card " + (i + j * 2) + ")");
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started