Create a java program that includes the Dice class, ChoHan class, and the playCh
ID: 3692251 • Letter: C
Question
Create a java program that includes the Dice class, ChoHan class, and the playChoHan class (see below for coding.) to test your application, compile each of the three classes and then run the playChoHan class. Once you have everything working correctly, modify the ChoHan class so that each game, after the user is prompted to enter a wager and his choice of even or odd, he or she is also prompted for the number of dice to roll. Then call the setNumDice method of the Dice class before the call to the rollDice method.
public class Dice
{
private int numDice;
private Random random;
public Dice()
{
numDice=1;
random=new Random();
}
public Dice(int n)
{
numDice=n;
random=new Random();
}
public int rollDice()
{
int sum=0;
for(int i=1;i<=numDice;i++)
sum+=random.nextInt(6)+1;
return sum;
}
public int getNumDice()
{
return numDice;
}
public void setNumDice(int n)
{
numDice=n;
}
}
public class ChoHan
{
private int winnings;
public ChoHan()
{
winnings=0;
}
private boolean win(String choice, int sum)
{
if(sum%2==0&&(choice.equals("e")||choice.equals("E")))
return true;
if(sum%2!=0&&(!(choice.equals("e")||choice.equals("E"))))
return true;
return false;
}
public void play()
{
Scanner input=new Scanner(System.in);
Dice dice=new Dice(2);
String choice;
String answer;
int wager;
do
{
System.out.print("Enter wager: ");
wager=input.nextInt();
System.out.println("Enter 'e' for even anything else for odd: ");
choice=input.next();
int sum=dice.rollDice();
System.out.println("You rolled a "+sum);
if(win(choice,sum))
{
winnings+=wager;
System.out.println("You won! Winnings so far: "+winnings);
}
else
{
winnings-=wager;
System.out.println("You lost! Winnings so far: "+winnings);
}
System.out.print(Play again? 'y' or 'Y' for 'Yes': ");
answer=input.next();
System.out.println();
}
while(answer.equals"Y")||answer.equals("y"));
reportWinnings();
}
private void reportWinnings()
{
if(winnings>0)
System.out.println("You won $"+winnings);
else if(winnings<0)
System.out.println("You lost $"+Math.abs(winnings));
else
System.out.println("You broke even");
System.out.println("Thanks for playing");
}
public class PlayChoHan
{
public static void main(String[] args)
{
ChoHan game=new ChoHan();
game.play();
}
}
Explanation / Answer
I have done required changes in ChoHan class.
All other classes do not need to change.
import java.util.Scanner;
public class ChoHan
{
private int winnings;
public ChoHan()
{
winnings=0;
}
private boolean win(String choice, int sum)
{
if(sum%2==0&&(choice.equals("e")||choice.equals("E")))
return true;
if(sum%2!=0&&(!(choice.equals("e")||choice.equals("E"))))
return true;
return false;
}
public void play()
{
Scanner input=new Scanner(System.in);
Dice dice=new Dice(2);
String choice;
String answer;
int num;
int wager;
do
{
System.out.print("Enter wager: ");
wager=input.nextInt();
System.out.println("Enter 'e' for even anything else for odd: ");
choice=input.next();
// setting number to roll dice
System.out.println("Enter number of dice to roll: ");
num = input.nextInt();
dice.setNumDice(num);
int sum=dice.rollDice();
System.out.println("You rolled a "+sum);
if(win(choice,sum))
{
winnings+=wager;
System.out.println("You won! Winnings so far: "+winnings);
}
else
{
winnings-=wager;
System.out.println("You lost! Winnings so far: "+winnings);
}
System.out.print("Play again? 'y' or 'Y' for 'Yes': ");
answer=input.next();
System.out.println();
}
while(answer.equals("Y")||answer.equals("y"));
reportWinnings();
}
private void reportWinnings()
{
if(winnings>0)
System.out.println("You won $"+winnings);
else if(winnings<0)
System.out.println("You lost $"+Math.abs(winnings));
else
System.out.println("You broke even");
System.out.println("Thanks for playing");
}
}
/*
Output:
Enter wager: 12
Enter 'e' for even anything else for odd:
e
Enter number of dice to roll:
4
You rolled a 12
You won! Winnings so far: 12
Play again? 'y' or 'Y' for 'Yes': n
You won $12
Thanks for playing
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.