Question

In: Computer Science

Create an application that checks whether an integer is an odd or even number. Welcome to...

Create an application that checks whether an integer is an odd or even number.

Welcome to the Odd/Even Checker! Enter an integer: ten Error! Invalid integer. Try again. Enter an integer: 10.3 Error! Invalid integer. Try again. Enter an integer: 10 The number 10 is even. Continue? (y/n): Error! This entry is required. Try again. Continue? (y/n): y Enter an integer: 9 The number 9 is odd. Continue? (y/n): n

Specifications:

Create a version of the Console class presented in chapter 7 that doesn’t use static methods. Create a MyConsole class that inherits the Console class. Then, override the getString() method so it doesn’t allow the user to enter an empty string. Use an instance of the MyConsole class to get and validate the user’s entries.

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

import java.util.*;
class MyScanner
{
   public static void main (String[] args)
   {
  
       System.out.println("Welcome to the Odd/Even Checker!");
       Scanner sc=new Scanner(System.in);
   while(true)
   {
   // ask for input
   System.out.print("Enter an integer:");
   String input = sc.nextLine();
   try
{
int num = Integer.parseInt(input);
// even??
if(num%2==0)
System.out.println("The number "+num+" is even");
else
System.out.println("The number "+num+" is odd");
  
// ask for choice
System.out.print("Continue? (y/n)");
String chioce = sc.nextLine();
   if(chioce.equals("n"))
   break;

}
catch (NumberFormatException e)
{
// error
System.out.println("Error! Invalid integer. Try again.");
}
   }
   }
}

OUTPUT:

Welcome to the Odd/Even Checker!
Enter an integer:ten
Error! Invalid integer. Try again.
Enter an integer:10.5
Error! Invalid integer. Try again.
Enter an integer:10
The number 10 is even
Continue? (y/n)y
Enter an integer:9
The number 9 is odd
Continue? (y/n)y
Enter an integer:123.er
Error! Invalid integer. Try again.
Enter an integer:123.45
Error! Invalid integer. Try again.
Enter an integer:123
The number 123 is odd
Continue? (y/n)n


Error! Invalid integer. Try again.
Enter an integer:123
The number 123 is odd
Continue? (y/n)n

(PROGRAM ENDED)
============================================


Related Solutions

//----------------------------------------------------------------- // Counts the number of odd, even, and zero digits in an integer // input...
//----------------------------------------------------------------- // Counts the number of odd, even, and zero digits in an integer // input value. Repeat as long as the user wishes to continue //----------------------------------------------------------------- public static void main(String[] args) {     // Declare the identifiers final int SENTINEL = -99;    // Declare the remaining identifiers ... Scanner scan = new Scanner(System.in);    // Display the programmer's information              // Input an integer number          // Count the number of odd, even, and...
Write a function name as 'checkEvenOrOdd' that checks the input value is odd or even number....
Write a function name as 'checkEvenOrOdd' that checks the input value is odd or even number. The main function is given: int main(){     int number;     cout << "Check number input: ";     cin >> number;     cout << "The input number " << number << " is " << checkEvenOrOdd(number) << endl;     return 0; } simple c++ code please
Using for loop . Input an integer and identify whether it's EVEN OR ODD ( without...
Using for loop . Input an integer and identify whether it's EVEN OR ODD ( without using modulo operator ) using only <iostream> . C++ programming
Counts the number of odd, even, and zero digits in an integer input value. Repeat until...
Counts the number of odd, even, and zero digits in an integer input value. Repeat until user does not want to continue. Develop the program in an incremental fashion. For example, write the part of the program, which inputs a single integer value and displays number of odd, even, and zero digits in that number. Submit your partial program for grading to make sure it works for the first few test cases. Below is an example execution of a partial...
1- Create a Java program to determine a certain number whether that number is odd or...
1- Create a Java program to determine a certain number whether that number is odd or even. Hint : use arithmetic operators and expressions to find the odd or even numbers. 2- Create a Java program to ask a user to enter a name and password as shown below: name is “Ahmed” and his password is 2321 or name is “Ali” and his password is 6776 . The program shows a greeting “Hi ..Welcome to my program” if the user...
prove that if an even integer n is subtracted from an odd integer m. then m...
prove that if an even integer n is subtracted from an odd integer m. then m - n is odd.
C++ Create a program that checks whether a number is a prime number and displays its...
C++ Create a program that checks whether a number is a prime number and displays its factors if it is not a prime number. Console Prime Number Checker Please enter an integer between 1 and 5000: 5 5 is a prime number. Try again? (y/n): y Please enter an integer between 1 and 5000: 6 6 is NOT a prime number. It has 4 factors: 1 2 3 6 Try again? (y/n): y Please enter an integer between 1 and...
prove that every integer is either even or odd but never both.
prove that every integer is either even or odd but never both.
Prove that every natural number is odd or even.
Prove that every natural number is odd or even.
Write c code to determine if a binary number is even or odd. If it is...
Write c code to determine if a binary number is even or odd. If it is odd, it outputs 1, and if it is even, it outputs 0. It has to be less than 12 operations. The operations have to be logical, bitwise, and arithmetic.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT