1) Complete the main() method of the program we started in class, adding an else block so that when the user enters a negative number, an error message is generated.
public static void main(String[] args) {
String inputValue = JOptionPane.showInputDialog(
"Enter a value:");Scanner stringReader =
new Scanner( inputValue ); // create a Scanner that can convert Strings to numeric values double value = stringReader.nextDouble(); // convert the user inputString to a double (if possible)System.
out.println("The value is " + value ); if( value >= 0 ) { // 0 or positive valuevalue = Math.sqrt( value );
// compute the square root of the value and reassign the result to the same variableSystem.
out.println("The square root of the value is " + value );} else { // value is negative - Note we could also write
else if( value < 0 )}
2
) Write another program that prompts the user to enter a positive integer value from 5 to 9. Use nested if conditionals to:public static void main(String[] args) {
String inputValue = JOptionPane.showInputDialog(
"Enter a value from 5 to 9:");Scanner stringReader =
new Scanner( inputValue ); // create a Scanner that can convert Strings to numeric values int value = stringReader.nextInt(); // convert the user inputString to a int (if possible)System.
out.println("The value is " + value ); if( value > 9 ) { // too largeSystem.
out.println("Error. The value is too large (greater than 9)." );} else if( value < 5 ) { //
too small}
}