This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Need Help in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need Help in java" Watch "Need Help in java" New topic
Author

Need Help in java

Jame Brown
Greenhorn

Joined: Mar 18, 2006
Posts: 4
I need to make a 3 classes that do this:

Classes: Deck-Of-Cards, Card, Shuffle-Deck

- Deck-Of-Cards that store 52 objects of the Card Class
- Shuffle-Deck (shuffles the deck,deals a card from the deck,keeps track how many cards are left in the deck)
- Card I guess is the driver class it really does not give me to much info on it.

It says that the Card class was from chapter 4 in my book but I can't find it anywhere expect as a project that we never did. And all it wanted was one class called card that represents a standard playing deck and deals like 5 cards out at random.

So I started to work on DeckOfCards first to hold the 52 objects so I thought of puting them as enum types in.

so I have it like this:


So to me I think thats all I need to do to hold in deck-of-cards since I think that holds the 52 to objects.

So then I moved to shuffle-Deck and did this:



as you can see not much since I am unable to get past a couple things like how do I shuffle this deck? I was going to use a random but problems I can see I will face is first how do I shuffle them as a whole? Like right now there are 2 enum types(Rank and Suit) how do I combine them together so that it will shuffle all 52 cards as a whole and still know that they belong to different suits.

So I need help and go from there.

*Deck-Of-Cards -- This is not a mistake I put those dashes on purpose in hopes to make it a bit harder to find on the search engine since I am sure many ppl will have trouble with this and will be looking for the answers..... So I don't them to just copy my work that I been working hard on....
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
In my opinion, you should start with a Card class. It could be pretty simple and could use the enum types you already stated


Then in your Deck-Of-Cards class it could hold a List<Card> of 52 unique Card objects.


[ March 18, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Jame Brown
Greenhorn

Joined: Mar 18, 2006
Posts: 4
I been thinking and I am wondering if this would work and how to go about doing it.

with the enum types right to keep that as if and add a arraylist to it. Like since a array list can grow to any length would this not be perfect to combine them all?

Like I was thinking of doing something like this

1st: Make the enum type Rank(holds: ace, two, three, four, five, six, seven, eight, nine, ten, jack, queen, king)
2nd: Make the enum type Suit(holds: Hearts,Spades,Clubs,Diamonds)
3rd: Make an arrayList called deck
4th: throw that in a loop and have the loop add each card to this array by using the enum types. So like the loop would look something like this

Loop:
So the loop would basicly go through and add everything in Rank with incrmenting each time to the next one from ace to two etc, At the same time Suit would be adding suit type to the end of each card.

Once Rank would go through once it would start all over again and suit would move to the next one in the enum so from Hearts to Spades.

Problems:

I have no Idea how to actually make this work I never done anything like this where I am looping and changing the enum type by doing this. I also would not know how to make it start over again and change the suit after every 13 times.

Once I would have this I think I could use the array list in the random shuffle method and that would work?
Jame Brown
Greenhorn

Joined: Mar 18, 2006
Posts: 4
So you think that Card is not the main class that is used to test it?

I also have questions about what you wrote:


First from the top why is it private? and why is it Private Rank myRank. ARe you getting the object Rank from enum types? and why did you name it myRank?

With this:

public Card(Rank rank, Suit suit)
{
//...
}

So your taking in 2 parmaters in right? 1 from rank and other from suit. So I am not sure though what I should add in it should I have an array list in it adding each paramter passed into the list? but then how do I get them all in? would I need some sort of loop or something like that?

Well thats where I am stuck I have not even attempted to look at the other hints you given me.
[ March 18, 2006: Message edited by: Jame Brown ]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
First from the top why is it private?

It is a standard technique to declare member variables private, and allow access to them through accessor methods. i.e.:
So your taking in 2 parmaters in right? 1 from rank and other from suit. So I am not sure though what I should add in it should I have an array list in it adding each paramter passed into the list? but then how do I get them all in? would I need some sort of loop or something like that?

That is what I was suggesting in the Deck-Of-Cards constructor. Each enum type has a staitc method values() that returns an array of all the valuea of that enum type in the order declared. Therefore:

would return an array of all the declared Rank's. Consequently, if you use two nested loops:

you can iterate though all the possible suits and ranks, instantiating a new Card instance for each one and adding it to the deck.
[ March 18, 2006: Message edited by: Garrett Rowe ]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
By the way, the Card class I was suggesting would represent a single card in a deck, while Deck-Of-Cards would represent a collection of 52 unique Card instances.
[ March 19, 2006: Message edited by: Garrett Rowe ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need Help in java