Question

In: Computer Science

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

  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.

Print out the remaining pool after the numbers are picked

Solutions

Expert Solution

Implementation in JAVA:


// import some neccessary classes
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class Lottery {
  
// driver method
   public static void main(String[] args) {
      
//       declare arraylist initial
ArrayList<Integer> initial= new ArrayList<Integer>();

// adding elements
for(int i=0;i<=29;i++) {
   initial.add(i);
}
      
// printing initial
System.out.println("Initial pool : "+initial);

// declare picked arraylist
ArrayList<Integer> picked= new ArrayList<Integer>();

// for picking 7 random elements
for(int i=0;i<7;i++) {
     
//   call method which is defined below for generating random numbers
   int random=getRandomNumberUsingNextInt(0,29);
     
//   check wheather it is picked earlier if yes then generate random no. again
   while(!check(initial,random)) {
       random=getRandomNumberUsingNextInt(0,29);
   }
     
//   removing picked no. form initial list
   for(int j=0;j<initial.size();j++) {
         
       if(initial.get(j)==random) {
           initial.remove(j);
       }
         
   }
     
//   add random to picked list
   picked.add(random);
     
}

// sort picked list by Collections
Collections.sort(picked);


// print picked in 2 digit format
System.out.print("Picked : {");
         
for(int i=0;i<picked.size();i++) {
     
   if(i==picked.size()-1) {
       System.out.print(String.format("%02d" , picked.get(i)));
   }
   else
   System.out.print(String.format("%02d" , picked.get(i))+", ");
}
System.out.println("}");


// print remaining pool
System.out.println("Remaiming pool : "+initial);

      
      
      
   }
  
//   method for generating random no. with in range
   public static int getRandomNumberUsingNextInt(int min, int max) {
   Random random = new Random();
   return random.nextInt(max - min) + min;
   }
  
//   method for checking wheather num is in list or not
   public static boolean check(ArrayList<Integer> list, int num) {
      
       for(int i=0;i<list.size();i++) {
           if(list.get(i)==num) {
               return true;
           }
       }
      
       return false;
      
   }

}

SAMPLE OUTPUTS:

1:

2:

Time complexity is high so you have to wait for a second for output

If you have any doubt regarding this question please ask me in comments

// THANK YOU:-)


Related Solutions

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 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 numbers drawn cannot repeat Sort...
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...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the two players will be the computer. The program will start by asking how many winning rounds are needed to win the game. Each round will consist of you asking the user to pick between rock, paper, and scissors. Internally you will get the computers choice by using a random number generator. Rock beats Scissors, Paper beats Rock, and Scissors beats Paper. You will report...
Write a Java program to simulate the rolling of two dice. The application should use an...
Write a Java program to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12. Your application should roll the dice 36,000,000 times. Store the results of each roll...
Without using the graphics module, write a program to simulate an archery score tracker. Your program...
Without using the graphics module, write a program to simulate an archery score tracker. Your program should use a Tkinter canvas to draw a target. Your program should allow the user to click the target to mark where their arrow landed on the target (or off). When the user clicks the target, the program should draw a dot at that location and compute the score. Scores for each ring are as follows: yellow 5, red 4, blue 3, black 2,...
Write a Java program that directs the user to enter a single word (of at least...
Write a Java program that directs the user to enter a single word (of at least four characters in length) as input and then prints the word out in reverse in the pattern shown below (please note that the spaces added between each letter are important and should be part of the program you write): <Sample Output Enter a word (at least four characters in length): cat Word must be at least four characters in length, please try again. Enter...
(JAVA) We will write a program to check the spelling of the word entered by user,...
(JAVA) We will write a program to check the spelling of the word entered by user, using data from a dictionary, which is text file. The program will ask user to enter a word for spell check, and then check in the text file (dictionary.txt) if the word exists. (The dictionary.txt file is provided) If the word exists in text file, the output will be “Word is correctly spelled”. If the word doesn’t exist in the text file, program will...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester.    The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
please write in c using linux or unix Write a program that will simulate non -...
please write in c using linux or unix Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally 10 jobs) and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT