Question

In: Computer Science

Java: Using ArrayLists to Simulate a Lottery Program Simulate a Lottery Drawing by choosing 7 numbers...

Java:

Using ArrayLists to Simulate a Lottery Program

Simulate a Lottery Drawing by choosing 7 numbers at random from a pool containing 30 numbers

Each time a number is selected, it is recorded and removed from the pool

The pool values are 00 to 29 inclusive

  1. Your program must output each selected number from the drawing using a two-digit format. For Example, the number 2 would be represented by 02 on the “Picked” line.   
  2. The numbers drawn cannot repeat
  3. Sort the picked numbers before printing them to the console
  4. Show the initial and remaining pool
  5. Your output must match as closely as possible to what is shown below

Example Output:

Initial Pool: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29]

Picked: {01,06,09,19,25,26,27}

Remaining:[0,2,3,4,5,7,9,10,11,12,13,14,15,16,17,18,20,21,22,23,24,28,29]

Algorithm

  1. Create two ArrayLists of type Integer called pool and pick
  2. Create a Random Number to represent the number chosen, it will represent the index of the number chosen
  3. Create the pool ArrayList by using a loop going from 0 to <30. Print the pool ArrayList Out
  4. Choose 7 numbers at random, remove them from the pool ArrayList and place them in the picked ArrayList.
  5. Use Collections.sort(pick) to sort the pick ArrayList<Integer>
  6. Print the pick ArrayList out but add an additional 0 in the front if it is one digit. Example, change 2 to 02.
  7. Print out the remaining pool after the numbers are picked.

Solutions

Expert Solution

CODE -

import java.util.ArrayList;

import java.util.Collections;

import java.util.Random;

public class lottry

{

    public static void main(String[] args)

    {

        Random rand = new Random();

        // Creating two ArrayLists of type Integer called pool and pick

        ArrayList<Integer> pool = new ArrayList<Integer>();

        ArrayList<Integer> pick = new ArrayList<Integer>();

        // Adding elements to the pool ArrayList

        for(int i=0; i<30; i++)

            pool.add(i);

        // Displaying the initial pool ArrayList

        System.out.print("Initial Pool: [" + pool.get(0));

        for(int i=1; i<30; i++)

            System.out.print(", " + pool.get(i));

        System.out.println("]");

        int i=0;

        // Choosing 7 random numbers

        while(i<7)

        {

            boolean found = false;

            int num = rand.nextInt(30);

            // Loop to check if random number generated is already in the ArrayList pick so that the numbers don't get repeated.

            for (int j=0; j<i; j++)

            {

                if(pick.get(j) == num)

                    found = true;

            }

            // Adding the choosen number to pool ArrayList and removing the number from the pool ArrayList

            if(!found)

            {

                pick.add(num);

                for(int j=0; j<30-i-1; j++)

                    if(pool.get(j) == num)

                        pool.remove(j);

                i++;

            }

        }

        // Sorting the pick ArrayList

        Collections.sort(pick);

        // Displaying the picked numbers

        System.out.printf("Picked: {%02d", pick.get(0));

        for (i=1; i<7; i++)

            System.out.printf(", %02d", pick.get(i));

        System.out.println("}");

        // Displaying the remaining pool ArrayList

        System.out.print("Remaining: [" + pool.get(0));

        for(i=1; i<23; i++)

            System.out.print(", " + pool.get(i));

        System.out.println("]");

    }

}

SCREENSHOTS -

CODE -

OUTPUT -

If you have any doubt regarding the solution, then do comment.
Do upvote.


Related Solutions

Java 176 Lottery Program in Word. Using ArrayLists to Simulate a Lottery Program Simulate a Lottery...
Java 176 Lottery Program in Word. Using ArrayLists to Simulate a Lottery Program Simulate a Lottery Drawing by choosing 7 numbers at random from a pool containing 30 numbers Each time a number is selected, it is recorded and removed from the pool The pool values are 00 to 29 inclusive Your program must output each selected number from the drawing using a two-digit format. For Example, the number 2 would be represented by 02 on the “Picked” line. The...
Choosing Lottery Numbers: In the Super-Mega lottery there are 50 numbers (1 to 50), a player...
Choosing Lottery Numbers: In the Super-Mega lottery there are 50 numbers (1 to 50), a player chooses ten different numbers and hopes that these get drawn. If the player's numbers get drawn, he/she wins an obscene amount of money. The table below displays the frequency with which classes of numbers are chosen (not drawn). These numbers came from a sample of 180 chosen numbers. Chosen Numbers (n = 180) 1 to 10 11 to 20 21 to 30 31 to...
Choosing Lottery Numbers: In the Super-Mega lottery there are 50 numbers (1 to 50), a player...
Choosing Lottery Numbers: In the Super-Mega lottery there are 50 numbers (1 to 50), a player chooses ten different numbers and hopes that these get drawn. If the player's numbers get drawn, he/she wins an obscene amount of money. The table below displays the frequency with which classes of numbers are chosen (not drawn). These numbers came from a sample of 180 chosen numbers. Chosen Numbers (n = 180) 1 to 10 11 to 20 21 to 30 31 to...
In a java program: a) Given the following list of numbers: 90 8 7 56 123...
In a java program: a) Given the following list of numbers: 90 8 7 56 123 235 9 1 653 trace the execution for: Selection Sort (only the first 5 steps) MergeSort. (b) Given the following list of numbers: 3 1 4 1 5 9 2 6 5 3 5 trace the execution for quicksort with median-of-three partitioning and a cutoff of 3.
In Java, Write a Java program to simulate an ecosystem containing two types of creatures, bears...
In Java, Write a Java program to simulate an ecosystem containing two types of creatures, bears and fish. The ecosystem consists of a river, which is modeled as a relatively large array. Each cell of the array should contain an Animal object, which can be a Bear object, a Fish object, or null. In each time step, based on a random process, each animal either attempts to move into an adjacent array cell or stay where it is. If two...
Write a java program of a multiplication table of binary numbers using a 2D array of...
Write a java program of a multiplication table of binary numbers using a 2D array of integers.
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
Part 1 Lottery Statistics Create a program that will compute some statistics on winning lottery numbers....
Part 1 Lottery Statistics Create a program that will compute some statistics on winning lottery numbers. Download a .csv file from Winning Powerball Numbers containing a record of past winning numbers. Examine the formatting of this file. Create a dictionary that contains each winning number and the number of times that number was drawn. Print the 10 most frequently drawn numbers, and the 10 least frequently drawn numbers. HINT: You can’t sort a dictionary. Build a list that is ordered...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT