Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment you will create a console (standalone) Java application. This

ID: 3779377 • Letter: I

Question

In this assignment you will create a console (standalone) Java application. This Java 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 between 1-75. However, there’s a catch! Each of these five numbers must be different.

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 project:

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 @ references please. This is a Java I class. The following figure shows a sample screenshot:

Please enter number 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 Number1 must be less than 76 Please enter number which should be 0 and less than 76 Please enter number which should be 0 and less than 76 Number must be greater than zero Please enter number which should be 0 and less than 76 Number2 must be less than 76 Please enter number which should be 0 and less than 76 Number2 must be different from number1 Please enter number 2 which should be 0 and less than 76

Explanation / Answer

import java.util.Scanner;

public class MegaMillion {
   private int[] megaMillion;
   private int megaBallNum;

   /**
   *
   * Default constructor to set megaMillion size as 5
   */
   public MegaMillion() {
       // TODO Auto-generated constructor stub
       megaMillion = new int[5];

   }

   /**
   * to return the megaMillion
   */
   public int[] getMegaMillion() {
       return megaMillion;
   }

   /**
   *
   * the megaMillion to set
   */
   public void setMegaMillion(int[] megaMillion) {
       this.megaMillion = megaMillion;
   }

   /**
   * to return the megaBallNum
   */
   public int getMegaBallNum() {
       return megaBallNum;
   }

   /**
   *
   * the megaBallNum to set
   */
   public void setMegaBallNum(int megaBallNum) {
       this.megaBallNum = megaBallNum;
   }

   /**
   * To read the input from keyboard for valid megaBallNum
   */
   public void readInput() {

       int count = 0;
       Scanner scanner = new Scanner(System.in);

       for (int i = 0; i < megaMillion.length;) {
           System.out.println("please enter number" + (i + 1)
                   + " which would be > 0 and less than 76");
           int n = scanner.nextInt();

           if (n <= 0)
               System.out.println("Number" + (i + 1)
                       + " must be greater than zero");
           else if (n >= 76)
               System.out
                       .println("Number" + (i + 1) + " must be less than 76");
           else if (i != 0) {
               if (isValidNum(n, i))
                   megaMillion[i++] = n;

           } else {
               megaMillion[i++] = n;
           }
       }
       boolean flag = true;
       do {

           System.out
                   .println("please enter a number which would be > 0 and less than 16 for lucky metga number");
           int n = scanner.nextInt();
           if (n <= 0)
               System.out.println("The mega number must be greater than zero");
           else if (n >= 16)
               System.out.println("The mega number must be less than 16");
           else {

               for (int i = 0; i < megaMillion.length; i++) {
                   if (n == megaMillion[i]) {
                       megaBallNum = n;
                       flag = false;
                       break;
                   }
               }

           }
       } while (flag);

   }

   /**
   *
   * method to write the output to the console
   */
   public void writeOutput() {
       System.out.println(" Your mega million numbers are ");
       for (int i = 0; i < megaMillion.length; i++) {
           System.out.print(" " + megaMillion[i]);
       }
       System.out.println(" and the mega ball number is " + megaBallNum);

   }

   /**
   * method to check the valid megaBallNum
   */
   private boolean isValidNum(int number, int count) {

       for (int i = 0; i < count; i++) {
           if (number == megaMillion[i])
               return false;
       }
       return true;

   }

}

public class MegaMillionTest {

   /**
   * method to test the MegaMillion class
   *
   */
   public static void main(String[] args) {

       // creating object
       MegaMillion megaMillion = new MegaMillion();
       // calling method for reading input
       megaMillion.readInput();
       // printing to console
       megaMillion.writeOutput();
   }

}

OUTPUT:

please enter number1 which would be > 0 and less than 76
0
Number1 must be greater than zero
please enter number1 which would be > 0 and less than 76
1
please enter number2 which would be > 0 and less than 76
76
Number2 must be less than 76
please enter number2 which would be > 0 and less than 76
2
please enter number3 which would be > 0 and less than 76
3
please enter number4 which would be > 0 and less than 76
4
please enter number5 which would be > 0 and less than 76
5
please enter a number which would be > 0 and less than 16 for lucky metga number
0
The mega number must be greater than zero
please enter a number which would be > 0 and less than 16 for lucky metga number
16
The mega number must be less than 16
please enter a number which would be > 0 and less than 16 for lucky metga number
2

Your mega million numbers are
1 2 3 4 5 and the mega ball number is 2