A garden watering valve can measure the volume of water coming out of a hose. A user fixed the valve to allow only 0.015 gallon of water. Create a Java exception class named “UnmatchedVolumeException” which will pass the following messages to a numerical controller.
Table:
Condition: volume > 0.015 ------- Message: Excessive volume.
Condition: volume < 0.015 ------- Message: Insufficient volume.
In the “main” method, take an input of volume. Write a proper Java exception handling code to throw an appropriate exception message. Make sure the output looks similar to the following. [Note: Be sure to use input and output dialog boxes]. Please have the input box ask "Enter the volume:" and have the output box give an output of "Excessive volume."
public class UnmatchedVolumeException extends RuntimeException{
public UnmatchedVolumeException(String message){
super(message);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the volume: ");
double volume = in.nextDouble();
if (volume > 0.015) {
throw new UnmatchedVolumeException("Excessive volume.");
} else if (volume < 0.015) {
throw new UnmatchedVolumeException("Insufficient volume.");
}
}
}
Comments
Leave a comment