create and call a method wager() , the asks the player how much they want to wag
ID: 3873591 • Letter: C
Question
create and call a method wager(), the asks the player how much they want to wager and returns the wager amount and create an overloaded method balance(), that takes either one or two parameters: (balance) (balance, wager). Balance() returns a type Boolean (True/False), if Balance is > 0 then returns True else False. If Balance - wager > 0 then return True else False.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Craps extends JApplet implements ActionListener {
boolean firstRoll = true;
int dieSum;
int myPoint;
int gameStatus;
int bankBalance, wager;
JLabel die1Label, die2Label, sumLabel, pointLabel, betLabel;
JTextField firstDie, secondDie, sum, point, better, chatter;
JButton roll;
final int WON = 0, LOST = 1, CONTINUE = 2;
public void init()
{
bankBalance = 1000;
gameStatus = CONTINUE;
betLabel = new JLabel( "bet:" );
better = new JTextField( "100", 10 );
die1Label = new JLabel( "Die 1" );
firstDie = new JTextField( 10 );
firstDie.setEditable( false );
die2Label = new JLabel( "Die 2" );
secondDie = new JTextField( 10 );
secondDie.setEditable( false );
sumLabel = new JLabel( "Sum is" );
sum = new JTextField( 10 );
sum.setEditable( false );
roll = new JButton( "Roll Dice" );
roll.addActionListener( this );
pointLabel = new JLabel( "Point is" );
point = new JTextField( 10 );
point.setEditable( false );
chatter = new JTextField( 25 );
chatter.setEditable( false );
Container container = getContentPane();
container.setLayout( new FlowLayout() );
container.add( die1Label );
container.add( firstDie );
container.add( die2Label );
container.add( secondDie );
container.add( sumLabel );
container.add( sum );
container.add( pointLabel );
container.add( point );
container.add( betLabel );
container.add( better );
container.add( roll );
container.add( chatter );
}
public void play()
{
if ( firstRoll ) {
dieSum = rollDice();
switch ( dieSum ) {
case 7: case 11:
gameStatus = WON;
point.setText( "" );
firstRoll = true;
break;
case 2: case 3: case 12:
gameStatus = LOST;
point.setText( "" );
firstRoll = true;
break;
default:
gameStatus = CONTINUE;
myPoint = dieSum;
point.setText( Integer.toString( myPoint ) );
firstRoll = false;
break;
}
}
else {
dieSum = rollDice();
if ( dieSum == myPoint )
gameStatus = WON;
else if ( dieSum == 7 )
gameStatus = LOST;
}
if ( gameStatus == CONTINUE )
showStatus( "($ " + bankBalance + ") Roll again." );
else {
if ( gameStatus == WON ) {
bankBalance += wager;
showStatus( "($ " + bankBalance + ") Player wins. " +
"Click Roll Dice to play again." );
}
else {
bankBalance -= wager;
checkBalance();
showStatus( "($ " + bankBalance + ") Player loses. " +
"Click Roll Dice to play again." );
}
better.setEditable( true );
firstRoll = true;
}
}
void checkBalance()
{
if ( bankBalance <= 0 ) {
System.out.println( "Sorry. You busted!" );
System.exit( 0 );
}
}
public void actionPerformed( ActionEvent e )
{
int wage = Integer.parseInt( better.getText() );
if ( wage > bankBalance || wage < 0 )
showStatus( "( $" + bankBalance + " ) " +
"Enter a valid wager!" );
else {
wager = wage;
better.setEditable( false );
play();
}
chatter.setText( chatter() );
}
public int rollDice()
{
int die1, die2, workSum;
die1 = 1 + ( int ) ( Math.random() * 6 );
die2 = 1 + ( int ) ( Math.random() * 6 );
workSum = die1 + die2;
firstDie.setText( Integer.toString( die1 ) );
secondDie.setText( Integer.toString( die2 ) );
sum.setText( Integer.toString( workSum ) );
return workSum;
}
public String chatter()
{
String statement = null;
switch ( ( int ) ( Math.random() * 5 ) ) {
case 0:
statement = "Oh, you're going for broke huh?";
break;
case 1:
statement = "Aw cmon, take a chance!";
break;
case 2:
statement = "You're up big. Now's the " +
"time to cash in your chips!";
break;
case 3:
statement = "You're way too lucky! I think you're a cheat!!!";
break;
case 4:
statement = "I'm betting all my money on you.";
break;
}
return statement;
}
}
Explanation / Answer
All the working you ask is already there in the above mention code and it need a little bit modification. So I did the modification. Your modified code is shown below:
package chegg;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Craps extends JApplet implements ActionListener {
boolean firstRoll = true;
int dieSum;
int myPoint;
int gameStatus;
int bankBalance, wager;
JLabel die1Label, die2Label, sumLabel, pointLabel, betLabel;
JTextField firstDie, secondDie, sum, point, better, chatter;
JButton roll;
final int WON = 0, LOST = 1, CONTINUE = 2;
public void init()
{
bankBalance = 1000;
gameStatus = CONTINUE;
betLabel = new JLabel( "bet:" );
better = new JTextField( "100", 10 );
die1Label = new JLabel( "Die 1" );
firstDie = new JTextField( 10 );
firstDie.setEditable( false );
die2Label = new JLabel( "Die 2" );
secondDie = new JTextField( 10 );
secondDie.setEditable( false );
sumLabel = new JLabel( "Sum is" );
sum = new JTextField( 10 );
sum.setEditable( false );
roll = new JButton( "Roll Dice" );
roll.addActionListener( this );
pointLabel = new JLabel( "Point is" );
point = new JTextField( 10 );
point.setEditable( false );
chatter = new JTextField( 25 );
chatter.setEditable( false );
Container container = getContentPane();
container.setLayout( new FlowLayout() );
container.add( die1Label );
container.add( firstDie );
container.add( die2Label );
container.add( secondDie );
container.add( sumLabel );
container.add( sum );
container.add( pointLabel );
container.add( point );
container.add( betLabel );
container.add( better );
container.add( roll );
container.add( chatter );
}
public void play()
{
if ( firstRoll ) {
dieSum = rollDice();
switch ( dieSum ) {
case 7: case 11:
gameStatus = WON;
point.setText( "" );
firstRoll = true;
break;
case 2: case 3: case 12:
gameStatus = LOST;
point.setText( "" );
firstRoll = true;
break;
default:
gameStatus = CONTINUE;
myPoint = dieSum;
point.setText( Integer.toString( myPoint ) );
firstRoll = false;
break;
}
}
else {
dieSum = rollDice();
if ( dieSum == myPoint )
gameStatus = WON;
else if ( dieSum == 7 )
gameStatus = LOST;
}
if ( gameStatus == CONTINUE )
showStatus( "($ " + bankBalance + ") Roll again." );
else {
if ( gameStatus == WON ) {
bankBalance += wager;
showStatus( "($ " + bankBalance + ") Player wins. " +
"Click Roll Dice to play again." );
}
else {
check();
checkBalance();
showStatus( "($ " + bankBalance + ") Player loses. " +
"Click Roll Dice to play again." );
}
better.setEditable( true );
firstRoll = true;
}
}
boolean check()
{
bankBalance -= wager;
if(bankBalance > 0)
return true;
else
return false;
}
void checkBalance()
{
if ( bankBalance <= 0 ) {
System.out.println( "Sorry. You busted!" );
System.exit( 0 );
}
}
public void actionPerformed( ActionEvent e )
{
int w = Wager();
boolean b = Balance(w, bankBalance);
if(b)
showStatus( "( $" + bankBalance + " ) " +
"Enter a valid wager!" );
else{
wager = w;
better.setEditable( false );
play();
}
chatter.setText( chatter() );
}
public int Wager()
{
int wage = Integer.parseInt( better.getText() );
return wage;
}
public boolean Balance(int w,int bal)
{
if ( w > bal || w < 0 )
return true;
else {
return false;
}
}
public int rollDice()
{
int die1, die2, workSum;
die1 = 1 + ( int ) ( Math.random() * 6 );
die2 = 1 + ( int ) ( Math.random() * 6 );
workSum = die1 + die2;
firstDie.setText( Integer.toString( die1 ) );
secondDie.setText( Integer.toString( die2 ) );
sum.setText( Integer.toString( workSum ) );
return workSum;
}
public String chatter()
{
String statement = null;
switch ( ( int ) ( Math.random() * 5 ) ) {
case 0:
statement = "Oh, you're going for broke huh?";
break;
case 1:
statement = "Aw cmon, take a chance!";
break;
case 2:
statement = "You're up big. Now's the " +
"time to cash in your chips!";
break;
case 3:
statement = "You're way too lucky! I think you're a cheat!!!";
break;
case 4:
statement = "I'm betting all my money on you.";
break;
}
return statement;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.