-1

there is already a class called Card.java with 52 cards. And in the Deck.java i have to write a constructor to initialize the 52 cards in a row with suite and value. i wrote the following code but it failed the public test..Can anybody help me out?

public class Deck {
    private Card[] cards;
    private final int DECK_SIZE=52;

    public Deck(){
        this.cards=new Card[DECK_SIZE];
        int index = 0;
        for (int suit = 0; suit <= 3; suit++) {
            for (int value = 1; value <= 13; value++) {
                this.cards[index] = new Card (suit, value);
                index++;
            }
        }
    }
    public Deck(Deck other) {
        this.cards= new Card[DECK_SIZE];
        for(int i=1;i<=DECK_SIZE;i++){
            this.cards[i]= other.cards[i];
}
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
javaLearner
  • 25
  • 1
  • 7
  • Public test? Is there an error message? –  Apr 14 '11 at 00:17
  • 2
    It looks good, from far. What does "failed the public test" mean? – Paŭlo Ebermann Apr 14 '11 at 00:17
  • 1
    Are you assured that for the second constructor, the given deck's size is the same as yours? –  Apr 14 '11 at 00:19
  • Looks good to me. What does your test look like, and are you sure the Card class is not at fault somehow? – sverre Apr 14 '11 at 00:20
  • Seems that it's homework, no? and BTW, this error can be easily found if you run your application at least 1 time and try to actually **read** exception that was thrown, because of this **-1** from me. – tenshi Apr 14 '11 at 00:37

3 Answers3

1

In your second constructor you iterating from 1 to DECK_SIZE (1..52), but you shoud iterate starting from 0:

    for(int i=0; i<DECK_SIZE;i++){
        this.cards[i]= other.cards[i];
    }

Your code should throw ArrayIndexOutOfBoundsException.

tenshi
  • 26,268
  • 8
  • 76
  • 90
  • @javaLearner: All java arrays are starting at index `0`. You can find more info here: http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – tenshi Apr 14 '11 at 12:01
0

Your for loop,

for(int i=1;i<=DECK_SIZE;i++){

is incorrect, it should read

for(int i=0;i <DECK_SIZE;i++){

An array of size 52 only has indexes from 0 to 51.

ThomasRS
  • 8,215
  • 5
  • 33
  • 48
  • its not actually..since i intialize to 1, its gonna go from 1 to 52. – javaLearner Apr 14 '11 at 05:11
  • All arrays start at 0, and go to index array.length - 1 inclusive. The array 'does not care' whether you init i to 1 or not, it just accepts indexes 0..51 and your last iteration has index == 52. – ThomasRS Apr 14 '11 at 11:29
0

The Java tutorials have a good example of a way to create a deck using Enum values: http://download.oracle.com/javase/1.5.0/docs/guide/language/enums.html

The conventional way of doing arrays in java is to for (int i = 0; i < XXX; i++). You should change your Suit loop then to be <4 not <=3, and your Deck look in the second constructor to be for (int i = 0; i < DECK_SIZE; i++). Arrays of length n start at index 0 and finish at index n-1.

Can a deck have less than 52 cards? In that case, your second constructor might throw an exception of some kind; as it is it will give an ArrayIndexOutOfBoundsException.

Trasvi
  • 1,207
  • 1
  • 15
  • 23