Question: Just looking for some help with my code. I've got most of it finished, but I'm getting an ArrayIndexOutOfBoundsException at Deck.next(Deck.java:53) when the index should

Just looking for some help with my code. I've got most of it finished, but I'm getting an ArrayIndexOutOfBoundsException at Deck.next(Deck.java:53) when the index should definitely not be out of bounds. Would anyone more experienced be able to offer some insight and see if they can get this code working? I've posted the instructions and my three classes below:

Just looking for some help with my code. I've got most of

CARD

public class Card { private String character; public Card(String deckSuit, String deckValue) { character = (deckSuit + ", " + deckValue); } public Card(String deckCharacter) { character = deckCharacter; }

public String toString() { return "[" + character + "]"; } }

import java.util.Random;

public class Deck

{

Card[] deck;

private int dealt = 0;

public Deck(String[] suit, String[] value)

{

int temp = (suit.length*value.length);

deck= new Card[temp];

int counter = 0;

for (int i = 0; i

{

for (int j = 0; j

{

deck[counter] = new Card(suit[i], value[j]);

counter ++;

}

}

}

DECK

public Deck(String[] character)

{

int temp = (character.length);

deck= new Card[temp];

int counter = 0;

for (int i = 0; i

{

deck[counter] = new Card(character[i]);

}

}

public Deck()

{

int temp = (52);

deck= new Card[temp];

String[] arr1 = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};

String[] arr2 = {"\u2665", "\u2666", "\u2660", "\u2663"};

int counter = 0;

for (int i = 0; i

{

for (int j = 0; j

{

deck[counter] = new Card(arr2[i], arr1[j]);

counter ++;

}

}

}

public Card next()

{

dealt++;

return deck[dealt];

}

public boolean hasNext()

{

if (next() != null)

{

return true;

}else{

return false;

}

}

public int remaining()

{

return (deck.length-dealt);

}

public int size()

{

return deck.length;

}

public void shuffle()

{

dealt=-1;

Random rand = new Random();

for (int i=0; i

int randomPosition = rand.nextInt(deck.length);

Card temp = deck[i];

deck[i] = deck[randomPosition];

deck[randomPosition] = temp;

}

}

}

TEST

class Test { public static void main(String[] args) { String[] array1 = {"A","B","C"}; String[] array2 = {"1","2"}; Deck cards = new Deck(array1, array2); Card curCard = cards.next(); String cardsString = curCard.toString(); while (cards.hasNext()) { curCard = cards.next(); cardsString = cardsString + " " + curCard.toString(); } System.out.println(cardsString.equals("[A,1] [A,2] [B,1] [B,2] [C,1] [C,2]")); } }

Module 2 Program - Deck of Cards In this project You will write two classes Deck and card . The interface (ie, list of methods) will be provided for you for Deck but it will be up to you to design the interface for Card. Deck Deck should have thre constructors. The constructor with two parameters should accept two string[] parameters and create enough cards so that every pair of strings one from the first array and one from the second array - is represented exactly ance. The constructor with one parameter should accept one stringt] parameter and create one card for each string in the array. Finally, the no-argument constructon should call the two argument constructor with the first array of strings being A 2 3 4 5 6 7 8 9 10 Q "K and the second array being either "w "4or H D S C' it's up to you to choose which). You can use lu2665, u2666, lu2660, and u2663 in your strings to represent these four unicode characters (eg, u2665" is a String literal with one heart character in it) next should take no parameters and should return the next undealt Card object in the deck. If there are no cards left to deal, an 1 1gal state ceptian should be thrown. hasNext should take no parameters and retum true if a call to next would succeed. remaining should take no parameters and retum how many cards are left to be dealt. size should take no parameters and return how many cards are in the Deck. shuffle should take no parameters, reset the number of cards dealt to zero, and randomly reorder the cards in the deck. Note that the Deck should be unshuffled and ready to deal when created and the initial order of the cards in the deck is specified by the constructor. A deck constructed as (A"."B", C"T. "1 deck order of A1, A2, B1, B2, C1, C2, ready to deal A1 2") should result in a Deck should use an array to hold its cards and the array should be exactly the same size as the number of cards in the deck. You should follow the steps for designing a class seen during lecture. You are free to design the class with any methods you see fit, but that everything else in the class should be marked private. For testing purposes Card must have a toString that returns cards as "[The Chariot" (ie, open square brace, string representing card, close square brace, with no added whitespace). Furthermore, two-valued cards should have a comma between its two values like "[10,9 You should write Card first and test it independently of Deck to make sure it works as you'd like. Once you believe it works correctly, move on to Deck. Example The following code should print truc when run. String ] arrayl"A"B, C" String [] array2"1",2" teck cardsnew Dack (arrayi, array2) St.ring cring -rcard.LoSt.ringY while (cards.hasNexO curcard-cards.next caraestring - cardsString ""curcard.toszring

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!