# Card.Java and BlackJack

• (2 Pages)
• 1
• 2

## 16 Replies - 9747 Views - Last Post: 28 January 2009 - 09:26 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=80935&amp;s=bada34d67b443f396f2bf8acbe896835&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Cuzenu

Reputation: 0
• Posts: 4
• Joined: 14-January 09

# Card.Java and BlackJack

Posted 14 January 2009 - 06:46 PM

```/**
* Cards.java stores the values of 52 playing cards.
*
* @author Melissa Gendron
* @version 1/14/09
*/

import java.util.Random;
public class Cards
{
//************************************************************************
// Create the enumerated type to determine the each card face
//************************************************************************
enum Card {ace, two, three, four, five, six, seven, eight, nine, ten,
jack, queen, king, error}

public static void main(String[] args)
{
//*********************************************************************
//Initial variable declarations
//*********************************************************************
int card = 1;  //used to determine which card, 1-13, to draw
int suit = 1; //used to determine which suit the card it
Card c = Card.ace;  //creates an enum d of type Card and gives it the
//initial value of "ace"

//*****************************************************************
//This switch sets the value of the enumerated type c showing
//the face value of the card.
//*****************************************************************
switch(card)
{
case 1: c = Card.ace; break;
case 2: c = Card.two; break;
case 3: c = Card.three; break;
case 4: c = Card.four; break;
case 5: c = Card.five; break;
case 6: c = Card.six; break;
case 7: c = Card.seven; break;
case 8: c = Card.eight; break;
case 9: c = Card.nine; break;
case 10: c = Card.ten; break;
case 11: c = Card.jack; break;
case 12: c = Card.queen; break;
case 13: c = Card.king; break;
default: c = Card.error; break;
}

//*****************************************************************
//This switch sets the value of the enumerated type for printing
//the chosen suit of the card.
//*****************************************************************
switch(suit)
{
case 2: System.out.println(" of clubs");
case 3: System.out.println(" of hearts");
case 4: System.out.println(" of diamonds");
default: System.out.println(); break;
}
}
}

```

Hello I have question regarding a program I am supposed to create.
We are supposed to: Design and implement a class called Card that represents a standard playing card. Each card has a suit and a face value.
And I am supposed to create a driver program that plays blackjack with the user.

What I'm wondering is, how can I make the switch statments of the card's face and suit value, print as a random generated number without doubling up? (like ace of heart x2)

I have learned about enums as well as basic switch statements. I have not learned about arrays.

I have not started the blackjack runner because I'm not sure how to create a correct shuffle and deal method, or at least a dealing method that ensure no glitches (repeated cards).

I already forsee a problem with my ace value because in blackjack it can count as 1 or 11, so I'm not sure if a user accepted loop can boolean the value into one or the other according to what the user wants, or if the circumstances force it, such as having an Ace, Jack, and 3, so ace must be a 1 to continue.

Can anybody help me with a skeleton of example or what I need for my program?

I use BlueJ, and I have only used Java for 4 months.

This post has been edited by Cuzenu: 14 January 2009 - 06:59 PM

Is This A Good Question/Topic? 0

## Replies To: Card.Java and BlackJack

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Card.Java and BlackJack

Posted 14 January 2009 - 08:00 PM

You need a class Card not an enum card or you will switch() for ever
Something like:

```class Card {
int suit;
int value;

Card(int suit, int value) {
this.suit = suit;
this.value = value;
}
}

```

Then you can have a class Deck

```class Deck {
Card[] card;

Deck() {
card = new Card[52];
int k = 0;
for(int i = 0; i < 4; i++) {
for(int j = 1; j <= 13; j++) {
card[k++] = new Card(i, j);
}
}
}
}

```

This post has been edited by pbl: 14 January 2009 - 08:46 PM

### #3 Cuzenu

Reputation: 0
• Posts: 4
• Joined: 14-January 09

## Re: Card.Java and BlackJack

Posted 14 January 2009 - 08:30 PM

I'm not sure what the "this." is, I have never seen that in my book.

I've decided a new approach, of using just the jack, queen, king, and ace as switch statements as well as the suits, but the numbers (2-10) will remain integers. I talked this over with a friend and she says I need to set my default (of the J, Q, K, A) to generate a random int, I'm guessing and checking but I am not sure how to write that specifically within my code.

Well I'm trying to change my code but I keep on getting a:

";" expected error message in regards to attempt to randomly generate the default

``` /**
* Cards.java stores the values of 52 playing cards.
*
* @author Melissa Gendron
* @version 1/14/09
*/

import java.util.Random;
public class Card02
{
//************************************************************************
// Create the enumerated type to determine the each card face
//************************************************************************
enum Face {jack, queen, king, ace, error}

{
//*********************************************************************
//Initial variable declarations
//*********************************************************************
Random generator = new Random();
int face = 1;  //used to determine which card, 1-13, to draw
int suit = 1; //used to determine which suit the card it
Face f = Face.ace;  //creates an enum c of type Card and gives it the
//initial value of "ace"
int faceValue;

//*****************************************************************
//This switch sets the value of the enumerated type c showing
//the face value of the card.
//*****************************************************************
switch(faceValue)
{
case 11: f = Face.jack; break;
case 12: f = Face.queen; break;
case 13: f = Face.king; break;
case 14: f =Face.ace; break;
default: f = Random generator.nextInt(int 10) + 2; break;
}

//*****************************************************************
//This switch sets the value of the enumerated type for printing
//the chosen suit of the card.
//*****************************************************************
switch(suit)
{
case 2: System.out.println(" of clubs");
case 3: System.out.println(" of hearts");
case 4: System.out.println(" of diamonds");
default: System.out.println(); break;
}
}
}

```

### #4 xCraftyx

• D.I.C Regular

Reputation: 11
• Posts: 258
• Joined: 13-September 08

## Re: Card.Java and BlackJack

Posted 14 January 2009 - 08:32 PM

If you would like some different strategies for blackjack contact me over msn or aim (both can be found in my profile), i finished writing a blackjack program a couple of days ago and would be happy to help with the logic. I'm not too good at proofreading others' code yet

This post has been edited by xCraftyx: 14 January 2009 - 08:34 PM

### #5 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Card.Java and BlackJack

Posted 14 January 2009 - 08:38 PM

Sorry I had a k++ missing
Here is an enhanced copy

```class Card {
private static final String[] cardName = {"", "As", "Two", "Three", "Four", "Five", "Six",
"Seven", "Height", "Nine", "Ten", "Jack", "Queen", "King"};

int suit;
int value;

Card(int suit, int value) {
this.suit = suit;
this.value = value;
}

public String toString() {
return cardName[value] + " of " + suitName[suit];
}

public static void main(String[] arg) {
Deck deck = new Deck();
for(int j = 0; j < 10; j++) {
deck.shuffle();
System.out.println("Pick number #" + j);
for(int i = 0; i < 10; i++)
System.out.println("Card picked: " + deck.getCard());
}
}
}

```

```import java.util.*;

class Deck {
Card[] card;
Random ran;
ArrayList<Card> arrayList;

Deck() {
ran = new Random();
arrayList = new ArrayList<Card>();
card = new Card[52];
int k = 0;
for(int i = 0; i < 4; i++) {
for(int j = 1; j <= 13; j++) {
card[k++] = new Card(i, j);
}
}
}

// shuffle reset the ArrayList
void shuffle() {
arrayList.clear();
for(int i = 0; i < 52; i++)
}

// return randomly a card
Card getCard() {
return arrayList.remove(ran.nextInt(arrayList.size()));
}
}

```

Which generates

```Pick number #0
Card picked: Ten of Heart
Card picked: King of Diamond
Card picked: King of Heart
Card picked: Jack of Diamond
Card picked: Five of Diamond
Card picked: Height of Diamond
Pick number #1
Card picked: Height of Diamond
Card picked: As of Diamond
Card picked: Queen of Heart
Card picked: Four of Heart
Card picked: Five of Diamond
Card picked: Jack of Diamond
Pick number #2
Card picked: King of Diamond
Card picked: King of Heart
Card picked: As of Heart
Card picked: Five of Heart
Card picked: Nine of Diamond
Card picked: Height of Heart
Pick number #3
Card picked: Seven of Heart
Card picked: Two of Diamond
Card picked: Six of Heart
Card picked: Ten of Heart
Card picked: Seven of Diamond
Pick number #4
Card picked: Seven of Heart
Card picked: Queen of Heart
Card picked: Height of Diamond
Card picked: Six of Heart
Card picked: King of Heart
Pick number #5
Card picked: Queen of Diamond
Card picked: Six of Heart
Card picked: Nine of Diamond
Card picked: Five of Heart
Card picked: King of Diamond
Pick number #6
Card picked: Three of Diamond
Card picked: Ten of Heart
Pick number #7
Card picked: Two of Heart
Card picked: Three of Heart
Card picked: Nine of Diamond
Card picked: Four of Heart
Card picked: Nine of Heart
Card picked: Six of Heart
Pick number #8
Card picked: Queen of Heart
Card picked: Jack of Diamond
Card picked: Six of Diamond
Pick number #9
Card picked: Ten of Diamond
Card picked: Queen of Diamond
Card picked: Four of Diamond
Card picked: Ten of Heart
Card picked: Height of Diamond
Card picked: Six of Heart

```

### #6 Cuzenu

Reputation: 0
• Posts: 4
• Joined: 14-January 09

## Re: Card.Java and BlackJack

Posted 14 January 2009 - 09:11 PM

Well I've attempted a few other changed but my major problem is that when I added the "deal" method the header says "public" and my compiler says it is an illegal start of expression.

I'm sorry if some of my errors make you cringe, but my current teacher's motto about teaching (any class, AP CS included): "If I can do it, you can do it." Which I find completely wrong because I have not attended college and worked as a debugger for IBM for xxx#? of years.

My basic learning method is readings from the book and then writing specific programs and working on labs.

So basically my teacher does not offer any assistance to all of her (4) students, so I'm struggling with CS and losing interest in Java/CS at a very steep decline, not to say I'm still trying to figure it all out, but I have no one within my family who can help me, my best advise giver is a girl in my class who would do a better job at teaching than the current teachers.

Anyways, that explains my very "noob" coding, any hints are appreciated. I don't mind constructive criticism as long as there is a "way to improve" that could help make it better, sort of comment.

Back to my actual point and topic, here is the updated code (with a few minor errors).

```/**
* Cards.java stores the values of 52 playing cards.
*
* @author Melissa Gendron
* @version 1/14/09
*/

import java.util.Random;
public class Card02
{
//************************************************************************
// Create the enumerated type to determine the each card face
//************************************************************************
enum Face {jack, queen, king, ace, error}

{
//*********************************************************************
//Initial variable declarations
//*********************************************************************
Random generator = new Random();
Face f = Face.ace;  //creates an enum c of type Card and gives it the
//initial value of "ace"
int faceValue;
int faceSuit;

//*****************************************************************
//This switch sets the value of the enumerated type c showing
//the face value of the card.
//*****************************************************************
switch(faceValue)
{
case 11: f = Face.jack; break;
case 12: f = Face.queen; break;
case 13: f = Face.king; break;
case 14: f =Face.ace; break;
default: f = "What?"; break;
}

//*****************************************************************
//This switch sets the value of the enumerated type for printing
//the chosen suit of the card.
//*****************************************************************
switch(faceSuit)
{
case 2: System.out.println(" of clubs");
case 3: System.out.println(" of hearts");
case 4: System.out.println(" of diamonds");
default: System.out.println(); break;
}

// Determine the card's face value with
// randomly generated int parameters

public int deal()
{
faceValue = generator.nextInt(faceValue) + 1;
faceSuit = generator.nextInt(faceSuit) + 1;

result = System.out.println (+faceValue + faceSuite);
return result;
}
}
}

```

Thanks for helping me so far, but I still have not learned the "this" object declaration thing, so it will not be acceptable in my program.

Here is a link to my actual book PDF (only up to Chapter 4, but I am on Chapter 4) the files are offered as a demo of the full book, so it's free and legal. It might help point out to my current limitations.

### #7 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Card.Java and BlackJack

Posted 14 January 2009 - 10:03 PM

This a method in the middle of nowhere
You need a } to close an balance your previous {}
```	}
// Determine the card's face value with
// randomly generated int parameters

public int deal()
{
faceValue = generator.nextInt(faceValue) + 1;
faceSuit = generator.nextInt(faceSuit) + 1;

result = System.out.println (+faceValue + faceSuite);
return result;
}
}

```

result is not define
System.out.println() returns void you cannot assign its returned value to something. It is:
- System.out.pritnln()
not
- abc = System.out.println();

You have a ; missing after your enum

```{
//*********************************************************************
//Initial variable declarations
//*********************************************************************
Random generator = new Random();

```

This is the beginning of a method... it needs a type an a name like
```public static void main(String[] arg)
{
//*********************************************************************
//Initial variable declarations
//*********************************************************************
Random generator = new Random();

```

### #8 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Card.Java and BlackJack

Posted 14 January 2009 - 10:51 PM

I had to do a blackjack program for last semester. I have released the code for anyone to look as a learning tool. See here

Let me know if you have any questions or you want to tell me how bad my code is

### #9 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Card.Java and BlackJack

Posted 14 January 2009 - 10:54 PM

KYA, on 14 Jan, 2009 - 09:51 PM, said:

I had to do a blackjack program for last semester. I have released the code for anyone to look as a learning tool. See here

Let me know if you have any questions or you want to tell me how bad my code is

KYA my good friend,

don't challenge me

### #10 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Card.Java and BlackJack

Posted 14 January 2009 - 10:55 PM

Uh oh

Be merciful pbl! I beseech thee!

### #11 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Card.Java and BlackJack

Posted 14 January 2009 - 10:56 PM

KYA, on 14 Jan, 2009 - 09:51 PM, said:

I had to do a blackjack program for last semester. I have released the code for anyone to look as a learning tool. See here

Let me know if you have any questions or you want to tell me how bad my code is

Actually your "signature" is quite impressive
How did you manage that ?

### #12 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Card.Java and BlackJack

Posted 14 January 2009 - 10:57 PM

It's a widget from a site that just reflects your IP address information back at you. People can only see their own info.

### #13 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Card.Java and BlackJack

Posted 14 January 2009 - 10:59 PM

KYA, on 14 Jan, 2009 - 09:51 PM, said:

I had to do a blackjack program for last semester. I have released the code for anyone to look as a learning tool. See here

Let me know if you have any questions or you want to tell me how bad my code is

Hey !!
I have a site with a lot of free Gigabytes if you want to post stuff

### #14 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Card.Java and BlackJack

Posted 14 January 2009 - 11:01 PM

Whoa what? I'll just upload it here.

#### Attached File(s)

This post has been edited by KYA: 14 January 2009 - 11:01 PM

### #15 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Card.Java and BlackJack

Posted 14 January 2009 - 11:10 PM

KYA, on 14 Jan, 2009 - 10:01 PM, said:

Whoa what? I'll just upload it here.