Problem 3-Write a program Submit a program called: EvenOdd.java Lärs wants to cr
ID: 3594889 • Letter: P
Question
Problem 3-Write a program Submit a program called: EvenOdd.java Lärs wants to create a program that asks a user to first enter an even number (continuing to prompt the user for an even number if they do not actually enter an even number) Afterwards, he wants the user to enter an odd number (once again, continuously prompting the user if they do not at first enter an odd number). Once the user has done this, he wants the program to exit saying:***Thanks! Bye!** He wants to use the following methods: public static int getUserlnput(String message) public static boolean checklfEven(int n) to create any additional helper methods). Sample Run: --Enter an even number: That is not even. Enter an even number. That is not even. Enter an even number. 4 --Ok thanks! Now enter an odd number: 4 That is not odd. Enter an odd number That is not odd. Enter an odd number. **Thanks! Bye!**Explanation / Answer
EvenOdd.java
import java.util.Scanner;
public class EvenOdd {
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
int n = getUserInput("Enter an even number: ");
while(!checkIfEven(n)) {
System.out.print("This is not even. ");
n = getUserInput("Enter an even number: ");
}
System.out.println();
System.out.print("--Ok thanks! Now ");
getUserInput("enter an odd number: ");
while(checkIfEven(n)) {
System.out.print("This is not odd. ");
n = getUserInput("Enter an odd number: ");
}
System.out.println("**Thanks! Bye!**");
}
public static int getUserInput(String message) {
System.out.println(message);
return scan.nextInt();
}
public static boolean checkIfEven(int n) {
return n % 2 ==0;
}
}
Output:
Enter an even number:
7
This is not even. Enter an even number:
4
--Ok thanks! Now enter an odd number:
4
This is not odd. Enter an odd number:
8
This is not odd. Enter an odd number:
9
**Thanks! Bye!**
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.