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
Instructor Solutions Manual For Introduction To Java Programming And Data Structures, Comprehensive Version, 11th Edition
- 6. See the code below and solve the following. import java.io.*; public class DataStream { } public static void main(String[] args) } DataOutputStream output = new DataOutputStream(new FileOutputStream("temp.dat")); output.writeUTF("Book1"); output.writeInt(85); output.writeUTF("Book2"); output.writeInt(125); output.writeUTF("Book3"); output.writeInt(70); output.close(); // ToDo: Read all data from temp.dat and print the data to the standard output (monitor) 6-1. This program has a compile error, and the message is “Unhandled exception type FileNotFoundException". How do you fix this error? (1 point) 6-2. Is FileNotFoundException a checked exception or an unchecked exception? (1 point) 6-3. What is the difference between checked exception and unchecked exception? (1 point) 6-4. Please complete the above program by reading all data from temp.dat and print the data to the standard output (monitor) by using System.out.print, System.out.println or System.out.printf method. (2 points)arrow_forwardWrite a program that reads a list of integers from input and determines if the list is a palindrome (values are identical from first to last and last to first). The input begins with an integer indicating the length of the list that follows. Assume the list will contain a maximum of 20 integers. Output "yes" if the list is a palindrome and "no" otherwise. The output ends with a newline. Hints: - use a for loop to populate the array based on the specified size (the first number entered) - use a for loop to check first value with last value, second value with second from end, etc. - if the values do not match, set a Boolean variable to flag which statement to output (yes or no) Ex: If the input is (remember to include spaces between the numbers): 6 1 5 9 9 5 1 the output is: yes Ex: If the input is: 5 1 2 3 4 5 the output is: C++ codingarrow_forwardDesign and draw a high-level "as-is" process diagram that illustrates a current process related to a product or service offered through the SSDCI.gov database.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrProgramming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage Learning




