Question

Dice Rollers( write java) Write a complete thread class to represent rolling dice. The thread tak...

Dice Rollers( write java)

Write a complete thread class to represent rolling dice. The thread takes a parameter that represents how many dice are being rolled at once and how many times to roll.

When the thread is run, it rolls the specified number of dice the specified number of times. The thread counts for how many rolls the number on all dice match. This value is saved as instance data.

Write a program to create and run several threads with different numbers of dice/rolls, outputting the results.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

import java.util.*;
import java.io.*;
class Mt implements Runnable
{
   private int dice,rolls;
   public Mt(int dice, int rolls)
   {
       this.dice=dice;this.rolls=rolls;
   }
   public void run()
   {
       Random rand=new Random();
       for(int j=0;j<dice;j++)//for every dice, roll it for rolls no. of times
       {
           for(int i=0;i<rolls;i++)
           {
               System.out.println("Thread: "+Thread.currentThread().getId()+" Dice:"+ (j+1) +" Roll:"+ (i+1) + " Result:"+rand.nextInt(6));
           }
       }
   }
}
public class Program11
{
   public static void main(String args[])
   {
       int n=3;// no. of threads
       for(int i=0;i<n;i++)
       {
           Thread obj=new Thread(new Mt(3,5));//3 is no. of dices, 5 is no. of rolls
           obj.start();
       }
   }
}

Output:

G\>java Program11 Thread: 9 Dice:1 Roll:1 Result:1 Thread: 9 Dice:1 Roll:2 Result:0 Thread: 9 Dice:1 Roll:3 Result:2 Thread:

Thread: 8 Dice:3 Roll:1 Result:1 Thread 8 Dice:3 Roll:2 Result:e Thread 8 Dice:3 Roll:3 Result:e Thread 8 Dice:3 Roll:4 Resul

Add a comment
Know the answer?
Add Answer to:
Dice Rollers( write java) Write a complete thread class to represent rolling dice. The thread tak...
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...

  • ( Java Programming ) Write an application to simulate the rolling of two dice. The application...

    ( Java Programming ) Write an application to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12, with 7 being the most frequent sum, and 2 and 12...

  • Write a java program that simulates the rolling of four dice. The program should use random...

    Write a java program that simulates the rolling of four dice. The program should use random number generator to roll dice. The sum of the four values should then be calculated. Note that each die can show an integer value from 1 to 6, so the sum of the four values will vary from 4 to 24, with 14 being the most frequent sum and 4 and 24 being the least frequent sums. Your program should roll the dice 12,960...

  • Write a java code that rolls three dice and counts how many times it takes to...

    Write a java code that rolls three dice and counts how many times it takes to get the same numbers in a series of runs

  • JAVA Program! Write a program that makes use of threading. You will have one thread count how many times a second has p...

    JAVA Program! Write a program that makes use of threading. You will have one thread count how many times a second has passed and the second thread will count how many times 3 seconds has passed This will occur in an infinite loop and the counts will be printed to the screen. Indent the count of one of the threads for easier viewing Write a program that makes use of threading. You will have one thread count how many times...

  • How to write python code that is a dice rolling function that generates random numbers. The...

    How to write python code that is a dice rolling function that generates random numbers. The dice rolling function takes two arguments: the first argument is the number of sides on the dice and the second argument is the number of dice. The function returns the sum of the random dice rolls. For example, if I call roll dice(6,2) it might return 7, which is the sum of randomly chosen numbers 4 and 3. It somewhere along the lines of:...

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

  • Write a Java pseudocode for a 2-dice game. This game will roll the two dice together...

    Write a Java pseudocode for a 2-dice game. This game will roll the two dice together for 100 times. In each time, it will display the face values from both dice. The program will count how many times (out of these 100 attempts) did the two dice generate a total point of 10 or above, and display the result at the end. In a word document, PPT file or a PDF file.

  • Complete each problem separately and perform in python. 1. Create a script that will roll five...

    Complete each problem separately and perform in python. 1. Create a script that will roll five dice and print out their values. Allow the user to roll the dice three times. Prompt them to hit enter to roll. Ex: Hit enter to roll 1,4,3,6,6 Hit enter to roll 3,5,1,2,1 Hit enter to roll 4,3,4,2,6 2. Write a script that will roll five dice (just one time). The user's score will be the sum of the values on the dice. Print...

  • The Dice game of "Pig" can be played with the following rules. 1. Roll two six-sided dice. Add the face values...

    The Dice game of "Pig" can be played with the following rules. 1. Roll two six-sided dice. Add the face values together. 2. Choose whether to roll the dice again or pass the dice to your opponent. 3. If you pass, then you get to bank any points earned on your turn. Those points become permanent. If you roll again, then add your result to your previous score, but you run the risk of losing all points earned since your...

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