Question
Solution
JAVA
import java.util.*;
class CheckWeight {
public void validWeight(double weight) throws InvalidWeight {
if (weight > 100) {
//System.out.println(String.format("%.0f is invalid weight", weight));
throw new InvalidWeight(String.format("%.0f is invalid weight", weight));
} else {
System.out.println(String.format("%.0f is the valid weight", weight));
}
}
public static void main(String[] args) {
CheckWeight checkWeight = new CheckWeight();
Scanner scanner = new Scanner(System.in);
System.out.print("Enter weight: ");
double weight = scanner.nextDouble();
try {
checkWeight.validWeight(weight);
} catch (InvalidWeight e) {
System.out.println("Exception caught: " + e.getMessage());
}
scanner.close();
}
}
// Define a user-defined exception named InvalidWeight
class InvalidWeight extends Exception {
public InvalidWeight(String message) {
super(message);
}
}
3/3 test cases passed
3/3 hidden test cases passed