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...
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...
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The...
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The program asks the user questions and then creates a paragraph using the user’s answers. 2.The program must perform the following: a.Uses a Scanner object to ask the user: (The program asks for no other information) i.Full Name (First and Last name only) - stores this Full Name in one String object variable. ii.Age – must be read in as an int. iii.Profession or expected...
rite a Java Program to play your version of the Texas Pick 3 Lottery. You will...
rite a Java Program to play your version of the Texas Pick 3 Lottery. You will need to include the provided code given below in your program. Task:The program will prompt for a number between 0 and 999. Your program will display the number of attempts it took for the entered number to match a randomly generated number. In the main() method, declare an int named myPick3choice. Create a prompt for myPick3choice as "Enter your Pick 3 choice:0-999". Create a...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT