Question

. 9. 10. 1. 11. 12. 13. 1. 14. 15. 16 2.1.3.4.1.5. 16. 17:18 Write a int method rolID20(int int m, Randomr) that returns how
0 0
Add a comment Improve this question Transcribed image text
Answer #1
Abc

Abc_Roll.java

import java.util.Random;

public class Abc_Roll {
   public static int rollD20(int n, int m, Random r)
   {
       //both n and m should be in the range of [1-20].
       if(n>20 || n<=0 || m>20 || m<=0)
       {
           System.out.println("Invalid numbers in parameter");
           return -1;
       }
       //variable to count the number of rolls.
       int count = 0;
       //flag variables to check when the first and second numbers have been rolled.
       int firstnum = 0;
       int secondnum = 0;
      
       //keep looping till the time we have rolled both the given numbers
       while(true)
       {
           //roll the next number between [1-20]
           int generate = r.nextInt(20)+1;
           count++;
           //the first time the first number is rolled.
           if(generate == n && firstnum == 0)
           {
               System.out.println("First number rolled!");
               firstnum = 1;
           }
           //the first time the second number is rolled.
           else if(generate == m && secondnum == 0)
           {
               System.out.println("Second number rolled!");
               secondnum = 1;
           }
           else
               System.out.println("You rolled a <"+generate+">!");
          
           //ending the loop when both the numbers are rolled.
           if(firstnum == 1 && secondnum == 1)
               break;
       }
       //print the given statements as given in the question.
       if(n == m)
           System.out.println("It took "+count+" rolls to get two "+n+"'s.");
       else
           System.out.println("It took "+count+" rolls to get a "+n+" and a "+m+".");
      
       return count;
   }
   public static void main(String[] args) {
       Random r = new Random();
       rollD20(5,7,r);
   }

}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
. 9. 10. 1. 11. 12. 13. 1. 14. 15. 16 2.1.3.4.1.5. 16. 17:18 Write a...
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
  • Java programming Write a simulation of the Craps dice game. Craps is a dice game that...

    Java programming Write a simulation of the Craps dice game. Craps is a dice game that revolves around rolling two six-sided dice in an attempt to roll a particular number. Wins and losses are determined by rolling the dice. This assignment gives practice for: printing, loops, variables, if-statements or switch statements, generating random numbers, methods, and classes. Craps game rules: First roll: The first roll (“come-out roll”) wins if the total is a 7 or 11. The first roll loses...

  • 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,...

  • Write a c++ program that simulates a million of games in craps. I am having a...

    Write a c++ program that simulates a million of games in craps. I am having a trouble getting to loop a million of times. I am trying to use a const for a million. This is the code so far. #include <iostream> #include <cstdlib>// contains prototypes for functions srand and rand #include <ctime>// contains prototype for function time #include <iomanip> using namespace std; int rollDice(); // rolls dice, calculates and displays sum void printstats(); int totroll = 0, games, point,...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

  • Craps

    Write a C++ game that plays Craps. Craps is a game played with a pair of dice. The shooter (the player with the dice) rolls a pair of dice and the number of spots showing on the two upward faces are added up. If the opening roll (called the ‘come out roll’) is a 7 or 11, the shooter wins the game. If the opening roll results in a 2 (snake eyes), 3 or 12 (box cars), the shooter loses,...

  • Hello, Could you please help me code this program in Java? It is important that all...

    Hello, Could you please help me code this program in Java? It is important that all rules are carefully followed. Using the PairOfDice class from PP 4.9, design and implement a class to play a game called Pig. In this game, the user competes against the computer. On each turn, the current player rolls a pair of dice and accumulates points. The goal is to reach 100 points before your opponent does. If, on any turn, the player rolls a...

  • need help with dice excercise For example, if we were writing a calendar program of some...

    need help with dice excercise For example, if we were writing a calendar program of some sort, we might very well use an array with 366 spots to hold the days, not worrying about adding more days to the year. Exercise 23 (C level] For another example, suppose we want to simulate rolling two six-sided dice repeatedly, tallying how many times each total is rolled. We need a memory cell to count the number of times each of the possible...

  • 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) The...

    7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) The human heart is found in which location? 10 Between the right midclavicular line and the right border of the sternum and below the clavicle to the eighth rib 20 Between the second and the fifth intercostal spaces from the right edge of the sternum to the loft midclavicular line 30 Between the third and sixth intercostal spaces from the left midclavicular line to the...

  • Previous 11 12 13 14 15 16 17 18 Question 16 of 18 (1 point) View...

    Previous 11 12 13 14 15 16 17 18 Question 16 of 18 (1 point) View problem in a pop-up License plates: In a certain state, license plates consist of three digts from 0 to 9 followed by Next three letters. Assume the numbers and letters are chosen at random. Replicates are allowed. Formulas Part 1 How many different license plates can be formed? The number of different license plates is 17576000 Part 2 out of 3 How many different...

  • Requirements: 1. You are not allowed to use global variables. 2. Must declare the following two...

    Requirements: 1. You are not allowed to use global variables. 2. Must declare the following two constants public static final int POINTS = 30; public static final int FORFEIT_POINTS = 20; 3. You can implement your own solution but you are required to break down the problem into smaller pieces. 4. You must provide the output for two rounds of game minimum. 5. Since we are using the Random number generator your output will not be exactly like mine. However,...

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