Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

**Java Language only** Directions: The code below tells the user to enter 5 numbers 0-9, To see if they will match the randomly selected numbers.

**Java Language only**

Directions: The code below tells the user to enter 5 numbers 0-9, To see if they will match the randomly selected numbers. the code is Imitating the lottery. For the code below Add descriptive java doc to the code and a uml diagram as well. It already has Notes on it

Also let the code be copy pastable to textpad.

Thank you I will thumbs up good work!

package classes4;

import java.util.Arrays;

import java.util.Random;

import java.util.Scanner;

/**

*

* Lottery program asks the user to enter 5 numbers 0-9,

* To see if they will match the randomly selected numbers.

* this program is Imitating the lottery

*

* **/

public class Lottery {

//stores randomly generated 5 digit number

int[] lotteryNumbers = new int[5];

//return lotteryNumbers

public int[] getLotteryNumbers() {

return lotteryNumbers;

}

//constructor of Lottery class

Lottery() {

//Initialize random class object

Random randomVal = new Random();

//loop for the size of lottery

for (int i = 0; i < lotteryNumbers.length; i++) {

//generate random number between 1 to 9

lotteryNumbers[i] = randomVal.nextInt((10 - 1) + 1);

}

}

/**this function will compare person lottery array with

* randomly generated lottery

**/

int compare(int[] personLottery) {

//stores number of digit match

int count = 0;

//sort the randomly generated lottery array

Arrays.sort(lotteryNumbers);

//sort the person lottery array

Arrays.sort(personLottery);

//loop for the length of the lottery array

for (int i = 0; i < lotteryNumbers.length; i++) {

//if digit matches increase the count by one

if (lotteryNumbers[i] == personLottery[i]) {

count++;

}

}

//return the count

return count;

}

//main class

public static void main(String[] args) {

//initialize person lottery array with size of 5

int[] personLotteryNum = new int[5];

//stores match number

int matchNum;

//create object of lottery

Lottery lnum = new Lottery();

//initialize system scanner

Scanner input = new Scanner(System.in);

//ask user to enter lottery nuumber

System.out.println("Enter the five digit lottery number");

//loop for lottery length

for (int i = 0; i < personLotteryNum.length; i++) {

//print message

System.out.println("Enter the digit " + (i + 1) + " :");

//get user input

personLotteryNum[i] = input.nextInt();

}

//get match count

matchNum = lnum.compare(personLotteryNum);

//if count is equal to 5 ..means if all digit matches

if (matchNum == 5)

//print you win

System.out.println("YOU WIN!!");

else

//else print you loss

System.out.println(" YOU LOSS!!");

//show message

System.out.println("Computer Generated Lottery Number :");

System.out.print("|");

//print computer generated lottery

for (int i = 0; i < lnum.getLotteryNumbers().length; i++) {

System.out.print(lnum.getLotteryNumbers()[i] + "|");

}

System.out.println(" Lottery Number Of user:");

//print person lottery

System.out.print("|");

for (int i = 0; i < personLotteryNum.length; i++) {

System.out.print(personLotteryNum[i] + "|");

}

System.out.println();

//print number of match count

System.out.println("Number Of digit matched: " + matchNum);

}

}

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

Practical Database Programming With Visual Basic.NET

Authors: Ying Bai

1st Edition

0521712351, 978-0521712354

More Books

Students also viewed these Databases questions

Question

What are the potential downsides to firm growth?

Answered: 1 week ago