Safe converting

Report a typo

Implement a method for converting a Long value to int (primitive type) according to the following rules:

  • if the given value is null the method should return the default value for ints;
  • if the given value is greater than Integer.MAX_VALUE the method should return the max value for ints;
  • if the given value is lesser than Integer.MIN_VALUE the method should return the min value for ints;
  • otherwise, the method should return the same value as the passed argument.
Write a program in Java 17
import java.util.Scanner;

public class Main {

public static int convert(Long val) {
// write your code here
}

/* Do not change code below */
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String val = scanner.nextLine();
Long longVal = "null".equals(val) ? null : Long.parseLong(val);
System.out.println(convert(longVal));
}
}
___

Create a free account to access the full topic