Question

JAVA. Rolling a die 60,000 times with streams. Modify the code below to capture the time before a...

JAVA. Rolling a die 60,000 times with streams.

Modify the code below to capture the time before and after evaluating the stream pipeline (use package java.time which contains instant and duration). Then calculate the difference between the Instants to determine the total time. Use Instant's static method now to get the current time. To determine the difference between two Instants, use class Duration's static method between, which returns a Duration object containing the time difference. Duration provides methods like toMillis to return a duration in milliseconds. Use these timing techniques to time the original stream pipeline's operation, then do so again using class Random from package java.util, rather than SecureRandom.

package randomintstream;

import java.security.SecureRandom;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.time.LocalDateTime;


public class RandomIntStream {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SecureRandom random = new SecureRandom();
  
//roll a die 60,000 times and summarize the results
System.out.printf("%-6s%s%n", "Face", "Frequency");
random.ints(60_000_000, 1, 7)
.boxed()
.collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()))
.forEach((face, frequency) ->
System.out.printf("%-6d%d%n", face, frequency));
}
  
}

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

Program

import java.security.SecureRandom;
import java.time.Duration;
import java.time.Instant;
import java.util.Random;
import java.util.function.Function;
import java.util.stream.Collectors;

public class KuchBhi {

   /**
   * @param args the command line arguments
   */
   public static void main(String[] args) {
       //SecureRandom random = new SecureRandom();
       Random random = new Random();
      
       Instant start = Instant.now();//Start instant
      
       //roll a die 60,000 times and summarize the results
       System.out.printf("%-6s%s%n", "Face", "Frequency");
       random.ints(10000, 1, 7).boxed().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
               .forEach((face, frequency) -> System.out.printf("%-6d%d%n", face, frequency));
      
       Instant end = Instant.now();//End instant
      
       //calculating duration
       long timeElapsed = Duration.between(start, end).toMillis();
      
       System.out.println("Time Taken: "+timeElapsed+" milliseconds");
   }
}

Outputs

e Test、巴src 、申(default package) > Q KuchBhì ··; main(String[l) : void 1 import java.security.SecureRandom; 2 import java.time1 import java.security SecureRandom; 2 import java.time.Duration; 3 import java.time.Instant; 4 import java.util.Random 5 impTest ig src (default package. Q KuchBhi 1 import java.security.SecureRandom; 2 import java.time.Duration; 3 import java.time.

Add a comment
Know the answer?
Add Answer to:
JAVA. Rolling a die 60,000 times with streams. Modify the code below to capture the time before 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 question, my questions are in the following codes with comments, just answer next or under...

    java question, my questions are in the following codes with comments, just answer next or under it thanks! theres only few, so no worry. import java.security.SecureRandom; public class clsTestRand {    public static void main(String[] args) {        // TODO Auto-generated method stub        //int num1, num2, num3, num4, num5, num6;        int num1 = 0;        int num2 = 0;        int num3 = 0;        int num4 = 0;        int...

  • Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button...

    Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button just hit enter on keyboard to try number -Remove Button Quit import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; // Main Class public class GuessGame extends JFrame { // Declare class variables private static final long serialVersionUID = 1L; public static Object prompt1; private JTextField userInput; private JLabel comment = new JLabel(" "); private JLabel comment2 = new JLabel(" "); private int...

  • Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack...

    Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack class, which implements a BlackJack game. Please do not use any java applet on the coding. Hint: Use a test class to test above classes. Pulic class Card {    private final String face; // face of card ("Ace", "Deuce", ...)    private final String suit; // suit of card ("Hearts", "Diamonds", ...)    // two-argument constructor initializes card's face and suit    public...

  • I have to modify a server program and chat program to work as the following instructions...

    I have to modify a server program and chat program to work as the following instructions but I am completely clueless as to where to start. I'd appreciate any help on how to atleast get started. This must be done in java. Diffie-Hellman Two parties use a key agreement protocol to generate identical secret keys for encryption without ever having to transmit the secret key. The protocol works by both parties agreeing on a set of values (a) and (q)....

  • So I can not get this to work properly and he does not want us using...

    So I can not get this to work properly and he does not want us using an array list. Below is the directions along with the code I have(Im not sure the code is in the right format)   ITSE 2321 – OBJECT-ORIENTED PROGRAMMING JAVA Program 7 – Methods: A Deeper Look Write a program that will help an elementary school student learn multiplication. Use a SecureRandom object to produce two positive one-digit integers. The program should then prompt the student...

  • JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time a...

    JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time at your location. Also include a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutes methods. Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you livein California, a new WorldCLock(3) should show the time in NewYork, three time zones ahead. Include an Alarm feature in the Clock class....

  • Must be done in Java. Provide the rest of the code with full comments and explanation and with proper indentation. Use...

    Must be done in Java. Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. CODE PROVIDED: import java.util.ArrayList; import java.util.Scanner; public class Problem3 {    public static void main( String [] args ) {        Scanner in = new Scanner( System.in );        //PLEASE START YOUR WORK HERE        //**********************************************************                                                  //**********************************************************        // PLEASE END YOUR WORK HERE        System.out.print("END OF OUTPUT");    } } 20 points! PROBLEM 3: EXTENDED FAMILY Complete this...

  • How do i write the pseudocode for this java code? First, write out pseudocode, and then create a program to help you by accomplishing the following tasks: :  Use command line interface to ask the use...

    How do i write the pseudocode for this java code? First, write out pseudocode, and then create a program to help you by accomplishing the following tasks: :  Use command line interface to ask the user to input the following. ○ How many apples are on hand ○ How many apples should be in stock ○ How many oranges are on hand ○ How many oranges should be in stock  Perform an operation to determine how many of...

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