Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# I have a few basic method problems with my program

Posted on 2004-08-18
Medium Priority
142 Views
//the progamming solutions have to be made with very basic programming skills
//so that I can fully understand program and I have very basic skills (and also I might not be
//that smart, sorry).
//these are the problems:

//1. when i say Hit it should add one card to user card.
//2. when the user wins it should add the money
//3. when i say stand it should give me the total and end the game.
//these are the problems with my program it does not do those three things.

// Java Programming

// This program allows the user to play BlackJack with
// a certain amount of money the user wishes to buy in
// chips.  Programming Notes:
// 1. Program must be case-insitive!
// 2. Must utilize character manipulation
// 3. Must keep track of both player and dealer's totals
// 4. Must have a minimum bet
// 5. Must have a method that error checks numbers

// Method Definitions:
//    - take that amount and use it
// 2. bet METHOD:
//    - Ask player how much to bet
//    - Return bet amount to deduct from total chip amount
// 3. dealCard METHOD:
//    - Deal cards for player and dealer
// 4. printCardDealer METHOD:
//    - Determine if card is 1-10 or J, Q, K, A to print
// 5. printCardPlayer METHOD:
//    - Determine if card is 1-10 or J, Q, K, A to print

import java.text.*;
import java.io.*;
import java.util.*;

class Card
{
public static void main (String [] args) throws IOException
{
DataInputStream stdin = new DataInputStream (System.in);
DecimalFormat numberFmt = new DecimalFormat("##,###.00");

// Declare constants and variables

//Player's 1st card, Player's 2nd card, Player's total points, dealer's 1st card
//dealer's 2nd card, dealer's total

int user1 = 0, user2 = 0, user_total = 0, dealer_1 = 0, dealer_2 = 0,
dealer_tot = 0;

//total amount of chip

double       total = 0.0;

//continue the program? stand, hit or doubledown, dealer wins?,
//player wins? who is the winner?

char cont_game = 'Y', sh = 'S', win_dealer = 'N', win_user = 'N',
winning      = 'N';

// 1. Welcome the player

System.out.println("\n\n\n");
System.out.println("        WELCOME TO BLACKJACK        ");

System.out.println("\n\n");
System.out.println("\n");
System.out.println("\t Let's Get The Game Begin! \n");

// 2. This will control my entire program

// a. This is for buying chip

cont_game = 'Y';

while (cont_game == 'Y')
{

user_total  = 0;
dealer_tot  = 0;

// b. This will subtract from the player's money

total = total - bet();
System.out.println("Your total of chip is now:  " + numberFmt.format(total));

// c. Cards for dealer

dealer_1 = dealCard();
printCardDealer(dealer_1);
dealer_1 = cardScore11(dealer_1);

dealer_2 = dealCard();
printCardDealer(dealer_2);
if (dealer_1 < 11)
{
dealer_2 = cardScore11(dealer_2);
}
else
{
dealer_2 = cardScore(dealer_2);
}

dealer_tot = dealer_1 +  dealer_2;
System.out.println("Dealer total points: " + dealer_tot);

// d. Determine if dealer gets BlackJack

win_dealer = bj(dealer_1, dealer_2);
System.out.println("\nDealer win?: "+ win_dealer);

// e. Cards for player

user1 = dealCard();
printCardUser(user1, 1);
user1 = cardScore11(user1);

user2 = dealCard();
printCardUser(user2, 2);
if (user1 < 11)
{
user2 = cardScore11(user2);
}
else
{
user2 = cardScore(user2);
}
user_total = user1 + user2;
System.out.println("Your total points: " + user_total);

// f. Determine if the player gets BlackJack

win_user = bj(user1, user2);
System.out.println("\nUser win?: " + win_user);

// g. anyone wins?

if ((win_user == 'N') && (win_dealer == 'N'))
{
winning = 'N';
}
else
{
winning = 'Y';
}

while (winning == 'N')
{

// h. Ask user to stand or hit

sh = sh();
System.out.println("\nYou want to:  " + sh);

switch (sh)
{
case 'S': stand(dealer_1, dealer_2, dealer_tot);
break;
case 'H': hit(user1, user2 + user_total);
break;
default:;
break;
}
winning = 'Y';
}//while winning

cont_game = end_game();

}
}

// 3. This will ask the player how many chips she/he wants to buy

{
double total = 0.0;

DataInputStream stdin = new DataInputStream (System.in);
DecimalFormat numberFmt = new DecimalFormat("##,###.00");

System.out.println("\nJust keep in mind that minimum Bet is \$15");
System.out.print("\nHow much chip would you want to buy?(One chip = \$1):  ");
System.out.flush();

System.out.println("Your total chip is: " + numberFmt.format(total));

// 4. This will ask the player to place his/her bit

static double bet()  throws IOException
{
double bet   = 0.0;

DataInputStream stdin = new DataInputStream (System.in);
DecimalFormat numberFmt = new DecimalFormat("##,###.00");

System.out.flush();

while (bet < 15)
{
System.out.println("\n SORRY MY FRIEND YOUR BET IS NOT ENOUGH ");
System.out.println("Minimum bet is:  \$15");
System.out.flush();

}//while

return bet;

}//bet

// 5. This is the method for generating random cards

static int dealCard()
{

for (long i = 0; i <= 100000000; i++) ;

Random card = new Random();
return ((Math.abs(card.nextInt())) % 13 + 1);

}//dealCard

// 6. This will print or show the dealer's cards

static void printCardDealer(int card)
{

switch (card)
{
case 1: System.out.println("\nDealer's card shows:  A");
break;
case 11: System.out.println("\nDealer's card shows:  J");
break;
case 12: System.out.println("\nDealer's card shows:  Q");
break;
case 13: System.out.println("\nDealer's card shows:  K");
break;
default: System.out.println("\nDealer's card shows:  " + card);

}

}//printCardDealer

// 7. This will print or show the player's card

static void printCardUser(int card, int cardNum)
{
switch (card)
{
case 1: System.out.println("\nYour card #" + cardNum +":  A");
break;
case 11: System.out.println("Your card #" + cardNum +":  J");
break;
case 12: System.out.println("Your card #" + cardNum +":  Q");
break;
case 13: System.out.println("Your card #" + cardNum +":  K");
break;
default: System.out.println("Your card #" + cardNum +":  " + card);
}

}//printCardUser

// 8. This will generate card scores from 11 to 10 which 11 is ace

static int cardScore11(int card)
{
int cardOut = 0;

switch (card)
{
case 1: cardOut = 11;
break;
case 2: cardOut = 2;
break;
case 3: cardOut = 3;
break;
case 4: cardOut = 4;
break;
case 5: cardOut = 5;
break;
case 6: cardOut = 6;
break;
case 7: cardOut = 7;
break;
case 8: cardOut = 8;
break;
case 9: cardOut = 9;
break;
default: cardOut = 10;
break;
}//switch
return cardOut;
}

// 9. This will generate the card from 1 to 10 which 1 is ace

static int cardScore(int card)
{
int cardOut = 0;

switch (card)
{
case 1: cardOut = 1;
break;
case 2: cardOut = 2;
break;
case 3: cardOut = 3;
break;
case 4: cardOut = 4;
break;
case 5: cardOut = 5;
break;
case 6: cardOut = 6;
break;
case 7: cardOut = 7;
break;
case 8: cardOut = 8;
break;
case 9: cardOut = 9;
break;
default: cardOut = 10;
break;
}//switch
return cardOut;
}

// 10. This method will calculate the black jack

static char bj(int card1, int card2)
{

char win = 'N';

if ((card1 == 11) && (card2 == 10))
{
win = 'Y';
}

if ((card1 == 10) && (card2 == 11))
{
win = 'Y';
}

return win;
}//bj

// 11. This will print the total for dealr and player if choose to stand
//Also if your cards are soft 17 or hard 17
static char hit(int user1, int user2)
{
char hit = 'H';
if((user1 == 'H') && (user2 == 'H'))
{
hit+=hit +1;
}
return hit;
}
static int stand(int dealer_total, int dealer1, int dealer2)
{
int dealer_n     = 0;      // dealer's nth card

//dealer soft 18 case1
while ((dealer1 != 11) && (dealer2 != 7))
{
//dealer , hard 17
while (dealer_total < 17)
{
dealer_n = dealCard();
printCardDealer(dealer_n);
dealer_total = (dealer_total + cardScore(dealer_n));

}//for hard

}//for soft

//dealer soft 18 case2
while ((dealer2 != 11) && (dealer1 != 7))
{

//dealer , hard 17
while (dealer_total < 17)
{
dealer_n = dealCard();
printCardDealer(dealer_n);
dealer_total = (dealer_total + cardScore(dealer_n));

}//for hard

}//for soft

return dealer_total;
}//stand

// 12. The method for Hit, Stand

static char sh() throws IOException
{

char sh = '0';
DataInputStream stdin = new DataInputStream (System.in);

System.out.print("\nStand (S) or Hit (H)?:  ");
System.out.flush();

while ((sh != 'S') && (sh != 'H'))
{

System.out.println(" THAT IS NOT A VALID ENTRY PLEASE TRY AGAIN ");

System.out.print("\nStand (S) or Hit (H)?:  ");
System.out.flush();
}//while

return sh;

}//shd

// 13. This will show the end of the game

static char end_game() throws IOException
{
char cont_game = 'Y';
DataInputStream stdin = new DataInputStream (System.in);

System.out.print("Do you want to continue the game?(YES OR NO):  ");
System.out.flush();

while ((cont_game != 'Y') && (cont_game != 'N'))
{

System.out.println(" THAT IS NOT A VALID ENTRY PLEASE TRY AGAIN ");

System.out.print("Do you want to continue the game?(YES OR NO):  ");
System.out.flush();
}//while

return cont_game;
}//end_game

}//class blackjack
0
Question by:hedge243
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4

LVL 14

Accepted Solution

sudhakar_koundinya earned 2000 total points
ID: 11838269
I don't know what the problems you are facing

but I can say following metthod reduces ur applicaton performance

// 5. This is the method for generating random cards

static int dealCard()
{

for (long i = 0; i <= 100000000; i++) ;

Random card = new Random();
return ((Math.abs(card.nextInt())) % 13 + 1);

}//dealCard
0

LVL 14

Expert Comment

ID: 11838278
>>for (long i = 0; i <= 100000000; i++) ;

Instead of that I would prefer some thing like this

try
{
new Thread().sleep(2);//waits for 2 milli secs
}
catch(Exception ex){}
0

LVL 14

Expert Comment

ID: 11838306
ur code is going to infinite loop

test it

//the progamming solutions have to be made with very basic programming skills
//so that I can fully understand program and I have very basic skills (and also I might not be
//that smart, sorry).
//these are the problems:

//1. when i say Hit it should add one card to user card.
//2. when the user wins it should add the money
//3. when i say stand it should give me the total and end the game.
//these are the problems with my program it does not do those three things.

// Java Programming

// This program allows the user to play BlackJack with
// a certain amount of money the user wishes to buy in
// chips.  Programming Notes:
// 1. Program must be case-insitive!
// 2. Must utilize character manipulation
// 3. Must keep track of both player and dealer's totals
// 4. Must have a minimum bet
// 5. Must have a method that error checks numbers

// Method Definitions:
//    - take that amount and use it
// 2. bet METHOD:
//    - Ask player how much to bet
//    - Return bet amount to deduct from total chip amount
// 3. dealCard METHOD:
//    - Deal cards for player and dealer
// 4. printCardDealer METHOD:
//    - Determine if card is 1-10 or J, Q, K, A to print
// 5. printCardPlayer METHOD:
//    - Determine if card is 1-10 or J, Q, K, A to print

import java.text.*;
import java.io.*;
import java.util.*;

class Card
{
public static void main (String [] args) throws IOException
{
DataInputStream stdin = new DataInputStream (System.in);
DecimalFormat numberFmt = new DecimalFormat("##,###.00");

// Declare constants and variables

//Player's 1st card, Player's 2nd card, Player's total points, dealer's 1st card
//dealer's 2nd card, dealer's total

int user1 = 0, user2 = 0, user_total = 0, dealer_1 = 0, dealer_2 = 0,
dealer_tot = 0;

//total amount of chip

double       total = 0.0;

//continue the program? stand, hit or doubledown, dealer wins?,
//player wins? who is the winner?

char cont_game = 'Y', sh = 'S', win_dealer = 'N', win_user = 'N',
winning     = 'N';

// 1. Welcome the player

System.out.println("\n\n\n");
System.out.println("        WELCOME TO BLACKJACK        ");

System.out.println("\n\n");
System.out.println("\n");
System.out.println("\t Let's Get The Game Begin! \n");

// 2. This will control my entire program

// a. This is for buying chip

cont_game = 'Y';

while (cont_game == 'Y')
{

user_total  = 0;
dealer_tot  = 0;

// b. This will subtract from the player's money

total = total - bet();
System.out.println("Your total of chip is now:  " + numberFmt.format(total));

// c. Cards for dealer

dealer_1 = dealCard();
printCardDealer(dealer_1);
dealer_1 = cardScore11(dealer_1);

dealer_2 = dealCard();
printCardDealer(dealer_2);
if (dealer_1 < 11)
{
dealer_2 = cardScore11(dealer_2);
}
else
{
dealer_2 = cardScore(dealer_2);
}

dealer_tot = dealer_1 +  dealer_2;
System.out.println("Dealer total points: " + dealer_tot);

// d. Determine if dealer gets BlackJack

win_dealer = bj(dealer_1, dealer_2);
System.out.println("\nDealer win?: "+ win_dealer);

// e. Cards for player

user1 = dealCard();
printCardUser(user1, 1);
user1 = cardScore11(user1);

user2 = dealCard();
printCardUser(user2, 2);
if (user1 < 11)
{
user2 = cardScore11(user2);
}
else
{
user2 = cardScore(user2);
}
user_total = user1 + user2;
System.out.println("Your total points: " + user_total);

// f. Determine if the player gets BlackJack

win_user = bj(user1, user2);
System.out.println("\nUser win?: " + win_user);

// g. anyone wins?

if ((win_user == 'N') && (win_dealer == 'N'))
{
winning = 'N';
}
else
{
winning = 'Y';
}

while (winning == 'N')
{

// h. Ask user to stand or hit

sh = sh();
System.out.println("\nYou want to:  " + sh);

switch (sh)
{
case 'S': stand(dealer_1, dealer_2, dealer_tot);
break;
case 'H': hit(user1, user2 + user_total);
break;
default:;
break;
}
winning = 'Y';
}//while winning

cont_game = end_game();

}
}

// 3. This will ask the player how many chips she/he wants to buy

{
double total = 0.0;

DataInputStream stdin = new DataInputStream (System.in);
DecimalFormat numberFmt = new DecimalFormat("##,###.00");

System.out.println("\nJust keep in mind that minimum Bet is \$15");
System.out.print("\nHow much chip would you want to buy?(One chip = \$1):  ");
System.out.flush();

System.out.println("Your total chip is: " + numberFmt.format(total));

// 4. This will ask the player to place his/her bit

static double bet()  throws IOException
{
double bet   = 0.0;

DataInputStream stdin = new DataInputStream (System.in);
DecimalFormat numberFmt = new DecimalFormat("##,###.00");

System.out.flush();

while (bet < 15)
{
System.out.println("\n SORRY MY FRIEND YOUR BET IS NOT ENOUGH ");
System.out.println("Minimum bet is:  \$15");
System.out.flush();

}//while

return bet;

}//bet

// 5. This is the method for generating random cards

static int dealCard()
{

for (long i = 0; i <= 100000000; i++) ;

Random card = new Random();
return ((Math.abs(card.nextInt())) % 13 + 1);

}//dealCard

// 6. This will print or show the dealer's cards

static void printCardDealer(int card)
{

switch (card)
{
case 1: System.out.println("\nDealer's card shows:  A");
break;
case 11: System.out.println("\nDealer's card shows:  J");
break;
case 12: System.out.println("\nDealer's card shows:  Q");
break;
case 13: System.out.println("\nDealer's card shows:  K");
break;
default: System.out.println("\nDealer's card shows:  " + card);

}

}//printCardDealer

// 7. This will print or show the player's card

static void printCardUser(int card, int cardNum)
{
switch (card)
{
case 1: System.out.println("\nYour card #" + cardNum +":  A");
break;
case 11: System.out.println("Your card #" + cardNum +":  J");
break;
case 12: System.out.println("Your card #" + cardNum +":  Q");
break;
case 13: System.out.println("Your card #" + cardNum +":  K");
break;
default: System.out.println("Your card #" + cardNum +":  " + card);
}

}//printCardUser

// 8. This will generate card scores from 11 to 10 which 11 is ace

static int cardScore11(int card)
{
int cardOut = 0;

switch (card)
{
case 1: cardOut = 11;
break;
case 2: cardOut = 2;
break;
case 3: cardOut = 3;
break;
case 4: cardOut = 4;
break;
case 5: cardOut = 5;
break;
case 6: cardOut = 6;
break;
case 7: cardOut = 7;
break;
case 8: cardOut = 8;
break;
case 9: cardOut = 9;
break;
default: cardOut = 10;
break;
}//switch
return cardOut;
}

// 9. This will generate the card from 1 to 10 which 1 is ace

static int cardScore(int card)
{
int cardOut = 0;

switch (card)
{
case 1: cardOut = 1;
break;
case 2: cardOut = 2;
break;
case 3: cardOut = 3;
break;
case 4: cardOut = 4;
break;
case 5: cardOut = 5;
break;
case 6: cardOut = 6;
break;
case 7: cardOut = 7;
break;
case 8: cardOut = 8;
break;
case 9: cardOut = 9;
break;
default: cardOut = 10;
break;
}//switch
return cardOut;
}

// 10. This method will calculate the black jack

static char bj(int card1, int card2)
{

char win = 'N';

if ((card1 == 11) && (card2 == 10))
{
win = 'Y';
}

if ((card1 == 10) && (card2 == 11))
{
win = 'Y';
}

return win;
}//bj

// 11. This will print the total for dealr and player if choose to stand
//Also if your cards are soft 17 or hard 17
static char hit(int user1, int user2)
{
char hit = 'H';
if((user1 == 'H') && (user2 == 'H'))
{
hit+=hit +1;
}
return hit;
}
static int stand(int dealer_total, int dealer1, int dealer2)
{
int dealer_n     = 0;     // dealer's nth card

//dealer soft 18 case1
//         while ((dealer1 != 11) && (dealer2 != 7))
if((dealer1 != 11) && (dealer2 != 7))
{
//dealer , hard 17
while (dealer_total < 17)
{
dealer_n = dealCard();
printCardDealer(dealer_n);
dealer_total = (dealer_total + cardScore(dealer_n));

}//for hard

}//for soft

//dealer soft 18 case2
//while ((dealer2 != 11) && (dealer1 != 7))
if ((dealer2 != 11) && (dealer1 != 7))
{

//dealer , hard 17
while (dealer_total < 17)
{
dealer_n = dealCard();
printCardDealer(dealer_n);
dealer_total = (dealer_total + cardScore(dealer_n));

}//for hard

}//for soft

return dealer_total;
}//stand

// 12. The method for Hit, Stand

static char sh() throws IOException
{

char sh = '0';
DataInputStream stdin = new DataInputStream (System.in);

System.out.print("\nStand (S) or Hit (H)?:  ");
System.out.flush();

while ((sh != 'S') && (sh != 'H'))
{

System.out.println(" THAT IS NOT A VALID ENTRY PLEASE TRY AGAIN ");

System.out.print("\nStand (S) or Hit (H)?:  ");
System.out.flush();
}//while

return sh;

}//shd

// 13. This will show the end of the game

static char end_game() throws IOException
{
char cont_game = 'Y';
DataInputStream stdin = new DataInputStream (System.in);

System.out.print("Do you want to continue the game?(YES OR NO):  ");
System.out.flush();

while ((cont_game != 'Y') && (cont_game != 'N'))
{

System.out.println(" THAT IS NOT A VALID ENTRY PLEASE TRY AGAIN ");

System.out.print("Do you want to continue the game?(YES OR NO):  ");
System.out.flush();
}//while

return cont_game;
}//end_game

}//class blackjack
0

LVL 14

Expert Comment

ID: 12280181
:-)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…