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

I need to make this into a Java Code: Write a program that prompts the user...
I need to make this into a Java Code: Write a program that prompts the user for a double value representing a radius. You will use the radius to calculate: circleCircumference = 2πr circleArea = πr2 sphereArea = 4πr2 sphereVolume = 43πr3 You must use π as defined in the java.lang.Math class. Your prompt to the user to enter the number of days must be: Enter radius: Your output must be of the format: Circle Circumference = circleCircumference Circle Area...
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 has a code file with main() in it and another code...
Create a java program that has a code file with main() in it and another code file with a separate class. You will be creating objects of the class in the running program, just as the chapter example creates objects of the Account class. Your system handles employee records and processes payroll for them. Create a class called Employee that holds the following information: first name, last name, monthly salary, and sales bonus. The class should have all the gets...
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...
JAVA PROGRAM, Create the game "Rock, Scissor, Paper": Make the AI pick randomly. You need to...
JAVA PROGRAM, Create the game "Rock, Scissor, Paper": Make the AI pick randomly. You need to look up Random numbers in Java. First ask the user what language they want to play in. Choice 1 is English, but choice 2 can be whatever real language you want. Your first loop is making sure they enter good input. If there is a tie you don't give them the option to play again. They have to. Outer loop runs until they say...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT