Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objectives Practice using standard input Practice random number generation Pract

ID: 3883510 • Letter: O

Question

Objectives Practice using standard input Practice random number generation Practice using loops and comparison Background How many times do you have to roll a pair of dice before they come up snake eyes? You could do the experiment by rolling the dice by hand. Tasks Write a computer program that simulates rolling two dice and report the number of rolls that it makes before a pair of dice come up "snake eyes" (both roll a one) In addition, report a message whenever the simulated dice roll is "boxcars" (both roll sixes) or Yo-leven" (one five and one six) What if you wasted the user to be able to choose how many dice were used in the experiment? How might your program have to be changed? Turn In A printout of your class file, appropriately commented Upload each "java" like to the Canvas assignment for Lab 2

Explanation / Answer

/*
* The java program that simulates
* the two dice upto generate snakeeyes
* */
//Task1.java

import java.util.Random;
public class Task1
{
   public static void main(String[] args)
   {
       //Create an instance of Random class
       Random rand=new Random();
       //declaration of integer variable
       int d1,d2;
       int numrolls=0;
       //set a boolean value to false
       boolean snakeeyes=false;
       //run the loop until snakeeyes is true
       while(!snakeeyes)
       {
           //generate a random number
           //in a range of 1 and 6
           d1=rand.nextInt(6)+1;
           d2=rand.nextInt(6)+1;
           //increment the numrolls by 1
           numrolls=numrolls+1;
           //checking for snakeeyes(both dice 1)
           if(d1==1 && d2==1)
               snakeeyes=true;
       }
       System.out.println("Number of rolls to make snakeeyes");
       //print numrolls to console
       System.out.println(numrolls);
   }

}

------------------------------------------------------------------------------------------

Sample output:

Number of rolls to make snakeeyes
30

------------------------------------------------------------------------------------------


/**
* The java program that simulates
* two dice and check for boxcars(both are 6)
* and Yo-leven(one die is 5 and one die is 6)
* */
//Task2.java
import java.util.Random;
public class Task2
{
   public static void main(String[] args)
   {
       //Create an instance of Random class
       Random rand=new Random();      
       int d1,d2;
       int numrolls=0;
       boolean snakeeyes=false;
       //run the loop until snakeeyes is true
       while(!snakeeyes)
       {
           d1=rand.nextInt(6)+1;
           d2=rand.nextInt(6)+1;
           numrolls=numrolls+1;
          
           if(d1==1 && d2==1)
               snakeeyes=true;
           if(d1==6 && d2==6)
               System.out.println("boxcars");
           if(d1==5 && d2==6)              
               System.out.println("Yo-leven");
                      
       }      
       System.out.println("Number of rolls to make snakeeyes");
       System.out.println(numrolls);      
   }
}

Sample output:

Yo-leven
Yo-leven
Number of rolls to make snakeeyes
32

------------------------------------------------------------------------------------------

/**
* The java program that prompts user to enter
* number of times to roll dice and prints
* whether whether snakeeyes,boxcar or Yo-leven
* occurs
* */
//Task3.java
import java.util.Random;
import java.util.Scanner;
public class Task3
{
   public static void main(String[] args)
   {
       //Create an instance of Scanner class
       Scanner scanner=new Scanner(System.in);
       //Create a instance of Random class
       Random rand=new Random();
       //declare integer variable
       int d1,d2;
       int numrolls=0;
       int N;
       boolean snakeeyes=false;
       System.out.println("Enter number of times to roll dice");
       N=scanner.nextInt();
       for (int i = 0; i < N; i++)
       {
           d1=rand.nextInt(6)+1;
           d2=rand.nextInt(6)+1;
           //increment the numrolls by 1
           numrolls=numrolls+1;
          
           if(d1==1 && d2==1)
               snakeeyes=true;
           if(d1==6 && d2==6)
               System.out.println("boxcars");
           if(d1==1 && d2==6)              
               System.out.println("Yo-leven");
                      
       }  
       System.out.println("Number of rolls to make snakeeyes");
       System.out.println(numrolls);  
   }
}

Sample Output:

Enter number of times to roll dice
50
boxcars
Yo-leven
Number of rolls to make snakeeyes
50