Positive Or Negative

Report a typo

Extend the given program by creating a checkNumber method which accepts an Object and implement a switch statement with guarded patterns to print certain information depending on the argument.

  • Print "Positive integer" if the object is an Integer greater than 0.

  • Print "Zero" if the object is an Integer equal to 0.

  • Print "Negative integer" if the object is an Integer less than 0.

  • Print "Not an integer" for any other object.

Sample Input 1:

11

Sample Output 1:

Positive integer
Write a program in Java 17
import java.util.Scanner;

public class Main {
// implement the method below
public static void ... {
// your code goes here ...
}

// please do not change the code below
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();

try {
int number = Integer.parseInt(input);
checkNumber(number);
} catch (NumberFormatException e) {
checkNumber(input);
}

scanner.close();
}
}

Create a free account to access the full topic