Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

These are the error messages I am getting. Can someone please help? java.lang.ArrayIndexOutOfBoundsException: Index 7 out of bounds for length 4 at Card.toString(Card.java:57) at java.base/java.lang.String.valueOf(String.java:2951)

These are the error messages I am getting.

Can someone please help?

java.lang.ArrayIndexOutOfBoundsException: Index 7 out of bounds for length 4 at Card.toString(Card.java:57) at java.base/java.lang.String.valueOf(String.java:2951) at DriverClass.main(DriverClass.java:24) java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 4 at Card.toString(Card.java:57) at java.base/java.lang.String.valueOf(String.java:2951) at DriverClass.main(DriverClass.java:24) java.lang.ArrayIndexOutOfBoundsException: Index 8 out of bounds for length 4 at Card.toString(Card.java:57) at java.base/java.lang.String.valueOf(String.java:2951) at DriverClass.main(DriverClass.java:24)

This is my code for the assignment consisting of 3 classes. Deck of Card class, Driver class, and Card class.

import java.util.*; import java.util.Random;

public class DeckOfCards { private Card deck[]; // array of Card objects private int currentCard; // index of next Card to be dealt private final int NUMBER_OF_CARDS = 51; // constant number of Cards private Random randomNumbers; // random number generator // constructor fills deck of Cards public DeckOfCards() { //String faces[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", // "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; //String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" }; deck = new Card[ NUMBER_OF_CARDS ]; // create array of Card objects currentCard = 0; // set currentCard so first Card dealt is deck[ 0 ] randomNumbers = new Random(); // create random number generator // populate deck with Card objects for ( int count = 0; count < deck.length; count++ ) deck[ count ] = //new Card( faces[ count % 13 ], suits[ count / 13 ] ); new Card( count % 11+1, count/11 +1); } // end DeckOfCards constructor // shuffle deck of Cards with one-pass algorithm public void shuffle() { // after shuffling, dealing should start at deck[ 0 ] again currentCard = 0; // reinitialize currentCard // for each Card, pick another random Card and swap them for ( int first = 0; first < deck.length; first++ ) { // select a random number between 0 and 51 int second = randomNumbers.nextInt( NUMBER_OF_CARDS ); // swap current Card with randomly selected Card Card temp = deck[ first ]; deck[ first ] = deck[ second ]; deck[ second ] = temp; } // end for } // end method shuffle // deal one Card public Card dealCard() { // determine whether Cards remain to be dealt if ( currentCard < deck.length ) return deck[ currentCard++ ]; // return current Card in array else return null; // return null to indicate that all Cards were dealt } // end method dealCard } // end class DeckOfCards

import java.util.Random;

/** * Card Class Standard Playing Card * * @author (Nicholas Murray) * */ public class Card {

// Variable declaration private int face; private int suit; //Global variables/constants public static final String suitLiterals[]={"Diamonds","Clubs","Hearts","Spades"}; public static final String faceLiterals[]={"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};

// Default Constructor public Card(){

Random random= new Random(); // generates a random number with upper limit 13 face=random.nextInt(12); // generates a random number with upper limit 4 suit=random.nextInt(3);

} // Argument constructor public Card(int suitValue, int faceValue){ suit=suitValue; face=faceValue; } // Gets and returns face values of cards int getFace() { return this.face; }

String getFaceText() { return faceLiterals[face]; }

int getSuit() { return this.suit; }

String getSuitText() { return suitLiterals[this.suit = 0]; } // toString method to print the output nicely public String toString() { return faceLiterals[face] + " of "+ suitLiterals[suit]; } // main method below public static void main(String args[]) { //Calling the argument constructor Card card = new Card(0,12); System.out.println(card.getFace()); System.out.println(card.getFaceText()); System.out.println(card.getSuit()); System.out.println(card.getSuitText()); System.out.println(card);

//Default constructor being used to get all the values, and then printing them out! Card card1= new Card(); System.out.println(card1.getFace()); System.out.println(card1.getFaceText()); System.out.println(card1.getSuit()); System.out.println(card1.getSuitText()); System.out.println(card1); } }

import java.util.*; import java.util.Random; import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test;

/** * The test class CardTest. * * @author (your name) * @version (a version number or a date) */ public class DriverClass {

public static void main(String[] args) { DeckOfCards d = new DeckOfCards(); System.out.println(d); // the unshuffled deck d.shuffle(); for (int i = 0; i<52; i++){ System.out.println("Card 1: " + d.dealCard()); } }

}

It appears the error has something to do with this line of code here, but I am not sure.

This is from the Card Class

return faceLiterals[face] + " of "+ suitLiterals[suit];

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 11g SQL

Authors: Jason Price

1st Edition

0071498508, 978-0071498500

More Books

Students also viewed these Databases questions

Question

Choosing Your Topic Researching the Topic

Answered: 1 week ago

Question

The Power of Public Speaking Clarifying the

Answered: 1 week ago