Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Deck of Cards- using another classes variables  RSS feed

 
Kirstie Fran
Ranch Hand
Posts: 33
Android Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm creating a program that deals cards at random. It has three classes in it: Card, DeckOfCards, Test(not shown here).
My issue is with using "rank" and "suit" in the DeckOfCards class sine they were initiated and defined in the card class.

Card


Deck of Cards


How do i get around the error:
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, you've defined "suit" and "rank" as instance variables of the Card class. That means you need to have an instance of Card in order to use "suit" and "rank." For example, if you create an instance...

Card myCard = new Card();

...then you can access the suit array as myCard.suit, or get its length using myCard.suit.length.

Note that the variable "myCard" (or whatever you call it) needs to be within the scope you're using it -- for example, somewhere in the Deck.
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would probably want to declare rank and suit static i.e make these arrays belong to the class since they aren't defining attributes of any instance of the class Card.
You can access public static variables without an instance of the class like this:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!