Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Beginning Java and the fly likes blackjack java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Badass: Making Users Awesome this week in the Game Development forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "blackjack java" Watch "blackjack java" New topic

blackjack java

chan eva

Joined: Apr 29, 2007
Posts: 1
i am learning the java. blackjack game is my class work. but i don't how to write the deck class. THANKS

An object of type Deck represents an ordinary deck of 52 playing cards.
The deck can be shuffled, and cards can be dealt from the deck.

public class Deck {

// You should declare the following variables here:
// 1. An array of 52 Cards, representing the deck.
// 2. An int variable cardsUsed indicating how many cards
// have been dealt from the deck.

public Deck() {
// Create an unshuffled deck of cards and initialize cardsUsed

// Hints: try to use a nested for loop -
// one of the for loop counts the suit, another counts the value
// use another counter to count the index of the card creating (0-51)

public void shuffle() {
// Put all the used cards back into the deck, and shuffle it into
// a random order. Reset the cardsUsed to zero.

// Hints:
// Method 1: try to use a for loop (e.g. 100 iteration),
// choose 2 random number between 0 and 51
// and swap the two cards in each iteration.
// Method 2: loop for 51 times and swap the current card
// with a random card

public int getCardsUsed() {
// As cards are dealt from the deck, the number of cards left
// decreases. This function returns the number of cards that
// are still left in the deck.

public Card dealCard() {
// Deals one card from the deck and returns it.
// The card should be the first unused card.
// Hints: use the variable cardsUsed and return the array element
// You need to throw a RuntimeException if no card is left:
// throw new RuntimeException("No card left");

public String toString() { // toString method is done for you.
String value = "[ ";
for (int i=0; i<deck.length; i++) {
value += deck[i]+" ";
if(i%13==12 && i!=deck.length-1) value+="\n";
return value + "]";

} // end class Deck
marc weber

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

You already have some fairly detailed instructions and hints for writing this code. Where exactly are you stuck?

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
I agree. Here's the link:
subject: blackjack java