Question

In: Computer Science

Need to make Java code as following: Create a dice playing threading program to do the...

Need to make Java code as following:

Create a dice playing threading program to do the following:

1. Start a thread for a process to simulate a computer rolling a die 1000 times.

2. Start another thread for a process to simulate a user rolling a die 1000 times.

3. Keep track of rolls for user and computer in an array(s).

4. Display on screen when the computer thread starts, the rolls, and when the computer thread ends.

5. Display on screen when the user thread starts, the rolls, and when the user thread ends.

6. Once the rolls are completed for both computer and user, use the array(s) to count number of computer wins, number of user wins, and ties. Each element of one array is compared to each element in the other array by index. So computerRoll[0] is compared to userRoll[0], etc.

Solutions

Expert Solution

import java.util.Random;

public class DiceRoll {
    static int userRolls[] = new int[1000];
    static int computerRolls[] = new int[1000];

    static class UserThread implements Runnable {

        @Override
        public void run() {
            System.out.println("User thread started.");
            Random random = new Random();
            for (int i = 0; i < 1000; i++) {
                userRolls[i] = random.nextInt(6) + 1;
                System.out.println("User dice rolled, Output: " + userRolls[i]);
            }
            System.out.println("User thread ended.");
        }
    }

    static class ComputerThread implements Runnable {

        @Override
        public void run() {
            System.out.println("Computer thread started.");
            Random random = new Random();
            for (int i = 0; i < 1000; i++) {
                computerRolls[i] = random.nextInt(6) + 1;
                System.out.println("Computer dice rolled, Output: " + computerRolls[i]);
            }
            System.out.println("Computer thread ended.");
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread computerThread = new Thread(new ComputerThread());
        Thread userThread = new Thread(new UserThread());
        computerThread.start();
        userThread.start();
        computerThread.join();
        userThread.join();
        int computerWins = 0, userWins = 0, ties = 0;
        for (int i = 0; i < 1000; i++) {
            if (computerRolls[i] > userRolls[i])
                computerWins++;
            else if (computerRolls[i] < userRolls[i])
                userWins++;
            else
                ties++;
        }
        System.out.println("Computer Wins: " + computerWins);
        System.out.println("User Wins: " + userWins);
        System.out.println("Ties: " + ties);
    }
}


Related Solutions

Using the provided Java program below, complete the code to do the following. You may need...
Using the provided Java program below, complete the code to do the following. You may need to create other data items than what is listed for this assignment. The changes to the methods are listed in the comments above the method names. TODO comments exist. Apply any TODO tasks and remove the TODO comments when complete. Modify the method findMyCurrency() to do the following:    a. Use a do-while loop b. Prompt for a currency to find. c. Inside this...
Create a simple dice game in Java. Add screenshots and the program here.
Create a simple dice game in Java. Add screenshots and the program here.
Create a java program that will do the following: Create a method called getInt.Allow the user...
Create a java program that will do the following: Create a method called getInt.Allow the user to enter up to 20 student names,and for each student 3 quiz scores (in the range 0-100). Once input is done, display each student’s name, their three quiz scores, and their quiz score average, one student per line. The output table does not need to line up perfectly in columns.Use dialog boxes for all input and output.Use the method to input the three scores.Parameter...
in java we need to order a list , if we create a program in java...
in java we need to order a list , if we create a program in java what  are the possible ways of telling your program how to move the numbers in the list to make it sorted, where each way provides the required result. list the name of sorting with short explanation
I need the Java Code and Flowchart for the following program: Suppose you are given a...
I need the Java Code and Flowchart for the following program: Suppose you are given a 6-by-6 matrix filled with 0s and 1s. All rows and all columns have an even number of 1s. Let the user flip one cell (i.e., flip from 1 to 0 or from 0 to 1) and write a program to find which cell was flipped. Your program should prompt the user to enter a 6-by-6 array with 0s and 1s and find the first...
Create a JAVA code program: Huffman code will be converted to its text equivalent Output an...
Create a JAVA code program: Huffman code will be converted to its text equivalent Output an error message if the input cannot be converted I can give an thumbs up! :)
Using java, I need to make a program that reverses a file. The program will read...
Using java, I need to make a program that reverses a file. The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order) EX: Hello World                       eyB       Bye            becomes    dlroW olleH I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and...
//Complete the incomplete methods in the java code //You will need to create a driver to...
//Complete the incomplete methods in the java code //You will need to create a driver to test this. public class ManagedArray { private int[] managedIntegerArray; //this is the array that we are managing private int maximumSize; //this will hold the size of the array private int currentSize = 0; //this will keep track of what positions in the array have been used private final int DEFAULT_SIZE = 10; //the default size of the array public ManagedArray()//default constructor initializes array to...
In Java, need to create a program with Body Mass Index (BMI) is a measure of...
In Java, need to create a program with Body Mass Index (BMI) is a measure of health on weight. It can be calculated by taking your weight in kilograms and dividing, by the square of your height in meters. Write a program that prompts the user to enter a weight in pounds and height in inches and displays the BMI. Note one pound is 0.45359237 kilograms and one inch is 0.0254 meters. Here is a sample run: Enter weight in...
Make a java program of Mickey I have the starter program but I need to add...
Make a java program of Mickey I have the starter program but I need to add eyes and a smile to it. import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import javax.swing.JFrame; public class Mickey extends Canvas { public static void main(String[] args) { JFrame frame = new JFrame("Mickey Mouse"); Canvas canvas = new Mickey(); canvas.setSize(400, 400); canvas.setBackground(Color.white); frame.add(canvas); frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { Rectangle bb = new Rectangle(100, 100, 200, 200); mickey(g, bb); } public void...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT