Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Roll the Dice in Java Part 1: you'll build on the CoinFlip.java program. The coin simulation asked you to flip a coin 1000 times and

Roll the Dice in Java

Part 1: you'll build on the CoinFlip.java program. The coin simulation asked you to flip a coin 1000 times and report the outcomes. For Lab 1, you should create a class called DiceSim.java (or similar), which simulates the rolling of five six-sided dice 7,776 times and reports the number of Yahtzees (five of a kind) rolled.

You'll either need to use a random generator (java.util.Random) or reuse the Math.random() function, but you'll need to tweak it to simulate a die with sides 1..6 in place of a coin with only 0 and 1 as options. Make sure you don't roll any 0's or 7's...

Part2: Now build a 2-dimensional array that is 5x5 and populate it with random rolls like you did above. Once youve populated the 2-D array I want you to print the 2-D array to the output screen then print how many Yahtzees exist in each row, how many Yahtzees exist in each column and how many Yahtzees exist on the diagnals. A sample output could look like this

45631

52411

65121

41351

15321

There are 0 row Yahtzees

There are 1 column Yahtzees

There are 1 diagonal Yahtzees

Both part 1 and part 2 should reside in the same program

Submit just the Java source code file (DiceSim.java or similar) from your project's 'src' folder. The file should contain the following lines of comments at the top:

This is what i have:

_________________________________________________________

public class CoinFlip { //----------------------------------------------------------------- // Creates a Coin object, flips it, and prints the results. //----------------------------------------------------------------- public static void main(String[] args) { Coin myCoin = new Coin();

myCoin.flip();

System.out.println(myCoin);

if (myCoin.isHeads()) System.out.println("You win."); else System.out.println("Better luck next time."); } }

public class Coin { private final int HEADS = 0; private final int TAILS = 1; private int face;

public Coin() { flip(); } public void flip() { face = (int) (Math.random()*2); } public boolean isHeads() { reutrn (face == HEADS); else faceName= "Tails"; return faceName; } }

public static void main(String[] args) {

Coin2 myCoin = new Coin2(); myCoin.setKey(1111);

System.out.println("Initial: " + myCoin); myCoin.lock(1111);

System.out.println("After lock: " + myCoin); myCoin.flip(); System.out.println("After attempted flip: " + myCoin);

myCoin.unlock(1111); myCoin.flip(); System.out.println("After unlock: " + myCoin); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions