
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
MyLab Programming with Pearson eText -- Access Card -- for Introduction to Java Programming and Data Structures, Comprehensive Version
- you can select multipy optionsarrow_forwardFor each of the following, decide whether the claim is True or False and select the True ones: Suppose we discover that the 3SAT can be solved in worst-case cubic time. Then it would mean that all problems in NP can also be solved in cubic time. If a problem can be solved using Dynamic Programming, then it is not NP-complete. Suppose X and Y are two NP-complete problems. Then, there must be a polynomial-time reduction from X to Y and also one from Y to X.arrow_forwardMaximum Independent Set problem is known to be NP-Complete. Suppose we have a graph G in which the maximum degree of each node is some constant c. Then, is the following greedy algorithm guaranteed to find an independent set whose size is within a constant factor of the optimal? 1) Initialize S = empty 2) Arbitrarily pick a vertex v, add v to S delete v and its neighbors from G 3) Repeat step 2 until G is empty Return S Yes Noarrow_forward
- Please help me answer this coding question in the images below for me(it is not a graded question):write the code using python and also provide the outputs requiredarrow_forwardWhat does the reduction showing Vertex Cover (VC) is NP-Complete do: Transforms any instance of VC to an instance of 3SAT Transforms any instance of 3SAT to an instance of VC Transforms any instance of VC to an instance of 3SAT AND transforms any instance of 3SAT to an instance of VC none of the abovearrow_forwardPlease assist me by writing out the code with its output (in python) using the information provided in the 2 images below.for the IP Address, it has been changed to: 172.21.5.204the serve code has not been open yet though but the ouput must be something along these lines(using command prompt):c:\Users\japha\Desktop>python "Sbongakonke.py"Enter the server IP address (127.0.0.1 or 172.21.5.199): 172.21.5.204Enter your student number: 4125035Connected to server!It's your turn to pour! Enter the amount to your pour (in mL):Please work it out until it gets the correct outputsNB: THIS QUESTION IS NOT A GRADED QUESTIONarrow_forward
- need help with a html code and css code that will match this image.arrow_forwardneed help with a html code and css code that will match this image. Part B - A Navigation Part B is the navigation component of a page. Information you need includes: Color Codes: Visiting links: #ff6666 Unvisited links: #ccff66 Hovered links: white Search box: #2ec4b6 rebeccapurple white Font: Google Font (Roboto) Icons: Font Awesome (fa-quidditch, fa-search) This is a flexbox based navigation menu. Other then padding, all spacing/positioning should be controlled using flex properties. The home link in the nav should point to your assignment file (to triggers visited styling). In the "state" screenshot below, Home is visited, Services is hovered (the mouse doesn't show up in the screenshot) and Products is unvisited.arrow_forwardMGMT SS STATS, an umbrella body that facilitates and serves various Social Security Organizations/Departments within the Caribbean territories, stood poised to meet the needs of its stakeholders by launching an online database. The database will provide members and the public access to the complete set of services that can (also) be initiated face-to-face, and it will provide managed, private, secure access to a repository of public and/or personal information. Ideally, the database will have basic details of pension plans recorded in the registry, member plan statistics, and cash inflows and outflows from pension funds.For example, insured persons accumulate contributions. Records for these persons will include information on the insured persons able to acquire various benefits once work is interrupted due to sickness, death, retirement, and maternity or employment injury. They will also include information on pensions such as invalidity, disability, and survivors that stem from one…arrow_forward
- Why all appvif i want to sign in its required phone number why not using google or apple its make me frustratedarrow_forwardWhy is the accuracy of time important in data visualizations? Detail a scenario from your professional experience in which time was structured poorly in a data visualization. How did this affect the understanding of the data presented? How do you think this error or oversight occurred?arrow_forwardWrite the KeanStudent class. The UML diagram of the class is represented below: KeanStudent - fullName: String - keanID: int -keanEmailAddress: String cellPhoneNumber: String + numberOfStudent: int + KeanStudent() + KeanStudent(fullName: String, keanID: int, keanEmailAddress: String, cellPhoneNumber: String) +getFullName(): String +setFullName(newFullName: String): void +getKeanIDO): int +getKeanEmailAddress(): String +getCellPhoneNumber(): String + setCellPhoneNumber(newCellPhoneNumber: String): void +toString(): String 1. Implement the KeanStudent class strictly according to its UML one-to-one (do not include anything extra, do not miss any data fields or methods) 2. Implement a StudentTest class to test the class KeanStudent you just created. • Create two KeanStudent objects using a no-args constructor and one from the constructor with all fields. o Print the contents of both objects. 。 Print numberOfStudent. 3. Add comments to your program (mark where data fields, constructors,…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




