Must Create a Java Program: entered and a different message will display when th
ID: 3774158 • Letter: M
Question
Must Create a Java Program:
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 do not include anything else in this class.
Please enter number1 which should be 0 and less than 76 Number1 must be greater than zero Please enter number1 which should be 0 and less than 76 76 Number 1 must be less than 76 Please enter number1 which should be 0 and less than 76 Please enter number2 which should be 0 and less than 76 Number2 must be greater than zero Please enter number2 which should be 0 and less than 76 76 Number must be less than 76 Please enter number2 which should be 0 and less than 76 Number must be different from number1 Please enter number2 which should be 0 and less than 76Explanation / Answer
import java.util.Scanner;
public class MegaMillion {
private int[] megaMillion;
private int megaBallNum;
public MegaMillion() {
// TODO Auto-generated constructor stub
megaMillion = new int[5];
}
/**
* @return the megaMillion
*/
public int[] getMegaMillion() {
return megaMillion;
}
/**
* @param megaMillion
* the megaMillion to set
*/
public void setMegaMillion(int[] megaMillion) {
this.megaMillion = megaMillion;
}
/**
* @return the megaBallNum
*/
public int getMegaBallNum() {
return megaBallNum;
}
/**
* @param megaBallNum
* the megaBallNum to set
*/
public void setMegaBallNum(int megaBallNum) {
this.megaBallNum = 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);
}
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);
}
/**
* @param number
* @param count
* @return
*/
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 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MegaMillion megaMillion = new MegaMillion();
megaMillion.readInput();
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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.