JAVA Write a boolean method called isOdd in a class called O

JAVA

Write a boolean method called isOdd() in a class called OddTest, which takes an int as an argument and returns true if the int is odd, false if its not. The signature of the method is as follows: public static boolean isOdd(int number) Also write a main() method that prompts user for a number, calls the method isOdd sending the number and prints \"ODD\" or \"EVEN\" depending on the returned value of the method. You should test for negative input.

Run the program. Use the number 13. Run it again, use -2. Capture the console output.

Put the program code and output at the end of your text file

Solution

import java.util.Scanner;

public class OddTest
{
   public static boolean isOdd(int number)
   {
       if(number % 2 == 0)   // used remainder operator % to find number is divisible by 2 or not
       return false;
       else
       return true;
   }

   public static void main (String[] args)
   {
       Scanner scan = new Scanner(System.in);
      
       System.out.println(\"Enter a number\");
      
       int number = scan.nextInt();   //input a number
      
       if(isOdd(number)) // send number as parameter to function isOdd()
       System.out.println(number +\" is an odd number\");
       else
       System.out.println(number +\" is an even number\");
      
   }
}

output:

JAVA Write a boolean method called isOdd() in a class called OddTest, which takes an int as an argument and returns true if the int is odd, false if its not. Th

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site