You may earn additional 5 extra points if you simulate the real lottery \"MEGA M
ID: 3732142 • Letter: Y
Question
You may earn additional 5 extra points if you simulate the real lottery "MEGA MILLIONS". If you create a good GUI you would be able to receive additional 3 points The rule of the game is (from Internet): The Lottery game in which the ticket purchaserselects, or has the computer randomly assign five (5)" different "numbers from Field 1 which includes a range of consecutive numbers from one (1) to fifty-six (56) and one (1) number from Field 2 which includes a range of consecutive numbers from one (1) to forty-six (46), shall be called MEGA MILLIONS" For example, the winning numbers from Field 1 may be (03, 05, 10, 20, 43) and from Field 2 105). As you can see the number from Field 2 may be the same as one of the numbers of Field 1. You have different ways to develop your algorithm and code. One of the ways is to use ArrayList. You are free to select some other way. Please give UML diagrams for all classes.Explanation / Answer
Content of the Field1.java file:
import java.util.*;
class Field1
{
int numList[] = new int[56];
int win[] = new int[5];
public Field1()
{
for (int i = 0; i < 56; i++)
{
numList[i] = i + 1;
}
Random rand = new Random();
int count = 0;
while (count < 5)
{
int rnd = rand.nextInt(56);
if (numList[rnd] != 0)
{
numList[rnd] = 0;
win[count] = rnd+1;
count++;
}
}
reset();
}
void reset()
{
for (int i = 0; i < 56; i++)
{
numList[i] = i + 1;
}
}
int[] purchaseTicket()
{
Random rand = new Random();
int arr[] = new int[5];
int count = 0;
while(count < 5)
{
int rnd = rand.nextInt(56);
if (numList[rnd] != 0)
{
numList[rnd] = 0;
arr[count] = rnd+1;
count++;
}
}
return arr;
}
int[] getWinning()
{
return win;
}
}
Content of the Field2.java file:
import java.util.*;
class Field2
{
int numList[] = new int[46];
int win;
public Field2()
{
for (int i = 0; i < 46; i++)
{
numList[i] = i + 1;
}
win = new Random().nextInt(46) + 1;
}
void reset()
{
for (int i = 0; i < 46; i++)
{
numList[i] = i + 1;
}
}
int purchaseTicket()
{
Random rand = new Random();
return rand.nextInt(46)+1;
}
int getWinning()
{
return win;
}
}
Content of MEGAMILLIONS.java:
import java.util.*;
class MEGAMILLIONS
{
public static void main(String args[])throws InputMismatchException
{
Scanner sc = new Scanner(System.in);
System.out.println(" WELCOME TO THE GAME OF MEGAMILLIONS!!");
System.out.println(" ---------------------------------------");
System.out.println(" You will get 5 tickets from Field 1 and 1 from Field 2");
System.out.println("You win if all of them match");
System.out.println();
System.out.println("1) Press <1> to choose your own tickets");
System.out.println("2) Press <2> to get randomly chosen tickets");
System.out.print(" Enter choice: ");
int choice=0;
try{
choice = sc.nextInt();
}
catch(InputMismatchException e)
{
System.out.println(" InputMismatchException!");
}
Field1 f1 = new Field1();
Field2 f2 = new Field2();
int f1tickets[] = new int[5];
int f2ticket = 0;
int f1win[] = f1.getWinning();
int f2win = f2.getWinning();
if (choice == 1)
{
System.out.print("Enter 5 integers within 1 and 56: ");
for(int i = 0; i < 5; i++)
{
f1tickets[i] = sc.nextInt();
}
System.out.print(" Enter another integer less than 47: ");
f2ticket = sc.nextInt();
}
else if (choice == 2)
{
f1tickets = f1.purchaseTicket();
f2ticket = f2.purchaseTicket();
}
boolean isWin = true;
for(int i = 0; i < 5; i++)
{
if (f1tickets[i] != f1win[i])
{
isWin = false;
}
}
if (f2ticket != f2win)
{
isWin = false;
}
if (isWin)
{
System.out.println(" Congratulations, you have won!");
}
else
{
System.out.println(" You have lost.");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.