You are going to create a program that generates SuperLotto lottery numbers. Cre
ID: 3879893 • Letter: Y
Question
You are going to create a program that generates SuperLotto lottery numbers. Create a class called SuperLottoPlus.java Create a method called generateNumbers that returns an String containing 6 random SuperLotto lottery numbers Method must have correct method signature [10 pt] The first 5 numbers must be random numbers from the range 1 to 47, and the 6th number (the MEGA) must be from 1 to 27. [10 pt] Method returns a String of lotto numbers in the format: N N N N N (MEGA: N). For example, 47 22 25 4 13 (MEGA: 14) [20 pt] There should be NO duplicates numbers in the first 5 numbers. You can write helper methods to achieve this. It is fine for the mega numberto be a duplicate. [20 pt] In the main method Ask the user how many lotto tickets they want, and store that number as a variable [10 pt] If the user enters 5, for example, then use a looping structure to loop 5 times. [10 pt] In each loop iteration, call the generateNumbers method and print out the numbers to the screen [10 pt] Add Javadoc style comments to your class, and all your methods. [10 pt] Hint: Use the Random class to create random numbers Example output: How many Super Lotto tickets do you want? 4 28 8 11 9 28 MEGA (4) 38 39 29 47 27 MEGA (27) 15 17 33 12 6 MEGA (19) 30 29 18 20 31 MEGA (12)
Explanation / Answer
import java.util.*;
import java.util.Scanner;
public class SuperLottoPlus{
public static void generateNumbers(){
Set<Integer> listOfRandom = new HashSet<Integer>();
Random rand = new Random();
while (listOfRandom.size() != 5) {
int x = rand.nextInt(47)+1;
if ((listOfRandom.contains(x) || listOfRandom.contains(0))) {
} else {
listOfRandom.add(x);
}
}
int a = rand.nextInt(27)+1;
listOfRandom.add(a);
System.out.println(listOfRandom);
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=0;i<n;i++){
generateNumbers();
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.