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

Write a method isEven that uses the remainder operator (%) to determine whether

ID: 3572493 • Letter: W

Question

Write a method isEven that uses the remainder operator (%) to determine whether an integer is even. The method should take an integer  argument and return true if the integer is even and false otherwise. Incorporate this method into an application that inputs a sequence of integers (one at a time) and determines whether each is even or odd.

import java.util.Scanner;
public class EvenOdd
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

System.out.println("Enter a number ");
int number = input.nextInt();

if (isEven(number))
System.out.println(number + " is even");
else
System.out.println(number + " is odd");
  
System.out.println("Enter y to play again, n to quit(y/n):");
check = read.next();
  
while (check.equalsIgnoreCase("Y"))
{
   System.out.println("Enter a number ");
   int number = input.nextInt();

   if (isEven(number))
       System.out.println(number + " is even");
   else
       System.out.println(number + " is odd");
}

public static boolean isEven(int number)
{
return number % 2 == 0;
}
}

What am I doing wrong here?

Explanation / Answer

EvenOdd.java


import java.util.Scanner;
public class EvenOdd
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a number ");
int number = input.nextInt();
if (isEven(number))
System.out.println(number + " is even");
else
System.out.println(number + " is odd");
  
System.out.println("Enter y to play again, n to quit(y/n):");
String check = input.next();
      
while (check.equalsIgnoreCase("Y"))
{
System.out.println("Enter a number ");
number = input.nextInt();
if (isEven(number))
System.out.println(number+ " is even");
else
System.out.println(number + " is odd");
System.out.println("Enter y to play again, n to quit(y/n):");
check = input.next();
}
}
public static boolean isEven(int number)
{
return number % 2 == 0;
}
}

Output:

Enter a number
5
5 is odd
Enter y to play again, n to quit(y/n):
y
Enter a number
2
2 is even
Enter y to play again, n to quit(y/n):
y
Enter a number
4
4 is even
Enter y to play again, n to quit(y/n):
y
Enter a number
7
7 is odd
Enter y to play again, n to quit(y/n):
n

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote