• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object Oriented 21 card game not working?  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so my problem right now is that my teacher is asking us to make 3 classes. One class (PlayingCard) returns a rank and suit for each card, which I got working. The other one, deckofplayingcards, is supposed to use 2 instance variables of an array of 52 playingcard objects, and index of the top card in the deck. My constructor is giving me problems though. I pass my values back from PlayingCard as a string, and I need to declare an array of unique object cards as these strings. It is not working, to say the least. I have attached the photo of what he asks for specifically.

My problem now is, that I cannot make an array of objects (in DeckOfPlayingCards at the end of the code) equal to a string (which is generated in the method finalValue in PlayingCard). Any help please? Also, any other corrections or tips for my H.W./code would be appreciated. Here is my code. It would return when working correctly something like 1 of hearts. Mind, I am still kind of a begginer to Java coding.

Playing Card:




DeckOfPlayingCards:

21-game-pic..PNG
[Thumbnail for 21-game-pic..PNG]
 
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My problem now is, that I cannot make an array of objects (in DeckOfPlayingCards at the end of the code) equal to a string 

Can you give an example of  a String and the array of objects you want to create that is equal to it?
 
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
First off, please repost the instructions as cut-n-paste textd, not a screen snap shot.

Instructions require "a method" to show rank and suit e.g. "ace of spades". This sound like a toString() method to me.

Your deck class needs to initialize the deck with 52 unique cards. Your use of random numbers does not guarantee uniqueness.
 
Ranch Hand
Posts: 83
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the instance variables section, it asks for the index of the top card in the deck. The instance variable you wrote is not the index, but a playing card itself.

Right now, I would focus on making your PlayingCard class better. Right now, you randomly generate cards but the instructions ask for them to be unique. So, you have to explicitly code each card. Don't just go through and write all the cards to be created manually though, use a loop.


I understand why you coded it like this: it seems easy to write and copy paste this over and over, but it's really bad practice to do this. Not only does it make it difficult to debug because of the possibility of you forgetting to change one when you copy paste, but it makes the code messy and it would take some work to change them in the future if you needed to. I would find a better way to do this, first of all. Don't use a case statement.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!