This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

blackjack java  RSS feed

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

You already have some fairly detailed instructions and hints for writing this code. Where exactly are you stuck?
The first person to drink cow's milk. That started off as a dare from this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!