Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructor and class problem  RSS feed

 
Sam Thompson
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone:

I have written two separate classes. One for creating an object for a playing card, and the other for creating a "deck" of cards. So far everything seems to be okay- BUT I am having an issue with the constructor. How i can tell is that when I have the program print out each element, it turns out that it put DUPLICATES of the same element!!

Can anyone tell where I went wrong? Here is my code below:



and...



I went wrong somewhere. Has to be something simple I think. Because these classes are very simple.

Thanks for your help in advance everyone.

S.T.
 
Mack Wilmot
Ranch Hand
Posts: 88
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because you only have one card and you are making 52 references to the same card.
 
Sam Thompson
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I create 52 cards in the deck without explicitly declaring them outside the arraylist?
 
Mack Wilmot
Ranch Hand
Posts: 88
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Thompson wrote:How would I create 52 cards in the deck without explicitly declaring them outside the arraylist?


You assign an ArrayList cell to a new Card Object in a loop. You don't need a variable name reference before inserting it.
 
Sam Thompson
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. How would the code look for a cell though?

So far, I've only heard cells being used in spreadsheets or database programming.

I'm sorry for sounding ignorant at times but I'm still an intermediate programmer haha.

S.T.
 
Mack Wilmot
Ranch Hand
Posts: 88
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every time you use deck.add() you are adding something to the next cell in the ArrayList. You are already adding the same card 52 times to the ArrayList, so you know how to populate the ArrayList in a loop.

Hint: deck.add(new Card())
 
Sam Thompson
Ranch Hand
Posts: 93
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohhhhhhhh.... Now I get what you're saying now.

And just to clarify, to set up each card's rank and suite, you would use a loop with something like this:

deck.get(i).ranks=I;
deck.get(I).suites=suite[j];

Correct?
 
Mack Wilmot
Ranch Hand
Posts: 88
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup!

You should use getter and setter methods when accessing the suites and ranks fields, but it will still work accessing the fields directly. It is just not good object oriented programming to access fields directly is all.

Also, your constructor in Card() is useless, but you could write the constructor so that you can set the suites and ranks fields on creation of the object. Also the fields should be called suite and rank since you don't have multiple suites and ranks for each card right?
 
Sam Thompson
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right.

I should be good now.

Mack thanks a bunch for your help.

Sam
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!