(java) Using your PairOfDice class write an application that simulates 100 pass-
ID: 3883286 • Letter: #
Question
(java) Using your PairOfDice class write an application that simulates 100 pass-line bets and output the results of each bet to an output file. Also, keep a running total of how many wins and losses. Output those totals at the end of your run. (not sure on how to implement file IO to output a separate text file, that will display how many wins and losses as well as the outputs from each roll)
pairofdice.java
import java.util.Random;
public class PairOfDice {
private int die1;
private int die2;
public PairOfDice()
{
}
public void roll()
{
Random d1 = new Random();
Random d2 = new Random();
die1 = d1.nextInt(6) + 1;
die2 = d2.nextInt(6) + 1;
}
public int getDie1()
{
return die1;
}
public int getDie2()
{
return die2;
}
public int getTotalValue()
{
return die1 + die2;
}
@Override
public String toString()
{
return die1 + "+" + die2 + " =" + getTotalValue();
}
}
craps.java
import java.io.*;
public class Craps {
private static int losses = 0;
private static int wins = 0;
static PairOfDice dice = new PairOfDice();
public static int win()
{
wins++;
return wins;
}
public static int lose()
{
losses++;
return losses;
}
public static void play()
{
for(int i = 1; i <=10; i++)
{
dice.roll();
switch (dice.getTotalValue()) {
case 2:
case 3:
System.out.println(dice);
System.out.println("Sorry you lose.");
lose();
break;
case 12:
System.out.println(dice);
System.out.println("Sorry you lose.");
lose();
break;
case 7:
case 11:
System.out.println(dice);
System.out.println("You Win!");
win();
break;
case 4:
case 5:
System.out.println(dice);
reroll();
break;
case 6:
case 8:
System.out.println(dice);
reroll();
break;
case 9:
System.out.println(dice);
case 10:
reroll();
break;
default:
break;
}
}
}
public static void reroll()
{
dice.roll();
if(dice.getTotalValue() == 4 || dice.getTotalValue() == 5)
{
System.out.println(dice);
System.out.println("You Win!");
win();
reroll();
}
if(dice.getTotalValue() == 6 || dice.getTotalValue() == 8)
{
System.out.println(dice);
System.out.println("You Win!");
win();
reroll();
}
if(dice.getTotalValue() == 9 || dice.getTotalValue() == 10)
{
System.out.println(dice);
System.out.println("You Win!");
win();
reroll();
}
else
{
System.out.println(dice);
System.out.println("Sorry you lose.");
lose();
}
}
public static void gameOver()
{
System.out.println("Game Over!");
System.out.println("You Won: " + wins + " times!");
System.out.println("You Lost: " + losses + " times!");
}
public static void main(String args[])
{
System.out.println("Good Luck!");
play();
gameOver();
}
}
Explanation / Answer
You can use file io library to write output to a separate file.
This library has functions like write to file and read from file.
2. FileInputStream in = null; // declare in file input stream variable to take input from file.
in = new FileInputStream("filename"); // like this you can open any file you want
FileOutputStream out = null; // It is a declaration for output stream
After closing file, just check current folder your output file is ready.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.