Concept explainers
Program plan:
- Import the required packages into the program.
- Create a class Exercise12_01 to throw the NumberFormatException exception.
- In the main() method,
- Declare the required variables.
- Check whether the length of argument is not equal to 3. If yes, display error message.
- In try block, assign the first argument value to variable.
- In catch block, catch the exception if error occurs.
- In try block, assign the third argument value to variable.
- In catch block, catch the exception if error occurs.
- Match the argument 1 with character 0 using switch case.
- If case "+" matches, then add the two numbers and stores the result.
- If case "-" matches, then subtract the two numbers and stores the result.
- If case "." matches, then multiply the two numbers and stores the result.
- If case "/" matches, then divide the two numbers and stores the result.
- Display the result.
The below program demonstrates how to operate mathematical operations and throw a NumberFormatException exception if error occurs.
Explanation of Solution
Program:
Filename: Exercise12_01.java
//Class definition
public class Exercise12_01
{
//Main method
public static void main(String[] args)
{
//Declare and initialize variables
int num1, num2, result = 0;
/*Check whether the length of argument is not equal to 3. */
if (args.length != 3) {
//Display error message
System.out.println("please use java Exercise12_01 operand1 operator operand2");
//Exit the program
System.exit(1);
}
//In try block
try {
//Assign the argument 0 as "num1"
num1 = Integer.parseInt(args[0]);
}
//In catch block
catch (NumberFormatException ex) {
//Display the exception
System.out.println("Wrong Input: " + args[0]);
return;
}
//In try block
try {
//Assign the argument 2 as "num2"
num2 = Integer.parseInt(args[2]);
}
//In catch block
catch (Exception ex) {
//Display the exception
System.out.println("Wrong Input: " + args[2]);
return;
}
//Match the argument 1 with character 0
switch (args[1].charAt(0)) {
//If case "+" matches
case '+':
/*Add the num1 and num2 and stores the result */
result = num1 + num2;
//Break the program
break;
//If case "-" matches
case '-':
/*Subtract the num2 from num1 and stores the result. *
result = num1 - num2;
;
//Break the program
break;
//If case "." matches
case '.':
/*Multiply the num1 with num2 and stores the result */
result = num1 * num2;
;
//Break the program
break;
//If case "/" matches
case '/':
/*Divide the num1 by num2 and stores the result */
result = num1 / num2;
;
}
//Display the calculated result
System.out.println(args[0] + " " + args[1] + " " + args[2] + " = " + result);
}
}
Command to run the program:
java Exercise12_01 4 + 5
4 + 5 = 9
Additional Output:
java Exercise12_01 4 - 5
4 – 5 = -1
java Exercise12_01 4x - 5
Wrong Input: 4x
Want to see more full solutions like this?
Chapter 12 Solutions
Introduction to Java Programming and Data Structures Comprehensive Version (11th Edition)
- Direction:Analyze the given problem and create a java program using exception. Problem:Create a program that will accept any real number then,converts the number to fractional number,and multiply the value by 9.arrow_forwardSubject: Object Oriented PrgrammingLanguage: Java ProgramTopic: Exception Define a new exception, called ExceptionLineTooLong, that prints out the error message "The strings is too long". Write a program that reads phrase and throws an exception of type ExceptionLineTooLong in the case where a string is longer than 80 characters. EXAMPLE: Input:The quick brown fox jumped over the lazy dogs. Output:The quick brown fox jumped over the laze dogs. ANOTHER EXAMPLE: Input:The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick…arrow_forward[Access Control Policy]: A computer system usually implements the exception handling mechanism. Like the interrupt handling mechanism, the original program flow will be stopped until the exception is handled. For example, when a program is calculating c=a/b, and for some reason b was set to 0, the program will encounter the "Division by Zero" exception. The control will be given to the exception handler, and the recovery action could be "report only", "return to the next instruction", "purge the program", etc., based on the severity of the problem. From the abov information, we can conclude: A computer system can integrate both Discretionary Access Control (DAC) and Mandatory Access Control (MAC) security policies An administrator can inhibit the exception handling The exception handling mechanism is like the Mandatory Access Control security policy, no matter which user caused the exception (even the administrator), the program should be stopped, and the control should be given to the…arrow_forward
- (Throwing Exceptions from a catch) Suppose a program throws an exception and the appropriate exception handler begins executing. Now suppose that the exception handler itself throwsthe same exception. Does this create infinite recursion? Write a program to check your observation.arrow_forwardJava : Write the code segments in main( ) and in methodA( ) for the following scenario: main( ) passes methodA an int variable called a. methodA generates an exception, but does not deal with it, if a is not between 1 and 100. Hint: main will try and catch the exception and methodA throws exception.arrow_forwardFoundation in java Please provide a basic exception code. Do not use input, output. Please provide code and explain. Thank you. Write the Java code for the following: Write the InvalidGradeException class that will be used below. Write the Java program that reads an integer grade from a user and checks that it is valid (between 0 and 100). If it is invalid an InvalidGradeException will be thrown, stating what the invalid grade is. This will be caught in the main( ) method which will write a message about the invalid grade. If the grade is valid, it is written to the monitor..arrow_forward
- use C++ programing language Write a program that prompts the user to enter a person's date of birth in numeric form such as 8-27-1980. The program then outputs the date of birth in the form: August 27, 1980. Your program must contain at least two exception classes: invalidDay and invalidMonth. If the user enters an invalid value for day, then the program should throw and catch an invalidDay object. Similar conventions for the invalid values of month and year. (Note that your program must handle a leap year.)arrow_forwardDefine the process of Generating the address of an exception handler ?arrow_forwardPYTHON: Write a program that reads integers user_num and div_num as input, and output the quotient (user_num divided by div_num). Use a try block to perform all the statements. Use an except block to catch any ZeroDivisionError and output an exception message. Use another except block to catch any ValueError caused by invalid input and output an exception message. Note: ZeroDivisionError is thrown when a division by zero happens. ValueError is thrown when a user enters a value of different data type than what is defined in the program. Do not include code to throw any exception in the program. Ex: If the input of the program is: 15 3 the output of the program is: 5 Ex: If the input of the program is: 10 0 the output of the program is: Zero Division Exception: integer division or modulo by zero Ex: If the input of the program is: 15.5 5 the output of the program is: Input Exception: invalid literal for int() with base 10: '15.5'arrow_forward
- 1. Write a java program to display the A) Arithmetic exception. B) Array bounds exception. C) String bounds exception. Using try and catch. 2. Write a Java program to find the evaluation of quadratic equation using methods. (Provided – a=2,b=6 and c=8) . 3.Using Java Biuild Rectangle class will also have the following methods: Specification: setLength- The setLength method will store a value in an object’s length field. setWidth- The setWidth method will store a value in an object’s width field. getLength- The getLength method will return the value in an object’s length field. getWidth- The getWidth method will return the value in an object’s width field. getArea- The getArea method will return the area of the rectangle, which is the result of the object’s length multiplied by its width.arrow_forwardJAVA EXCEPTION HANDLING:Define a new exception, called ExceptionLineTooLong, that prints out the error message "The strings is too long". Write a program that reads phrase and throws an exception of type ExceptionLineTooLong in the case where a string is longer than 80 characters. For example: Input Result The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped over the lazy dogs. The quick brown fox jumped over the lazy dogs.The quick brown fox jumped…arrow_forwardAdvanced programming applications using javaarrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning