• Post Reply Bookmark Topic Watch Topic
  • New Topic

Load deck of cards  RSS feed

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

I need to load a deck of 52 cards.
What I did so far:


 

It's giving me an error as "no suitable constructor found for Card (int, int)"
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kristina Hawkins wrote:
It's giving me an error as "no suitable constructor found for Card (int, int)"


Well, does your Card class have a constructor that takes to int parameters?

Henry
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it takes only one int and two Strings
 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless it is very long it helps if you post the complete set of code so that if needed we could compile and run it.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kristina Hawkins wrote:No, it takes only one int and two Strings


So, obviously, that is why that part of your loadDeck() method does not compile.

Henry
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Line 67 will constantly reset your deck index.
Line 68 deck[i] is a Card, you are trying to assign a String to it. You need to assign a new Card(...).
 
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kristina Hawkins wrote:

Override toString() method in the Card class, so the printing of the card would be more elegant.
 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Line 67 will constantly reset your deck index.
Line 68 deck[i] is a Card, you are trying to assign a String to it. You need to assign a new Card(...).


Correction:
Ah, deck[] is an array of Strings so line 68 would work, however, in your constructor you are allocating a "types" array with space for 52 cards. "deck" on line 62 is local to your loadDeck() method and won't be visible anywhere else while "types" is accessible anywhere in your class.

"types" is a lousy name. Probably should be "cards".
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I changed method to this as I assume I need to fill up existing types[], but the problem here is that constructor in Card class takes one int, but I have two now as I have two arrays
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
constructor in Card class takes String, String, int, but here all I have is just two arrays, not sure how to do that
 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kristina Hawkins wrote:I changed method to this as I assume I need to fill up existing types[], but the problem here is that constructor in Card class takes one int, but I have two now as I have two arrays

This is your Card constructor
You have several problems with your call on line 8:
  • You are passing suit and description in the wrong order
  • You are passing suit and description as an int whereas the constructor is expecting Strings.
  • You are not passing a "value".


  •  
    Carey Brown
    Saloon Keeper
    Posts: 3330
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    With these two constants you can convert from an int to a suit or description.
     
    Carey Brown
    Saloon Keeper
    Posts: 3330
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    Also, these two arrays are constants and should, by convention, be named with all upper case characters. I.e. SUITS and DESCRIPTION.

    You might consider moving these to your Card class.
     
    Carey Brown
    Saloon Keeper
    Posts: 3330
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Kristina Hawkins wrote:constructor in Card class takes String, String, int, but here all I have is just two arrays, not sure how to do that

    you should probably change your Card constructor to take int's for suit and description. The only place you need Strings is when you go to display a card and you can easily convert an int to a String using your constant arrays. An overridden toString() method would be the perfect place for the conversion to take place.
     
    Kristina Hawkins
    Ranch Hand
    Posts: 60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you, I did this:




    I need to ask instructor about value
     
    Kristina Hawkins
    Ranch Hand
    Posts: 60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Another question is why when I try to print out cards, it always gives me 47 instead of 52
     
    Carey Brown
    Saloon Keeper
    Posts: 3330
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Neither Card nor Deck has a main() method. How are you running this? What does your main() look like?
     
    Kristina Hawkins
    Ranch Hand
    Posts: 60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This is my main(..)

     
    Kristina Hawkins
    Ranch Hand
    Posts: 60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    For some reason my code missing first five cards and the rest is fine
     
    Carey Brown
    Saloon Keeper
    Posts: 3330
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Kristina Hawkins wrote:Thank you, I did this:

    You need "i++" after line 8.
     
    Kristina Hawkins
    Ranch Hand
    Posts: 60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Everyone,

    I have the last questions, why when I try to print out all cards from the deck, I don't get first five cards back and I'm not sure why.
    Here is my code:


     
    Carey Brown
    Saloon Keeper
    Posts: 3330
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your takeACard() needs some work. For instance:

    should be

    I don't see anything in the code you posted that would cause the first 5 cards to go missing. Presumably they have been set to null somehow but I'd need to see your other code for clues to that.
     
    Kristina Hawkins
    Ranch Hand
    Posts: 60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This is my Card class, I don't see anything here.

     
    Kristina Hawkins
    Ranch Hand
    Posts: 60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    and this is my main (..)


     
    Carey Brown
    Saloon Keeper
    Posts: 3330
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This is what I get when I run your code
     
    Kristina Hawkins
    Ranch Hand
    Posts: 60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This is what I get (no matter if I use driver class or create object in Deck class):

     
    Kristina Hawkins
    Ranch Hand
    Posts: 60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm using BlueJ
     
    Carey Brown
    Saloon Keeper
    Posts: 3330
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm using your TestDeck.main() which doesn't have a showDeck() after the loadDeck().
     
    Kristina Hawkins
    Ranch Hand
    Posts: 60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I don't need to use showDeck(), because I have out print method in loadDeck()
     
    Carey Brown
    Saloon Keeper
    Posts: 3330
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I did add this to my copy of your Card class


    This allowed me to simplify showDeck() to


     
    Kristina Hawkins
    Ranch Hand
    Posts: 60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    but the first five cards are still missing, right?
    It is just so weird, I don't see any reason, why it would be
     
    Carey Brown
    Saloon Keeper
    Posts: 3330
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Kristina Hawkins wrote:I'm using BlueJ

    Is it possible that your BlueJ window truncates console output to 47 lines? (dumb question but I had to ask)
     
    Kristina Hawkins
    Ranch Hand
    Posts: 60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hmm, I'm not sure, but if it was, it would cut the last five cards, not the first I think
     
    Carey Brown
    Saloon Keeper
    Posts: 3330
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Windows tend to scroll up as new text is added to the bottom. The top gets cut off if there's not enough room.
     
    Kristina Hawkins
    Ranch Hand
    Posts: 60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Carey,

    you were right

    Thank You!!
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!