Square class

Report a typo

You have a class named Square with a single constructor, which takes only one argument: a. If the argument is positive, the constructor assigns it to the field a or throws a SquareSizeException otherwise.

Here is what you need to do:

  1. Finish the constructor implementation
  2. Handle a custom exception using a try-catch block

Sample Input 1:

-6

Sample Output 1:

zero or negative size
Write a program in Java 17
import java.util.Scanner;

class Square {
int a;

public Square(int a) throws SquareSizeException {
if (a > 0) this.a = a;
else ; //put you code here

}
}

class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int a = scn.nextInt();
//put your code here
Square square = new Square(a);

}
}

class SquareSizeException extends Exception {
public SquareSizeException(String message) {
super(message);
}
}
___

Create a free account to access the full topic