**Java Language only** Directions: The code below tells the user to enter 5 numb
ID: 3746851 • Letter: #
Question
**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 put descriptive comments on the code including an introduction of what the code does at the begining of the code. Add descritive java doc to the code and a uml diagram as well.
Also let the code be copy pastable to textpad.
Thank you I will thumbs up good work
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Lottery {
int[] lotteryNumbers = new int[5];
public int[] getLotteryNumbers() {
return lotteryNumbers;
}
Lottery() {
Random randomVal = new Random();
for (int i = 0; i < lotteryNumbers.length; i++) {
lotteryNumbers[i] = randomVal.nextInt((10 - 1) + 1);
}
}
int compare(int[] personLottery) {
int count = 0;
Arrays.sort(lotteryNumbers);
Arrays.sort(personLottery);
for (int i = 0; i < lotteryNumbers.length; i++) {
if (lotteryNumbers[i] == personLottery[i]) {
count++;
}
}
return count;
}
public static void main(String[] args) {
int[] personLotteryNum = new int[5];
int matchNum;
Lottery lnum = new Lottery();
Scanner input = new Scanner(System.in);
System.out.println("Enten the five digit lottery number");
for (int i = 0; i < personLotteryNum.length; i++) {
System.out.println("Enter the digit " + (i + 1) + " :");
personLotteryNum[i] = input.nextInt();
}
matchNum = lnum.compare(personLotteryNum);
if (matchNum == 5)
System.out.println("YOU WIN!!");
else
System.out.println(" YOU LOSS!!");
System.out.println("Computer Generated Lottery Number :");
System.out.print("|");
for (int i = 0; i < lnum.getLotteryNumbers().length; i++) {
System.out.print(lnum.getLotteryNumbers()[i] + "|");
}
System.out.println(" Lottery Number Of user:");
System.out.print("|");
for (int i = 0; i < personLotteryNum.length; i++) {
System.out.print(personLotteryNum[i] + "|");
}
System.out.println();
System.out.println("Number Of digit matched: " + matchNum);
}
}
Explanation / Answer
The following steps are
1. user number={a,b,c,d,e} i.e. this is five random digits from 0 to 9 according to the question. That is taken in the array.
2. Let computer generated code (lotterynumber) five random digit numbers from 0 to lottery number={f,g,h,i,j} taken in array of size five
3. According to the situation all five digits should be matched irrespective of the place or index.
4. For that, We can approach by two arrays i.e. usernumber, lotterynumber ascending order or descending order. Because if compare repeated digits are possible in the any array. This can be solve by the sorting in ascending order or descending order.
5. After sorting the two arrays, we can compare the two arrays from first element to last element.
6. Here we can take count=0, it will incremented if the two array elements are matched by index.
7. If the count=5, then person won the lottery. Otherwise person loss the lottery.
Example:
usernumber Generated code User sort Code sorting Count of matched indexes Status
2,4,4,5,2 2,4,7,8,5 { 2,2,4,4,5} {2,4,5,7,8} 1 Not Matched
{3,4,5,6,9} {9,4,5,6,3} {3,4,5,6,9} {3,4,5,6,9} 5 matched
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.