• Post Reply Bookmark Topic Watch Topic
  • New Topic

Card Class and Drivers  RSS feed

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Card Class:




Driver Class:

package Week5;



Questions:
1. The output keeps on returning "Joker" every time I execute, why? It seems to me that I want to generate random suits and values for the card and then go from there, but that seems not to be working.
2. Instead of returning a String, I want the accessors to return a boolean to indicate to the driver class that it was successful/failed. Can someone help me on this? What must I do? Should I initialise the Values of the card in an array and then take the values based on the index instead or something like that?

Thanks!

Kind Regards,
 
Author
Ranch Hand
Posts: 133
10
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark, For your first question the reason you're not getting a random card is because your PlayingCardClass has two constructors and one of the constructors isn't calling Math.random(). Notice inside your PlayingCardClass that you have two thingies both named PlayingCardClass --- one of them has the heading

public PlayingCardClass()

and the other has the heading

public PlayingCardClass(int theValue, int theSuit)

In the main method, when you call new PlayingCardClass() you're calling the first of the two constructors -- the constructor with empty parentheses (the constructor that takes no parameters). And as it happens, this constructor's body code simply says



It contains no call to the Math.random() method, so you're getting a joker every time you call it. On the other hand, the second constructor which takes two int parameters (theValue and theSuit) seems to want to generate a card with a certain suit and a certain value, but inside that constructor, you're calling Math.random() (which doesn't seem to be what you want to when you've supplied a suit and a value as parameters).
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome! Great explanation!
Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!