Question

objective: create methods that receive arrays as arguments. Problem Statement: Roll a 6-side die a user specified number of times and keep track of the number of times each value appears. Simulate rolling the die by generating a random number in the range of 1 to 6. After rolling the die the needed number of rolls, display a histogram of the distribution of roll values See the sample output below How many times o you want to ro the die Max value s 1 00 TRY AGAIN! Hou many times do you want to roll the die? [Max value is 100] 15 Lets get rolling The number of 1s 4 The number of 2s The number of 3s The number of 4 The number of 5s: 4 The number of 6s: 2 To begin, create a new project called Lab9-RollDie. Download and add the Roll Diejava starter code provided on Blackboard.

the RollDie.java:

import java.util.Random;
import java.util.Scanner;

/*
 *   HEADER HERE !!
 */
public class RollDie {

        public static void main(String[] args) {
                
                Scanner scnr = new Scanner(System.in);
                
                // Step 1: Declare and initialize array and 
                //   initialize random number generator
                int[] rollValueCounts = new int[7]; 
                Random randGen = new Random();

        
                // Step 2: Determine the number of rolls 
                System.out.print("How many times do you want to roll the die?");
                System.out.print(" [Max value is 100] ");
                int numRolls = scnr.nextInt();
                // TODO: Check that user provided value is valid 
                //  (i.e., in the range 0-100) and if not reprompt
                
                System.out.println("Let's get rolling...");
                
                // Step 3: Generate numRolls random values between 1-6
                for(int i = 0; i < numRolls; i++) {
                        int rollValue = randGen.nextInt(6) + 1;
                        /* The rollValue will serve as the index and at that
                           location in the array we will keep the number of rolls 
                           for that value. 
                         */
                        rollValueCounts[rollValue]++;
                }
                
                // TODO:
                // Step 4: Display the roll counts as a histogram
                
                //printTable(rollValueCounts);
                
        }
        
        // FOR LAB, CREATE A NEW METHOD TO DISPLAY THE ROLL COUNTS
        //  AS A HISTOGRAM.  BE SURE TO STATE STEPS IN THE METHOD AND
        //  PROVIDE A HEADER
        
        
        /**************************************************************************
         * printTable - Prints the contents of an integer array as a table
         * 
         * int[] data - The data to be displayed on the screen
         */
        public static void printTable(int[] data) {
                


                for(int i = 0; i < data.length + 1; i++) {
                           System.out.print("-----");
                }
                System.out.print("\nIndex");
                for(int i = 0; i < data.length; i++) {
                        System.out.printf("|%4d", i);
                }
                System.out.print("\nValue");
                for(int i = 0; i < data.length; i++) {
                        System.out.printf("|%4d", data[i]);
                }
                System.out.println();
                for(int i = 0; i < data.length+1; i++) {
                        System.out.print("-----");
                }
        }

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
package com.company;
import java.util.Random;
import java.util.Scanner;

/*
 *   HEADER HERE !!
 */
public class RollDie {

    public static void main(String[] args) {

        Scanner scnr = new Scanner(System.in);

        // Step 1: Declare and initialize array and
        //   initialize random number generator
        int[] rollValueCounts = new int[7];
        Random randGen = new Random();
        int numRolls;
        // Step 2: Determine the number of rolls

 //  (in the range 0-100) and if not reprompt

        while(true) {
            System.out.print("How many times do you want to roll the die?");
            System.out.print(" [Max value is 100] ");
             numRolls = scnr.nextInt();
            if(numRolls>100)
                System.out.println("Try Again!");
            else
                break;
        }

          

            System.out.println("Let's get rolling...");

            // Step 3: Generate numRolls random values between 1-6
            for (int i = 0; i < numRolls; i++) {
                int rollValue = randGen.nextInt(6) + 1;
                        /* The rollValue will serve as the index and at that
                           location in the array we will keep the number of rolls
                           for that value.
                         */
                rollValueCounts[rollValue]++;


            }

           
            // Step 4: Display the roll counts as a histogram

            printTable(rollValueCounts);
        }


    /**************************************************************************
     * printTable - Display the roll counts as a histogram

     *
     * int[] data - The data to be displayed on the screen
     */

    public static void printTable(int[] data) {


        System.out.print("The number of 0's :" +data[0]);
        for (int i = 0; i <data[0] ; i++) {

            System.out.print(" *");
        }
        System.out.println();
System.out.print("The number of 1's :" +data[1]);
        for (int i = 0; i <data[1] ; i++) {

            System.out.print(" *");
        }
        System.out.println();
        System.out.print("The number of 2's :" +data[2] );
        for (int i = 0; i <data[2] ; i++) {

            System.out.print(" *");
        }
        System.out.println();
        System.out.print("The number of 3's :" +data[3] );
        for (int i = 0; i <data[3] ; i++) {

            System.out.print(" *");
        }
        System.out.println();
        System.out.print("The number of 4's :" +data[4] );
        for (int i = 0; i <data[4] ; i++) {

            System.out.print(" *");
        }
        System.out.println();
        System.out.print("The number of 5's :" +data[5]);
        for (int i = 0; i <data[5] ; i++) {

            System.out.print(" *");
        }
        System.out.println();
        System.out.print("The number of 6's :" +data[6] );
        for (int i = 0; i <data[6] ; i++) {

            System.out.print(" *");
        }
        System.out.println();
    }
}

output:-->How many times do you want to roll the die? [Max value is 100] 200 Try Again! How many times do you want to roll the die? [Ma

Add a comment
Know the answer?
Add Answer to:
the RollDie.java: import java.util.Random; import java.util.Scanner; /* * HEADER HERE !! */ public class RollDie {...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • package week_4; import input.InputUtils; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Random; import static input.InputUtils.positiveIntInput; import static input.InputUtils.yesNoInput;...

    package week_4; import input.InputUtils; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Random; import static input.InputUtils.positiveIntInput; import static input.InputUtils.yesNoInput; /** Write a program to roll a set of dice. Generate a random number between 1 and 6 for each dice to be rolled, and save the values in an ArrayList. Display the total of all the dice rolled. In some games, rolling the same number on all dice has a special meaning. In your program, check if all dice have the same value,...

  • import java.util.Scanner; public class Lab6d { public static void main(String[] args) {    Scanner scnr =...

    import java.util.Scanner; public class Lab6d { public static void main(String[] args) {    Scanner scnr = new Scanner(System.in); // TODO: get user choice    // TODO: call printTable method passing choice as the parameter    } public static void printTable(int stop) { // TODO: print header    // TODO: loop to print table rows up to stop value    } Write a Java program where the main () method prompts the user to select an integer value between 1 and...

  • import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args)...

    import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args) { int[] grades = randomIntArr(10); printIntArray("grades", grades); ArrayList<Integer> indexesF_AL = selectIndexes_1(grades); System.out.println(" indexesF_AL: " + indexesF_AL); int[] indexesF_Arr = selectIndexes_2(grades); printIntArray("indexesF_Arr",indexesF_Arr); } public static int[] randomIntArr(int N){ int[] res = new int[N]; Random r = new Random(0); for(int i = 0; i < res.length; i++){ res[i] = r.nextInt(101); // r.nextInt(101) returns an in in range [0, 100] } return res; } public static void...

  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • SIMPLE: PSEUDOCODE FOR THE CODE BELOW Bingo.java package bingo; import java.util.Scanner; import java.util.Random; public class Bingo...

    SIMPLE: PSEUDOCODE FOR THE CODE BELOW Bingo.java package bingo; import java.util.Scanner; import java.util.Random; public class Bingo {        public static BingoCard gameCard;     public static int totalGamesWon = 0;        public static void main(String[] args) {               //Use do-while loop to do the following:         //1. instantiate a gameCard         //2. call the method playGame()         //3. call the method determineWinner()         //4. Ask user if they wish to play again? (1 = yes; 2 = no)...

  • Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public...

    Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public class Project2 { //Creating an random class object static Random r = new Random(); public static void main(String[] args) {    char compAns,userAns,ans; int cntUser=0,cntComp=0; /* * Creating an Scanner class object which is used to get the inputs * entered by the user */ Scanner sc = new Scanner(System.in);       System.out.println("*************************************"); System.out.println("Prime Number Guessing Game"); System.out.println("Y = Yes , N = No...

  • Please Help! I need to update the code below to meet these criteria! 1. The constructor...

    Please Help! I need to update the code below to meet these criteria! 1. The constructor of die class initializes face of Die to 3 2. roll method updates face of die to value from 1-6 3. getFace() method provides current face to the main program 4. main create a Die object 5. main create a Die object 6. main rolls die 5 times 7. main computes sum of rolls 8. main computes actual average of the rolls <--------- Code...

  • package rpsgamesimulation; import java.util.Random; import java.util.Scanner; /** * * @author cristy */ public class RPSGame {...

    package rpsgamesimulation; import java.util.Random; import java.util.Scanner; /** * * @author cristy */ public class RPSGame { private String userChoice, computerChoice;    public RPSGame() { userChoice = "rock"; computerChoice = "rock"; }    public String getUserChoice() { return userChoice; }    public String getComputerChoice() { return computerChoice; }    public void setUserChoice(String aUserChoice) { userChoice = aUserChoice; }    public void setComputerChoice(String aComputerChoice) { computerChoice = aComputerChoice; }    public String toString() { return "User Choice: " + userChoice + "...

  • Java // Topic 2c // Program reserves airline seats. import java.util.Scanner public class Plane {    //...

    Java // Topic 2c // Program reserves airline seats. import java.util.Scanner public class Plane {    // checks customers in and assigns them a boarding pass    // To the human user, Seats 1 to 2 are for First Class passengers and Seats 3 to 5 are for Economy Class passengers    //    public void reserveSeats()    {       int counter = 0;       int section = 0;       int choice = 0;       String eatRest = ""; //to hold junk in input buffer       String inName = "";      ...

  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT