Advanced Java Programming with Object-Oriented Programming Design VERY IMPORTANT
ID: 3725396 • Letter: A
Question
Advanced Java Programming with Object-Oriented Programming Design
VERY IMPORTANT NOTE!!: This program for the question below MUST USE at least 2 CLASSES, getters/setters, etc and other object-oriented design (OOD) concepts because I'm in a more advanced Java class that requires me to utilize the concepts mentioned above. Also, I need detailed, but brief comments to most, if not all of the code to explain what is being used in the code and what is does so I have a better understanding of the code. I've seen answers to the question below already posted, however they don't utilize object-oriented design (OOD) concepts such as a minumum of two classes, getters/setters, etc which is required for this advanced Java programming class. So please......answer the question and meet the requirements for this question. Basically, I need advanced Java object-oriented design concepts for simple programs so here's to hoping this works out and if it does, I'll give a thumbs up, I promise!!!
Modify the craps program of Fig. 6.8 to allow wagering. Initialize variable bankBalance to 1000 dollars. Prompt the player to enter a wager. Check that wager is less than or equal to bankBalance, and if it’s not, have the user reenter wager until a valid wager is entered. Then, run one game of craps. If the player wins, increase bankBalance by wager and display
the new bankBalance. If the player loses, decrease bankBalance by wager, display the new bankBalance, check whether bankBalance has become zero and, if so, display the message "Sorry. You busted!" As the game progresses, display various messages to create some “chatter,” such as "Oh, you're going for broke, huh?" or "Aw c'mon, take a chance!" or "You're up big. Now's the time to cash in your chips!". Implement the “chatter” as a separate method that randomly chooses the string to display.
Sadly I can't post a picture of figure 6.8 for whatever reason so here's what I just pulled from a PDF containing the required figure. 6.8 I'm very sorry but this is the best I can do, I hope to God this helps otherwise I'm screwed as I don't know what else to do:
1 // Fig. 6.8: Craps.java
2 // Craps class simulates the dice game craps.
3 import java.util.Random;
Fig. 6.8 | Craps class simulates the dice game craps. (Part 1 of 3.)
public class Craps
6 {
7 // create random number generator for use in method rollDice
8 private static final Random randomNumbers = new Random();
9
10
11
12
13 // constants that represent common rolls of the dice
14
15
16
17
18
19
20 // plays one game of craps
21 public static void main( String[] args )
22 {
23 int myPoint = 0; // point if no win or loss on first roll
24
25
26
27
28 // determine game status and point based on first roll
29 switch ( sumOfDice )
30 {
31
32
33
34 break;
35
36
37
38
39 break;
40
41
42
43 System.out.printf( "Point is %d ", myPoint );
44 break; // optional at end of switch
45 } // end switch
46
47 // while game is not complete
48 while ( ) // not WON or LOST
49 {
50
51
52 // determine game status
53 if ( sumOfDice == myPoint ) // win by making point
54 ;
Fig. 6.8 | Craps class simulates the dice game craps. (Part 2 of 3.)
55 else
56 if ( sumOfDice == SEVEN ) // lose by rolling 7 before point
57
58 } // end while
59
60 // display won or lost message
61 if ( )
62 System.out.println( "Player wins" );
63 else
64 System.out.println( "Player loses" );
65 } // end main
66
67 // roll dice, calculate sum and display results
68
69 {
70 // pick random die values
71 int die1 = 1 + randomNumbers.nextInt( 6 ); // first die roll
72 int die2 = 1 + randomNumbers.nextInt( 6 ); // second die roll
73
74 int sum = die1 + die2; // sum of die values
75
76 // display results of this roll
77 System.out.printf( "Player rolled %d + %d = %d ",
78 die1, die2, sum );
79
80
81 } // end method rollDice
82 } // end class Craps
Output:
Player rolled 5 + 6 = 11
Player wins
Player rolled 5 + 4 = 9
Point is 9
Player rolled 4 + 2 = 6
Player rolled 3 + 6 = 9
Player wins
Player rolled 1 + 2 = 3
Player loses
Player rolled 2 + 6 = 8
Point is 8
Player rolled 5 + 1 = 6
Player rolled 2 + 1 = 3
Player rolled 1 + 6 = 7
Player loses
Fig. 6.8 | Craps class simulates the dice game craps. (Part 3 of 3.)
gameStatus = Status.LOST;
gameStatus == Status.WON
public static int rollDice()
return sum; // return sum of dice
Explanation / Answer
Code is given below, all three classes are included with objected oriented design features
****************************************************************************
// Craps class simulates the dice game craps.
import java.util.Random;
public class Craps
{
// create random number generator for use in method rollDice
private Random randomNumbers = new Random();
// enumeration with constants that represent the game status
private enum Status { CONTINUE, WON, LOST };
// constants that represent common rolls of the dice
private final static int SNAKE_EYES = 2;
private final static int TREY = 3;
private final static int SEVEN = 7;
private final static int YO_LEVEN = 11;
private final static int BOX_CARS = 12;
// plays one game of craps
// public void play()
public boolean play()
{
int myPoint = 0; // point if no win or loss on first roll
Status gameStatus; // can contain CONTINUE, WON or LOST
int sumOfDice = rollDice(); // first roll of the dice
// determine game status and point based on first roll
switch ( sumOfDice )
{
case SEVEN: // win with 7 on first roll
case YO_LEVEN: // win with 11 on first roll
gameStatus = Status.WON;
break;
case SNAKE_EYES: // lose with 2 on first roll
case TREY: // lose with 3 on first roll
case BOX_CARS: // lose with 12 on first roll
gameStatus = Status.LOST;
break;
default: // did not win or lose, so remember point
gameStatus = Status.CONTINUE; // game is not over
myPoint = sumOfDice; // remember the point
System.out.printf( "Point is %d ", myPoint );
break; // optional at end of switch
} // end switch
// while game is not complete
while ( gameStatus == Status.CONTINUE ) // not WON or LOST
{
sumOfDice = rollDice(); // roll dice again
// determine game status
if ( sumOfDice == myPoint ) // win by making point
gameStatus = Status.WON;
else
if ( sumOfDice == SEVEN ) // lose by rolling 7 before point
gameStatus = Status.LOST;
} // end while
// display won or lost message
// if ( gameStatus == Status.WON )
// System.out.println( "Player wins" );
// else
// System.out.println( "Player loses" );
if ( gameStatus == Status.WON )
return true;
else
return false;
} // end method play
// roll dice, calculate sum and display results
public int rollDice()
{
// pick random die values
int die1 = 1 + randomNumbers.nextInt( 6 ); // first die roll
int die2 = 1 + randomNumbers.nextInt( 6 ); // second die roll
int sum = die1 + die2; // sum of die values
// display results of this roll
System.out.printf( "Player rolled %d + %d = %d ",
die1, die2, sum );
return sum; // return sum of dice
} // end method rollDice
// public static void main(String[] args)
// {
// Craps c=new Craps();
// c.play();
// }
} // end class Craps
*********************************************************************************************
import java.util.Scanner;
public class ModifiedCraps extends Craps{
private int bankBalance=1000; //initial bank balance is 1000 dollars
private int wager=0; //wager
private int counter=0;
private boolean resultOfGame=false; //for getting result Of Game
//this is getter method bank Balance, return current bank balance
public int getBankBalance()
{
return bankBalance;
}
//updates bank balance, setter method
public void setBankBalance(int win)
{
bankBalance=bankBalance+win;
}
//updates wager....no need but of this method, can be done without it bust just because getter setter requirenment its added
public void setWager(int w)
{
wager=w;
}
//returns wager....same as above
public int getWager()
{
return wager;
}
//returns result of game
public boolean getResultOfGame()
{
return play();
}
//chatter method....instead of chosing randomly...chosing wisely...little more sophisticated
public void Chatter(int num)
{
if(num==1)
{
System.out.println("You're up big. Now's the time to cash in your chips!");
}else
{
int balance=getBankBalance();
if(balance<200) //if bankBalance is lower than 200 dollars tells you that you are broke
System.out.println("Oh, you're going for broke, huh?");
else
{
System.out.println( "Sorry. You busted!");
}
}
}
//displays result of game differently..............not needed here..you can delete if you want it
public void displayResult()
{
if(getResultOfGame())
System.out.println("Player Wins");
else
System.out.println("Player Loses");
}
//gamePlay method is imporatant one....here game is actually being played
public void gamePlay()
{
System.out.println("Enter some Wager to play the game");
Scanner in=new Scanner(System.in);
int w;
//hammering user until valid input of wager
do {
w=in.nextInt();
if(w<=0)
System.out.println("C'mon be a man place a real bait!....Try Again!");
if(w>getBankBalance())
System.out.println("Buddy, Be in your limits. You don't have that kind of money...Try Again!");
} while (w<=0 || w>getBankBalance());
setWager(wager);
//depending on result messages are printed
if(getResultOfGame())
{
wager=getWager();
System.out.println("Great...Bravo.....YOU WON");
Chatter(1);
setBankBalance(wager);
}
else
{
wager=getWager();
System.out.println("Bad Luck...YOU LOST!");
Chatter(0);
setBankBalance(wager);
}
//part where we ask user if he/she want to play again
Scanner l=new Scanner(System.in); //different scanner object is used due to java internal issue
System.out.println("Do you wanna try your luck again?(Y/N)");
String ans=l.nextLine();
doYou(ans);
}
//method decides if game is to be continued or ended
public void doYou(String ans)
{
//int ans=in.nextInt();
if(counter<=1 && getBankBalance()>0)
{
if(ans.equalsIgnoreCase("yes") ||ans.equalsIgnoreCase("y"))
{
counter=0;
gamePlay();
}else{
System.out.println("Aw c'mon, take a chance!");
counter++;
// System.out.println("counter is: "+counter);
Scanner l=new Scanner(System.in);
System.out.println("Do you wanna try your luck again?(Y/N)");
ans=l.nextLine();
doYou(ans);
}
}else if(getBankBalance()<=0)
{
System.out.println("Sorry, You don't have enough balance");
}
else{
System.out.println("Thanks for playing");
}
}
}
********************************************************************************************
public class Simulate{
public static void main(String[] args)
{
ModifiedCraps m=new ModifiedCraps();
System.out.println("Hey There!..Welcome to my Game....Hope you know the Rules");
m.gamePlay();
}
}
*************************************************************************************
Most of the things are done, all getter and setter methods are provided in modifiedCrap class
chatter is not so random so comment on this answer i will provide the chatter method..can't provide not because i am short of time right now..thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.