Write a custom exception class called ParameterNotAllowedException that contains the following: 1. An integer instance variable representing the input number that went wrong. 2. A two parameter constructor that accepts a message and an integer, This constructor should copy the integer parameter into the instance variable and call the Exception class's one parameter constructor. 3. An overidden getMessage() method that returns a message explaining that the integer is invalid.

Respuesta :

Answer:

See explaination

Explanation:

//class extends Exception

class ParameterNotAllowedException extends Exception {

//Instance variable

private int input;

//Argumented constructor

public ParameterNotAllowedException(String message, int input) {

super(message);

this.input = input;

}

public int getInput() {

return input;

}

public void setInput(int input) {

this.input = input;

}

atOverride // Replace the "at" with at symbol ie shift 2

public String getMessage() {

//Returns the message

return input+" is invalid. "+super.getMessage();

}

}

class Main {

public static void main(String[] args) throws ParameterNotAllowedException {

int n = -1;

//Throw negative not allowed exception

if(n<0){

throw new ParameterNotAllowedException("negative number",n);

}

}

}