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.
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.
divide even number by 2 and what is the reminder? duvid odd number by 2 and...
divide even number by 2 and what is the reminder? duvid odd number by 2 and what is the reminder? here modulus is a remiander function. MY_ARRAY[] is an array if 49 elements or DINT[49]. Create four versions of structured text code that sets all even elements of MY_ARRAY[] =3 and all odd elements = 7 using. You can use any function or construct in structured text however each version will contain only 1 type of loop construct: 1) 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT