Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HELP! NEEDED IN BASIC BEGINNER JAVA WITH NO CASES! Write a program that simulate

ID: 3828234 • Letter: H

Question

HELP! NEEDED IN BASIC BEGINNER JAVA WITH NO CASES!

Write a program that simulates a basic lottery.

Your user will “purchase” one lottery ticket. Each ticket will contain 5 numbers. Each number will be between 1 and 20, inclusive. You will allow the user to select their own numbers for their ticket or to have the computer randomly select the numbers. You should represent the “ticket” using an array.

Once the ticket has been purchased, your program will randomly generate the “winning lottery numbers” and determine if the user has won. The winning lottery numbers should be stored in a separate array.

Here’s the fun part – your program should continue to generate a full set of “winning lottery numbers” drawings until your user wins the lottery. Your program will count the number of lottery drawings it takes to win.

A few things to consider:

The program should contain the following methods

o A main method
o A method that determines the user’s ticket numbers
o A method that determines the winning lottery numbers
o A method that determines if the user has a winning ticket
o A method that displays the following to the screen:

A message indicating the win

The numbers in the user’s ticket when the win occurred

The winning lottery ticket numbers

The number of times it took to get a match with a winning ticket

The number of times each value 1-20 was generated for a winning lottery ticket

The number(s) generated most often for the winning lottery tickets during this run of the

program

For the sake of simplicity, you should implement your code to prevent duplicate values in the lottery

tickets (ie – a specific number should only appear one time in any ticket). You will need to write code to ensure the user doesn’t select the same number twice and that the computer doesn’t generate the same number twice.

The numbers do not have to match in order. For example, a user ticket with numbers 1, 2, 3, 4, 5 should be considered a match if the winning ticket numbers are 5, 2, 1, 3, 4.

You must validate all user input.

Numeric output should be displayed with commas

The code should be written using efficient processing.

Remember to use good programming practices (comments, naming conventions, program structure,

etc)

Explanation / Answer

Lotto.java

----------------------------------------------------------------------------------------------------------------------------

package com.lotto;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

public class Lotto {
   //for frequency
   private static HashMap<Integer, Integer> freqMap = new HashMap<>();
   public static void main(String[] args) {
       int lottery[] = new int[5];
       int winner[] = new int[5];
       lottery = getUserLottery();
       boolean win = false;
       int turnsTook = 0;
       //continue till a win
       while(!win){
           winner = winningLotteryGen();
           win = matchWithWinner(lottery,winner);
           turnsTook++;
       }
       System.out.print("You Won!!!!! The winning lottery is : ");
       for(int i =0; i <5 ;i++)
           System.out.print(winner[i]+" , ");
       System.out.println("Turns took to win : "+ turnsTook);
       System.out.println("-------------------------------");
       System.out.println("Frequency of numbers generated");
       //iterator
       Iterator it = freqMap.entrySet().iterator();
       while(it.hasNext()){
           //print frequency
           Map.Entry<Integer, Integer> keyVal = (Entry<Integer, Integer>) it.next();
           System.out.println(keyVal.getKey()+"------>"+keyVal.getValue());
       }
       //most frequent number
       System.out.println("Most freq number generated : "+Collections.max(freqMap.entrySet(), Map.Entry.comparingByValue()).getKey());
      

   }
  
   /**
   * get lottery numbers (non repeating and below 20)
   * @return
   */
   private static int[] getUserLottery(){
       System.out.println("Hi, Please enter your 5 numbers:");
       int lottoNum[] = new int[5];
       Scanner scanner = new Scanner(System.in);
       for(int i = 0;i<5;i++){
           int lottoTempNum = Integer.parseInt(scanner.nextLine());
           if(Arrays.asList(lottoNum).contains(lottoTempNum) || lottoTempNum > 20){
               System.out.println("Cant allow duplicates: Enter another number");
               i--;
           }else{
               lottoNum[i] = lottoTempNum;
           }
       }
       return lottoNum;
   }
  
   /**
   * generate winning lottery of random numbers between 1-20
   * @return
   */
   private static int[] winningLotteryGen(){
       //List<Integer> randomList = new ArrayList<Integer>();
       int winnerList[] = new int[5];
       //generate a arraylist of 5 random numbers
       for(int i = 0; i < 5; i ++){
           int randomNum = ThreadLocalRandom.current().nextInt(1, 21);
           checkFrequency(randomNum);
           winnerList[i] = randomNum;
       }
       return winnerList;
   }
  
   /**
   * match user with winning lottery and check if user won
   * @param user
   * @param winner
   * @return
   */
   private static boolean matchWithWinner(int user[], int winner[]){
       //sort the user lottery
       Arrays.sort(user);
       //sort the winning lottery
       Arrays.sort(winner);
       //check if both are same
       return Arrays.equals(user,winner);
   }
  
   /**
   * calculate frequency of each number
   * @param num
   */
   private static void checkFrequency(int num){
       int value = 0;
       if(freqMap.containsKey(num)){
           value = freqMap.get(num);
           value++;
       }else{
           freqMap.put(num, 1);
       }
       freqMap.put(num, value);
      
   }
}