![Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780134670942/9780134670942_largeCoverImage.gif)
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.
![Check Mark](/static/check-mark.png)
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)
- After the FCC licensing freeze was lifted, sitcoms featuring urban settings and working class characters became far less common. Question 14 options: True Falsearrow_forwardsolve this questions for me .arrow_forwarda) first player is the minimizing player. What move should be chosen?b) What nodes would not need to be examined using the alpha-beta pruning procedure?arrow_forward
- Consider the problem of finding a path in the grid shown below from the position S to theposition G. The agent can move on the grid horizontally and vertically, one square at atime (each step has a cost of one). No step may be made into a forbidden crossed area. Inthe case of ties, break it using up, left, right, and down.(a) Draw the search tree in a greedy search. Manhattan distance should be used as theheuristic function. That is, h(n) for any node n is the Manhattan distance from nto G. The Manhattan distance between two points is the distance in the x-directionplus the distance in the y-direction. It corresponds to the distance traveled along citystreets arranged in a grid. For example, the Manhattan distance between G and S is4. What is the path that is found by the greedy search?(b) Draw the search tree in an A∗search. Manhattan distance should be used as thearrow_forwardwhats for dinner? pleasearrow_forwardConsider the follow program that prints a page number on the left or right side of a page. Define and use a new function, isEven, that returns a Boolean to make the condition in the if statement easier to understand. ef main() : page = int(input("Enter page number: ")) if page % 2 == 0 : print(page) else : print("%60d" % page) main()arrow_forward
- What is the correct python code for the function def countWords(string) that will return a count of all the words in the string string of workds that are separated by spaces.arrow_forwardConsider the following program that counts the number of spaces in a user-supplied string. Modify the program to define and use a function, countSpaces, instead. def main() : userInput = input("Enter a string: ") spaces = 0 for char in userInput : if char == " " : spaces = spaces + 1 print(spaces) main()arrow_forwardWhat is the python code for the function def readFloat(prompt) that displays the prompt string, followed by a space, reads a floating-point number in, and returns it. Here is a typical usage: salary = readFloat("Please enter your salary:") percentageRaise = readFloat("What percentage raise would you like?")arrow_forward
- assume python does not define count method that can be applied to a string to determine the number of occurances of a character within a string. Implement the function numChars that takes a string and a character as arguments and determined and returns how many occurances of the given character occur withing the given stringarrow_forwardConsider the ER diagram of online sales system above. Based on the diagram answer the questions below, a) Based on the ER Diagram, determine the Foreign Key in the Product Table. Just mention the name of the attribute that could be the Foreign Key. b) Mention the relationship between the Order and Customer Entities. You can use the following: 1:1, 1:M, M:1, 0:1, 1:0, M:0, 0:M c) Is there a direct relationship that exists between Store and Customer entities? Answer Yes/No? d) Which of the 4 Entities mention in the diagram can have a recursive relationship? e) If a new entity Order_Details is introduced, will it be a strong entity or weak entity? If it is a weak entity, then mention its type?arrow_forwardNo aiarrow_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
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337102100/9781337102100_smallCoverImage.gif)
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
![Text book image](https://www.bartleby.com/isbn_cover_images/9781133187844/9781133187844_smallCoverImage.gif)
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337102124/9781337102124_smallCoverImage.gif)