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...
You are to write a program using Java that will simulate a slot machine with four...
You are to write a program using Java that will simulate a slot machine with four wheels. It will determine the amount of money won for each spin. The four wheels spin and stop showing a value between 0 and 9 inclusive. It costs $2 to play. •You win $500 (but not the $2 you bet) if you get 4 wheels the same. •You win $10 (but not the $2 you bet) if you get exactly 3 of a kind....
Part1: Write a program in Java to simulate CPU scheduling using (i) Shortest Job First (both...
Part1: Write a program in Java to simulate CPU scheduling using (i) Shortest Job First (both preemptive and non-preemptive) and (ii)Priority scheduling Go through the related text and implement each of these CPU algorithms. Consider arrival time, CPU burst time and priority given for each of the processes. Show the results of different steps of your implementation. Part2: Draw the Gantt Chart and compute Average Waiting Time and Average Turn Around Time in each case
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...
URGENT JAVA Develop a Java computer program to simulate access control to a system network. The...
URGENT JAVA Develop a Java computer program to simulate access control to a system network. The username and password authentication mechanism is used as an access control mechanism in this project. The program needs to authenticate the user with their username and hashed password stored in Access Manager User (AMU) file. To create an Access Manager User file, you need to develop a java program that takes username and password as input and then generates a file containing the username,...
Program Requirements: This assignment requires you to create one program in Java to simulate a Point-of-Sale...
Program Requirements: This assignment requires you to create one program in Java to simulate a Point-of-Sale (POS) system. The solution must be in JAVA language. You create the main program called POSmain.java and two classes: ShoppingCart and CashRegister. Your POSmain program should take three file names from command line arguments. The first file contains a list of products and their prices; the second and third files are lists of items in two shopping carts of two customers. The POSmain program...
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...
You need to create a Java class library to support a program to simulate a Point-...
You need to create a Java class library to support a program to simulate a Point- of-Sale (POS) system. General Requirements: You should create your programs with good programming style and form using proper blank spaces, indentation and braces to make your code easy to read and understand; You should create identifiers with sensible names; You should make comments to describe your code segments where they are necessary for readers to understand what your code intends to achieve. Logical structures...
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT