Question

5.4 Java Create a program that generates SuperLotto lottery numbers. Create a class called SuperLottoPlus.java Create...

5.4 Java Create a program that generates SuperLotto lottery numbers.

  • Create a class called SuperLottoPlus.java
  • Create a method called generateSuperLottoNumbers() that returns an array of 6 random SuperLotto lottery numbers.
    • The first 5 numbers must be from the range 1 to 47
    • The 6th number (the MEGA) must be from 1 to 27.
  • Create a method called printTicket() that takes an integer array as an parameter
    • it will loop through the integer array and print out the data
    • 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.
    • In each loop iteration, call the printTicket() method, passing a call to the method generateSuperLottoNumbers() as an agrument
  • Hard part: 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

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)

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

import java.util.Random;
import java.util.Scanner;

class SuperLottoPlus {
   public static int[] generateSuperLottoNumbers() {
       int arr[] = new int[6];
       Random r = new Random();
       for (int i = 0; i < 5; i++)
           arr[i] = r.nextInt(47) + 1;
       arr[5] = r.nextInt(27) + 1;
       return arr;
   }

   public static void printTicket(int arr[]) {
       for (int i = 0; i < 5; i++)
           System.out.print(arr[i] + " ");
       System.out.println("(MEGA: " + arr[5] + ")");
   }

   public static int[] generateSuperLottoNoDupes() {
       int arr[] = new int[6];
       int count[] = new int[47];
       int n = 0;
       Random r = new Random();
       for (int i = 0; i < 5;) {
           n = r.nextInt(47) + 1;
           if (count[n] == 0) {
               arr[i] = n;
               count[n]++;
               i++;
           }
       }
       arr[5] = r.nextInt(27) + 1;
       return arr;
   }
}

public class SuperLottoPlusDemo {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("How many Super Lotto tickets do you want?");
       int n = sc.nextInt();
       for (int i = 0; i < n; i++)
           SuperLottoPlus.printTicket(SuperLottoPlus.generateSuperLottoNumbers());
      
       System.out.println("How many Super Lotto tickets do you want without dup?");
       n = sc.nextInt();
       for (int i = 0; i < n; i++)
           SuperLottoPlus.printTicket(SuperLottoPlus.generateSuperLottoNoDupes());
      
   }
}

Add a comment
Know the answer?
Add Answer to:
5.4 Java Create a program that generates SuperLotto lottery numbers. Create a class called SuperLottoPlus.java 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
  • Using C++ create a lotto program Lottery Design a program that simulates a lottery. Requirements:  The program...

    Using C++ create a lotto program Lottery Design a program that simulates a lottery. Requirements:  The program should have an array of 5 integers named lottery and should generate a random number in the range of 1 through 99 for each element of the array. The user should enter five digits, which should be stored in an integer array named user. The program is to compare the corresponding elements in the two arrays and keep a count of the digits that...

  • Write a Java program that generates an array of Fibonacci numbers. Specifications: The program -Fills a...

    Write a Java program that generates an array of Fibonacci numbers. Specifications: The program -Fills a one-dimensional array with the first 30 Fibonacci numbers using a calculation to generate the numbers. Note: The first two Fibonacci numbers 0 and 1 should be generated explicitly as in -long[] series = new long[limit]; //create first 2 series elements series[0] = 0; series[1] = 1; -But, it is not permissible to fill the array explicitly with the Fibonacci series’ after the first two...

  • Create a class called Play that has an InputReader as an instance variable. Be sure to...

    Create a class called Play that has an InputReader as an instance variable. Be sure to initialize it in the constructor. The class has two methods. Write a method with this signature: Write a method with this signature: public void stringPlay() The method prompts the user for a string, reads it in, and then displays the string as many times as the length of that string. The output string should be formatted with the first letter uppercase and the rest...

  • Write a Lottery class that simulates a 6-number lottery (e.g. "Lotto"). The class should have an...

    Write a Lottery class that simulates a 6-number lottery (e.g. "Lotto"). The class should have an array of six integers named lotteryNumbers, and another array of six integers named userLotteryPicks. The class' constructor should use the Random class to generate a unique random number in the range of 1 to 60 for each of the 6 elements in the lotteryNumbers array. Thus, there should be a loop that keeps generating random numbers until all 6 numbers are unique.  The Lottery class...

  • java/javafx assignment: Island Paradise just started running their city lotto. You've been tasked with writing a...

    java/javafx assignment: Island Paradise just started running their city lotto. You've been tasked with writing a program for them. The program is going to allow to user to first select their lotto numbers. The program will then randomly generate the winning lotto numbers for the week and then check the winning numbers against the random ticket the user played in the Lotto to see how many numbers the user guessed correctly. The rules for lotto work as follows: Select 7...

  • Lottery Game (15 Numbers). Design and implement a C++ program that generates 15 random non-repeating positive...

    Lottery Game (15 Numbers). Design and implement a C++ program that generates 15 random non-repeating positive integer numbers from 1 to 999 (lottery numbers) and takes a single input from the user and checks the input against the 15 lottery numbers. The user wins if her/his input matches one of the lottery numbers. Your implementation must have a level of modularity (using functions) Functions you need to implement: Define function initialize() that takes array lotterNumbers[] as a parameter and assigns...

  • Create a class called Student. This class will hold the first name, last name, and test...

    Create a class called Student. This class will hold the first name, last name, and test grades for a student. Use separate files to create the class (.h and .cpp) Private Variables Two strings for the first and last name A float pointer to hold the starting address for an array of grades An integer for the number of grades Constructor This is to be a default constructor It takes as input the first and last name, and the number...

  • in java eclipse, Create a program “FibonacciFifteen.java” and a method called “double[] getFibonacci()” that creates an...

    in java eclipse, Create a program “FibonacciFifteen.java” and a method called “double[] getFibonacci()” that creates an array with a length of 15 that contains the first 15 numbers in the Fibonacci sequence and returns it. Set the first element to 0 and the second element to 1, then use a for loop to fill out the rest of the array by adding the prior two elements.

  • Create a new class called DemoSortingPerformacne Create a private method called getRandomNumberArray. It returns an array...

    Create a new class called DemoSortingPerformacne Create a private method called getRandomNumberArray. It returns an array of Integer and has 2 parameters: arraySize of type int and numberOfDigits of type int. This method should create an array of the specified size that is filled with random numbers. Each random numbers should have the same number of digits as specified in the second parameter In your main method (or additional private methods) do the following: Execute selection sort on quick sort...

  • Write a Java program that has a method called arrayAverage that accepts an arrary of numbers...

    Write a Java program that has a method called arrayAverage that accepts an arrary of numbers as an argument and returns the average of the numbers in that array. Create an array to test the code with and call the method from main to print the average to the screen. The array size will be from a user's input (use Scanner). - array size = int - avergage, temperature = double - only method is arrayAverage Output:

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