Question

Please write the following 4 programs in Java. [Programs completed but with errors, could someone re-create...

Please write the following 4 programs in Java. [Programs completed but with errors, could someone re-create it?]

Please make it as simple as possible. [I am a beginner]

1. How would you use random numbers to represent the drawing of a playing card? Write a loop to test your answer by generating 100 randomly drawn cards and verify that they are in the correct range by printing and looking them over.



2. How would you use random numbers to represent the roll of a six-sided die? Write a loop to test your answer by generating 25 random rolls and verify that they are in the correct range by printing and looking them over.




3. How would you use random numbers to represent the roll of two six-sided dice? Write a loop to test your answer by generating 50 random rolls and verify that they are in the correct range by printing and looking them over.




4. Write a loop to generate 100 random numbers between 0 and 1000, and then display output which would be similar to the following:

Your random number test produced the following results:

Average: 104

Sum: 10444

Min: 23

Max: 900

[Thank you]

0 0
Add a comment Improve this question Transcribed image text
Answer #1
abc
package abc1;
import java.util.Random;

public class RandomProgram {

        public static void main(String[] args) {
                Random generator = new Random();
                
                //# Program 1
                
                int face=0, suit=0;
                int i=0;
                String[] suits = {"Spade", "Heart", "Diamod", "Club"};
                String[] faces = {"Ace","2","3","4","5","6","7","8","9","10","J","K","Q"};
                while(i<100) {
                        face = generator.nextInt(13);           //13 cards in a suit
                        suit = generator.nextInt(4);            //4 suits
                        System.out.println(suits[suit] + " - " + faces[face]);
                        i++;
                }
                
                //# Program 2
                i=0;
                while(i<25) {
                        int x = 1 + generator.nextInt(6);
                        System.out.println(x);
                        i++;
                }
                
                //# Program 3
                i=0;
                while(i<50) {
                        int x = 1 + generator.nextInt(6);
                        int y = 1 + generator.nextInt(6);
                        System.out.println(x + " - " + y);
                        i++;
                }
                
                //# Program 4
                i=0;
                double sum = 0;
                double average = 0;
                int min =0, max = 0;
                while(i<100) {
                        int x = generator.nextInt(1000+1);
                        sum += x;
                        if(x<min)
                                min = x;
                        if(x>max)
                                max = x;
                        i++;
                }
                System.out.println("Your random number test produced the following results: ");
                System.out.println("Average: " + sum/100);
                System.out.println("Sum: " + (int)sum);
                System.out.println("Min: " + min);
                System.out.println("Max: " + max);
        }

}

Add a comment
Know the answer?
Add Answer to:
Please write the following 4 programs in Java. [Programs completed but with errors, could someone re-create...
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
  • question 2 in C programming please PE-05b-01 (Six-sider) write a counter-controlled program that prompts the user...

    question 2 in C programming please PE-05b-01 (Six-sider) write a counter-controlled program that prompts the user to enter the number of times n to roll a six-sided die. The program should print to the screen the iteration of the loop and result for each roll 1 ) How many times would you like to roll? 3 roll 6 6 5 1 2) PE 05b 02 (Flash Cards) Write a counter-controlled program that creates addition problems for an elementary kid to...

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

  • Please Use Python and provide explanations!!!! 1. Write a function called multiply_and_print that takes two numbers,...

    Please Use Python and provide explanations!!!! 1. Write a function called multiply_and_print that takes two numbers, multiplies them together, and prints out the sentence '____ times ____ is ____'. Ask the user for two numbers, and call multiply_and_print with their two numbers as arguments. 2.Write a function called roll_dice that rolls two six-sided dice and prints out the result of each die. Call it three times. (Remember, you can import the random module with the statement import random, and then...

  • . 9. 10. 1. 11. 12. 13. 1. 14. 15. 16 2.1.3.4.1.5. 16. 17:18 Write a...

    . 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 many times It takes to roll two numbers with a single 20 sided dice. The parameters to the method are as follows: into The first number you are trying to roll int m The second number you are trying to roll Randomr A Random object to simulate the dice rolls. Please see the documentation for...

  • Write a Java program that deals a five-card poker hand and then determines which of the...

    Write a Java program that deals a five-card poker hand and then determines which of the following hands are contained in it: high card, pairs, two pair, three of a kind, flush. Test only for those hands. Use the numbers from 0 to 51 to represent the cards of the poker deck. 1. To deal the cards, your main method should call a secondary method that selects a random card. The secondary method should accept 5 integers that represent the...

  • Write a program, which reads a list of student records from a file in batch mode....

    Write a program, which reads a list of student records from a file in batch mode. Each student record comprises a roll number and student name, and the student records are separated by commas. An example data in the file is given below. · SP18-BCS-050 (Ali Hussan Butt), SP19-BCS-154 (Huma Khalid), FA19-BSE-111 (Muhammad Asim Ali), SP20-BSE-090 (Muhammad Wajid), SP17-BCS-014 (Adil Jameel) The program should store students roll numbers and names separately in 2 parallel arrays named names and rolls, i.e....

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

  • Can someone upload a picture of the code in matlab Write a "Guess My Number Game"...

    Can someone upload a picture of the code in matlab Write a "Guess My Number Game" program. The program generates a random integer in a specified range, and the user (the player) has to guess the number. The program allows the use to play as many times as he/she would like; at the conclusion of each game, the program asks whether the player wants to play again The basic algorithm is: 1. The program starts by printing instructions on the...

  • Exercise 2 Write a program that simulates the Texas Powerball lottery. You will have to do resear...

    I HAVE A PROBLE WITH THIS JAVA PROBLEM CAN ANYONE HELP ME PLEASE. Exercise 2 Write a program that simulates the Texas Powerball lottery. You will have to do research to find out how many numbers are drawn, and what the range(s) of the numbers are. For this exercise you need to create a class called "Powerbali", which exposes a public function called play'. This function accepts several parameters , which represent the individual numbers the player wants to play....

  • Random Number Generation and Static Methods Write a program that simulates the flipping of a coin...

    Random Number Generation and Static Methods Write a program that simulates the flipping of a coin n-times to see how often it comes up heads or tails. Ask the user to type in the number of flips (n) and print out the number of times the coin lands on head and tail. Use the method ‘random()’ from the Math class to generate the random numbers, and assume that a number less than 0.5 represents a tail, and a number bigger...

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