• 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 ...
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!