*In Java Only Please*
15.11 LAB: Unique random integers (HashSet) Given integer inputs howMany and maxNum, generate an array of howMany unique random integers from 0 to maxNum (exclusive). The structure of the program is: main() calls uniqueRandomInts() with arguments howMany and maxNum. • uniqueRandom Ints()) returns an array of howMany unique random integers. • The required output is already provided in main() and printNums(). Complete uniqueRandom Ints(), which generates random integers until howMany unique integers have been collected in array nums. Hint: If a generated number is new, add the number to the array nums and the set alreadySeen. If the number has been seen before, increment the static variable retries and generate another random integer. Note: For testing purposes, a random number generator object is created with a fixed seed value (29) in uniqueRandomInts(). Refer to the textbook section on Random numbers to learn more about pseudo-random numbers. Ex: When the input is: 58 the output is 50 17 3 [3 retries]
1 import java.util.Scanner; 2 import java.util.HashSet; 3 import java.util.Random; 4 5 public class LabProgram { 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41} 42 // Print the numbers in array separated by a space public static void printNums (int[] nums) { for (int i 0; i alreadySeen = new HashSet(); /* Type your code here. */ return nums; int howMany int maxNum scnr.nextInt (); // Initialize static variable // Random number generator with seed 29 int[] uniqueInts = uniqueRandom Ints (howMany, maxNum); printNums (uniqueInts); System.out.printf(" [%d retries]\n", retries); // Print static variable here Load default template...
1: Compare output Output differs. See highlights below. Input Your output 2: Compare output Expected output 5 017 Your output Output differs. See highlights below. Input 35 Expected output 3: Compare output Your output 58 Expected output 00000 4: Compare output Output differs. See highlights below. Input 23 Your output 000 [0 retries] [0 retries] 341 [0 retries] [3 retries] Output differs. See highlights below. Input 00 [0 retries] 10 [2 retries] 10 14 0000000000 [0 retries] Expected output 13 4 9 7 12 2 8 11 1 0 [13 retries] 0/2 0/2 0/2 0/2
5: Compare output Output differs. See highlights below. Input Your output Expected output 10 15 0000000000 13 4 1 6 14 11 10 [0 retries] 25 [0 retries] 0/2
*In Java Only Please*
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am