aspose file tools*
The moose likes Beginning Java and the fly likes can't split card shuffle method into a separate class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can Watch "can New topic
Author

can't split card shuffle method into a separate class

Bill Stanard
Greenhorn

Joined: Oct 10, 2005
Posts: 12
I have created a class named DeckOfCard.java which creates a deck of cards (using a class named Card.java) and then shuffles it. This class includes a method to deal cards from the created, shuffled deck one at a time. What I want to do is split out, into a separate class, the shuffle method. I know that I�m missing something simple, but I keep getting a compile error. Here, first, is the one-file (single class) code for Card.java:

Here is the DeckOfCards.java code, the single-class create-shuffle-deal class:

My question is this: If I want to set up Shuffle.java as a separate class and use its method to shuffle the deck created in DeckOfCards, how do I have to configure Shuffle.java and how do I have to call its shuffle method? Here is what I wrote for a shuffle class:

Here is what I wrote for a test class (with the non-working call commented):

When I comment out the shuffle method in the original code and then run the Test.java with the Shuffle.riffle( ) call, I get the following error:
Test.java:10: riffle(Card[]) in Shuffle cannot be applied to (DeckOfCards)
Bottom line: splitting out the shuffle code to a separate class and calling its method, named riffle( ), from test is a no-go. I know it is something simple, but its obviousness escapes me.

[ December 02, 2005: Message edited by: Bill Stanard ]

[ December 05, 2005: Message edited by: Bill Stanard ]
[ December 05, 2005: Message edited by: Bill Stanard ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

The riffle() method is not static. To call it, you would need an instance of a Shuffle class. Or make the method static.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Bill Stanard
Greenhorn

Joined: Oct 10, 2005
Posts: 12
Thank you, Henry.

When I run the following


I then get the following error:

which seems to me to say that a DeckOfCards object cannot be "processed" by the riffle method

even though the method clearly calls for a Card argument. Is the DeckOfCards that riffle( ) cannot be applied to possibly NOT a Card[] object. If not, (a) what is it? and, (b) how do I have to declare the riffle method to achieve the shuffle desired?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

I am guessing once you fix the static method problem, this error will go away.

Henry
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

What is the reason for creating a separate Shuffle class? A shuffle method seems more appropriate as part of the Deck class. I can see you having a ShuffleStrategy interface that has a shuffle method and the Deck class taking an implementation of the ShuffleStrategy. That way, you could easily change the way the cards in a Deck are shuffled, if that's what you're trying to achieve.



Junilu - [How to Ask Questions] [How to Answer Questions]
Bill Stanard
Greenhorn

Joined: Oct 10, 2005
Posts: 12
Good question, Junilu. Less than satisfactory answer: I want to create a card game project for a group of students, so I want to break out a series of methods into separate classes (card, deck, shuffle, deal, discard, draw, score, et cetera).

I have been having the devil's own time calling classes that contain methods to use in this manner. I'm obviously making some fundamental error, but I don't seem to be able to figure it out. I can write the entire card game in a couple of classes, but I can't split it into a larger number of classes. Is there a project that you all know of that demonstrates the kind of interlinked series of classes like my projected card game that will give me a picture of how I must structure these classes?
[ December 05, 2005: Message edited by: Bill Stanard ]
Mahesh Sethi
Greenhorn

Joined: Nov 30, 2005
Posts: 6
riffle method is expecting array of Card objects as parameter, not the DeckofCard object.
However, if you pass the member "deck" (which is array of Card objects) of DeckofCard object, you may expect riffle to execute properly.

You can create a "getDeck" method in DeckofCard class that returns private member "deck" which is an array of card object and call this method on "boo" instance of DeckofCard class, passing as parameter to riffle.

Method to be included in DeckofCard Class:

public Card[] getDeck(){

return deck;

}


Call in main method:

s.riffle(bobo.getDeck());

Mahesh
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Originally posted by Bill Stanard:
Good question, Junilu. Less than satisfactory answer: I want to create a card game project for a group of students, so I want to break out a series of methods into separate classes (card, deck, shuffle, deal, discard, draw, score, et cetera).[ December 05, 2005: Message edited by: Bill Stanard ]


Bill, some of the things in the list don't logically qualify as classes, e.g. shuffle, deal, discard, draw. Notice that these are all verbs. The nouns are better candidates for classes, e.g. card, deck, score.

Some candidate classes might be: Card, Deck, Game, Dealer, Player, Hand, Shuffler.

Shuffler - takes one or more decks and shuffles them.

Game - encapsulates the rules of the particular card game: determines how many players can join a game, how many cards can be dealt, checks Hands to see if it has won the game, etc. Implementations may include PokerGame, BlackJackGame.

Player - encapsulates rules for playing a certain game. Variations of this class may include AggressivePlayer, ConservativePlayer, DumbLuckPlayer.

Dealer - a special kind of Player. Controls the Game and what Players can do, deals Hands to Players.

Hand - a collection of Card objects; varies depending on Game. Implementations may include PokerHand, BlackJackHand, etc.
Bill Stanard
Greenhorn

Joined: Oct 10, 2005
Posts: 12
Bill, some of the things in the list don't logically qualify as classes, e.g. shuffle, deal, discard, draw. Notice that these are all verbs. The nouns are better candidates for classes, e.g. card, deck, score.

Some candidate classes might be: Card, Deck, Game, Dealer, Player, Hand, Shuffler.


Aahhh... I think I need some direction in how to think about classes, methods, et cetera. Can you (or any of the helpfull folks in this neighborhood )suggest a book that might set me thinking more logically about the Java structure. I very much like the linguistic approach you seem to be suggesting and would like to pursue that...
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

I personally like Scott Ambler's "Object Primer" http://www.accu.org/bookreviews/public/reviews/o/o003581.htm
http://www.ambysoft.com/books/theObjectPrimer.html

Here are some articles that might help you:
http://www.developer.com/tech/article.php/10923_3304881_1
http://www.developer.com/design/article.php/3332401
http://www.developer.com/design/article.php/3347291
http://www.developer.com/design/article.php/3359871

You may also want to look at the book that the author of these articles wrote.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can't split card shuffle method into a separate class