In this assignment you will create a console (standalone) application. This prog
ID: 3776675 • Letter: I
Question
In this assignment you will create a console (standalone) application. This program will allow you to select MegaMillion numbers. For the first 5 numbers you will be requested to enter a number that is greater than zero and less than 76 since the first 5 numbers on the MegaMillion lottery must be between1-75. However, there’s a catch! Each of these five numbers must be different. The following figure shows a sample screenshot:
When any of the first five numbers entered is less than 0 or greater 75 the user will receive a message to this effect and will be asked to reenter the number. You will create the code that will display one message when a number that is less than 1 is entered and a different message will display when the number entered is greater than 75. For example, if the user enters zero you might display the message: “The number must be greater than zero. Please reenter the number.”
If the number is the same as any number entered before it (with the exception of the MegaBall number, which is the last number entered) the user will receive a message to this effect and will be requested to reenter the number. This is the same for the second through fifth numbers.
When entering the MegaBall number, if the number entered IS NOT between 0 and 15, the user will receive a message to this effect and asked to reenter the number. One message will display if the number entered is less than 1, and a different message if the number entered is greater than 15.
The following MUST be included in the program:
You must use Eclipse to create this assignment. You must have multiple classes. One class must include the accessor/mutator methods, a readInput() method and a writeOutput() method. Name this first program “MegaMillion.java”. The values of the first five numbers must be saved within an array. The first element of the array will equal the first number entered, the second element of the array will equal the second number entered, etc. However, the number entered is not to be added to the array unless it is both unique from the other numbers entered, and it also falls within the correct range of numbers.
The second program is to be named “MegaMillionTest.java” and will be responsible for creating a MegaMillion object and invoking the readInput() and writeOutput() methods located in the MegaMillion class. You are NOT to include anything else in this testing class. Only have it create a MegaMillion object and call the readInput() and writeOutput() method for this object.
Please include comments throughout the program, as I will be using this as a reference to write my own program. The whole idea is for me to understand what im doing ;-) No @param please. This is a Java I class.
Please enter number 1 which should be 0 and less than 76 Numberl must be greater than zero Please enter number which should be 0 and less than 76 76 Number must be less than 76 Please enter number which should be o and less than 76 Please enter number 2 which should be 0 and less than 76 Number must be greater than zero Please enter number 2 which should be 0 and less than 76 76 Number 2 must be less than 76 Please enter number 2 which should be 0 and less than 76 Number 2 must be different from number 1 Please enter number which should be 0 and less than 76Explanation / Answer
package megamillions;
/*The util package contains the Scanner and Random clasees i need so i just
* imported the whole thing
*/
import java.util.*;
/**
* The megaMillions class handles all operations to be performed on the lottery
* tickets such a generating, sorting, and printing
* @author SirCharles
*/
public class MegaMillions{
/**
* The ticketGen() function takes no arguments and generates random numbers
* into the array for the lottery tickets using a for loop. It also returns
* the lottery ticket for use outside of the function. The completed ticket
* is printed from here.
* @return ticket
*/
public static int[] ticketGen(){
//My generator to make the random numbers to go in the array
Random generator = new Random();
/*Since the numbers i need are 1-56 i put +1 since the .nextInt method
*returns numbers up to n-1, n being the parameter of the method. If I had
* put 57 in the box it wouldnt work because it could still choose 0.
*/
int ticket[] = new int[6]; //The array for groups 1 and 2
for(int i=0; i<5; i++){ //A loop to put a random number in each array index
ticket[i] = generator.nextInt(56) + 1;
//if(ticket[i] >= 1 && ticket[i].equals(ticket[i-1]){ //was trying to get it not to repeat random
// ticket[i] = generator.nextInt(56)+1; //numbers inside of a group...
//}
}
ticket[5] = generator.nextInt(46) + 1; //And putting a number in group 2
for (int i=0; i<ticket.length; i++){ //Finally a loop to print the completed ticket
System.out.print(ticket[i]+" ");
}
return ticket;
}
/**
* A method to sort the tickets into ascending order
* @return
*/
//public static int[] ticketSort(){
// int[] ticket;
// return ticket;
//}
/**
* The main method handles user input and calls the functions for sorting and
* generating the lottery tickets.
* @param args none
*/
public static void main(String[] args) {
//A for loop to make sure the user enters a number 1-5
for (int i=0; i!=1; i++){
//A scanner to take in input
System.out.println("How many tickets would you like to purchase? [1-5]: ");
Scanner ticketAmount = new Scanner(System.in);
String userInput = ticketAmount.next();
//An if-else statement to check if the user entered acceptable data
if(userInput.equals("1")){ //one ticket is printed
ticketGen();
}
else if(userInput.equals("2")){ //two tickets are printed
ticketGen();
System.out.println("");
ticketGen();
}
else if(userInput.equals("3")){ //three tickets are printed
ticketGen();
System.out.println("");
ticketGen();
System.out.println("");
ticketGen();
}
else if(userInput.equals("4")){ //four tickets are printed
ticketGen();
System.out.println("");
ticketGen();
System.out.println("");
ticketGen();
System.out.println("");
ticketGen();
}
else if(userInput.equals("5")){ //five tickets are printed
ticketGen();
System.out.println("");
ticketGen();
System.out.println("");
ticketGen();
System.out.println("");
ticketGen();
System.out.println("");
ticketGen();
}
else{ //Error message if wrong input
System.out.println(" Please enter a number 1-5.");
i--;
}
}
}
}