Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help with this, and leave comments explainging the code you wrote, thank you for your help Write a Lottery class that simulates a 6-number

Please help with this, and leave comments explainging the code you wrote, thank you for your help

Write a Lottery class that simulates a 6-number lottery (e.g. "Lotto"). The class should have an array of six integers named lotteryNumbers, and another array of six integers named userLotteryPicks. The class' constructor should use the Random class to generate a unique random number in the range of 1 to 60 for each of the 6 elements in the lotteryNumbers array. Thus, there should be a loop that keeps generating random numbers until all 6 numbers are unique. The Lottery class should have a method, getUsersPicks(), which prompts the user to enter 6 unique numbers between 1 - 60, which will be stored in the array of integers named usersLotteryPicks. Thus, there should be validation to ensure that each number the user selects is different from all the other numbers that he/she entered previously, and to keep looping until the user enters 6 unique numbers. Finally, the Lottery class should have a method, checkLotteryMatch(), which will compare the 2 arrays - lotteryNumbers & usersLotteryPicks - and return the number of digits that match. The digits do not have to match in the exact order, so a nested loop should be created that goes through each digit in the user's lottery picks array and compares it to each digit in the lotteryNumbers array, counting each of the matches. The checkLotteryMatch() method will return an int containing the number of matches.

Write a Driver class called the LotteryGame, which instantiates the Lottery class. Once a Lottery object has been created, the driver will call the Lottery object's getUsersPicks() method, followed by the checkLotteryMatch() method. Use the results of the checkLotteryMatch() method to determine if the user is a winner by using the following criteria:

For a 3-digit match, display a message to the user that he/she will receive a free Lottery ticket as the prize

For a 4-digit match, display a message to the user that he/she will receive a $2000 prize

For a 5-digit match, display a message to the user that he/she will receive a prize of $50,000.

For a 6-digit match, display a message to the user that he/she will receive a grand prize of $1,000,000.

If there are no matches, display the following message to the user: "Sorry, no matches today. Try again!"

This is what I have so far

Domain

package lotterygame; import java.util.Scanner; import java.util.Random; // ///** // * // * @author morty // */ public class Lottery {

static int[] lotteryNumbers = new int[6]; static int[] userLotteryPicks = new int[6]; static int userNums; int numMax, numMin; public int noDuplicates (int numMax, int numMin) { return ((int)(Math.random()*(numMax - numMin))) + numMin; }

public Lottery() { for (int i = 0; i < lotteryNumbers.length; i++) { int rand = noDuplicates(1, 60); lotteryNumbers [i] = rand; } } public void getUsersPicks() { Scanner key= new Scanner(System.in); do { System.out.println("Please enter 6 numbers between 1 and 60. GOOD LUCK! "); userNums = key.nextInt(); } while (userNums > 60 || userNums < 1); for (int j = 0; j < 6; j++) { userLotteryPicks[j] = userNums; } } public int checkLotteryMatch() { int sameNums = 0; for (int k = 0; k < lotteryNumbers.length; k++) { if (lotteryNumbers.equals(userLotteryPicks)) { sameNums++; System.out.println(sameNums); } } return sameNums; } }

Driver

package lotterygame;

/** * * @author morty */ public class LotteryGame {

/** * @param args the command line arguments */ public static void main(String[] args) { Lottery lotto = new Lottery(); lotto.getUsersPicks(); lotto.checkLotteryMatch(); int counter = lotto.checkLotteryMatch(); if (counter == 3) { System.out.println("You get a free Lottery ticket as the prize"); } else if (counter == 4) { System.out.println("Bravo, you won $2,000"); } else if (counter == 5) { System.out.println("Fantastic, you won $50,000"); } else if (counter == 5) { System.out.println("Congratulations, you won the grand prize of $1,000,000"); } else { System.out.println("Sorry, no matches today. Try again!"); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions

Question

Outline what actions constitute sexual harassment.

Answered: 1 week ago

Question

Explain the strength of acid and alkali solutions with examples

Answered: 1 week ago

Question

Introduce and define metals and nonmetals and explain with examples

Answered: 1 week ago