Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA PROGRAMING Create a program that generates SuperLotto lottery numbers. Crea

ID: 3820359 • Letter: J

Question

JAVA PROGRAMING

Create a program that generates SuperLotto lottery numbers.

Create a class called SuperLottoPlus.java

Create a static method called generateSuperLottoNumbers() that returns an array of 6 random SuperLotto lottery numbers.

Must have correct method signature, and return the entire array of lotto numbers. [1 pt]

The first 5 numbers must be from the range 1 to 47 [1 pt]

The 6th number (the MEGA) must be from 1 to 27. [1 pt]

Create a static method called printTicket() that takes an integer array as an parameter

Must have the correct method signatures [1 pt]

it will loop through the integer array and print out the data [1 pt]

Display the data in the format: 47 22 25 4 13 (MEGA: 14)

In the main method

Ask the user how many lotto tickets they want.

If the user enters 5, for example, then using a for loop, loop 5 times. [1 pt]

In each loop iteration, call the printTicket() method, passing a call to the method generateSuperLottoNumbers() as an agrument [1 pts]

Hint: numbers[i] = (int) (10 * Math.random()) + 1; // will assign your array element to a random number from 1 to 10. You can also create the random numbers by creating an object of the Random class.

Extra Credit: Create another method called generateSuperLottoNoDupes() which does the same thing as generateSuperLottoNumbers(), but returns an array without duplicates in the first 5 numbers. It is OK for the MEGA number to be a duplicate of any of the first 5 (+1 point)

Explanation / Answer

import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

public class SuperLotto {
  
   // function to generate unique lotto tickets
   /*
   * in this function i am using the set since set has the property that each element in the set will be unique
   */
   static int[] generateSuperLottoNoDupes(){
       Set<Integer> s = new HashSet<Integer>();
       int arr[] = new int[5];
       while(s.size()!=5){
           int max=47, min=1;
           int num=0;
          
           Random r = new Random();
           if(s.size()==arr.length-2){
               max = 27;
               num = r.nextInt(max-min+1)+min;
               s.add(num);
           }
           else{
               num = r.nextInt(max-min+1)+min;
               s.add(num);
           }
       }
      
       // iterating over the set and inserting the values to array
       Iterator<Integer> itr =s.iterator();
       int i=0;
       while(itr.hasNext()){
           arr[i] = itr.next().intValue();
           i++;
       }
       return arr;
   }
   static int[] generateSuperLottoNumbers() {
       int arr[] = new int[5];
       int max = 47, min = 1;
       Random r = new Random();
       int num=0;
       for(int i=0;i<arr.length;i++){
           if(i==arr.length-1){
               max=27;
               num = r.nextInt(max-min+1)+min;
               arr[i]=num;
           }
           else
           {
               num = r.nextInt(max-min+1)+min;
               arr[i] = num;
           }
       }
      
       return arr;
   }
  
   static void printTicket(int arr[]){
       System.out.println();
       for(int i=0;i<arr.length;i++)
           System.out.print(arr[i]+" ");
   }
  
   public static void main(String args[]){
       Scanner sc = new Scanner(System.in);
       System.out.print("How many lotto tickets you want: ");
       int num = sc.nextInt();
      
       for(int i=0;i<num;i++)
           printTicket(generateSuperLottoNumbers()); // printing the lotto tickets using generateSuperLottoNumbers
      
       // printing the values of the lottery tickets generated using generateSuperLottoNoDupes()
       printTicket(generateSuperLottoNoDupes());
   }
}