bartleby

Concept explainers

Question
Book Icon
Chapter 12, Problem 12.1PE
Program Plan Intro

Program to throw the NumberFormatException exception

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.

Expert Solution & Answer
Check Mark
Program Description Answer

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);

}

}

Sample Output

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?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
When the FCC added Color Television to the Industry Standards, they went with the system developed in the 1940s by Peter Goldman for CBS.   Question 15 options:   True   False Part of the reason that many critics disliked 1950s gameshows was the fact that gameshows offered one of the few opportunities to see unscripted interactions with "real" (average/non-famous) people on television.   Question 16 options:   True   False The Andy Griffith Show is an example of the "rural revival" shows that become enormously popular on 1960s American television.   Question 19 options:   True   False During the Network Era, the hours before primetime each day were exclusively devoted to locally-produced programming, not programming dictated by an affiliate station's parent network.   Question 20 options:   True   False
Although color television was not added to the industry standard until 1956, CBS had been broadcasting selected special events in color as early as 1950.   Question 1 options:   True   False Two key factors in creating the Network Era of American television were the FCC licensing freeze and ______________.   Question 4 options:   The Quiz Show Scandals   Habitual Viewing   Operation Frontal Lobes   Drop-In Viewing Least Objectionable Programming was designed to embrace the public service-oriented vision of using television to elevate mass culture and enrich viewers.   Question 6 options:   True   False By the end of the 1950s, all three remaining networks (NBC, CBS, & ABC) were broadcasting their entire nightly programming schedule in full color.   Question 9 options:   True   False
7. See the code below and solve the following. public class Test { public static void main(String[] args) { int result = 0; } result = fn(2,3); System.out.println("The result is: + result); // fn(x, 1) = x // fn(x, y) = fn(x, y-1) + 2, when y>1 public static int fn(int x, int y) { if (x <= 1) return x; else return fn(x, y-1) + 2; } } 7-1. This program has a bug that leads to infinite recursion. Modify fn(int x, int y) method to fix the problem. (2 point) 7-2. Manually trace the recursive call, fn(2,3) and show the output (step by step). (2 point) 7-3. Can you identify the Base Case in recursive method fn(int x, int y)? (1 point)

Chapter 12 Solutions

Instructor Solutions Manual For Introduction To Java Programming And Data Structures, Comprehensive Version, 11th Edition

Ch. 12.4 - Prob. 12.4.3CPCh. 12.4 - Prob. 12.4.4CPCh. 12.4 - Prob. 12.4.5CPCh. 12.4 - Prob. 12.4.6CPCh. 12.4 - What is displayed when running the following...Ch. 12.4 - Prob. 12.4.8CPCh. 12.4 - What does the method getMessage() do?Ch. 12.4 - What does the method printStackTrace() do?Ch. 12.4 - Prob. 12.4.11CPCh. 12.4 - Prob. 12.4.12CPCh. 12.5 - Prob. 12.5.1CPCh. 12.6 - Prob. 12.6.1CPCh. 12.7 - Prob. 12.7.1CPCh. 12.8 - Prob. 12.8.1CPCh. 12.9 - Prob. 12.9.1CPCh. 12.9 - Prob. 12.9.2CPCh. 12.10 - What is wrong about creating a File object using...Ch. 12.10 - How do you check whether a file already exists?...Ch. 12.10 - Can you use the File class for I/O? Does creating...Ch. 12.11 - Prob. 12.11.1CPCh. 12.11 - Prob. 12.11.2CPCh. 12.11 - Prob. 12.11.3CPCh. 12.11 - Prob. 12.11.4CPCh. 12.11 - What will happen if you attempt to create a...Ch. 12.11 - Prob. 12.11.6CPCh. 12.11 - Suppose you enter 45 57, 8 789, then press the...Ch. 12.11 - Prob. 12.11.8CPCh. 12.12 - How do you create a Scanner object for reading...Ch. 12.13 - Prob. 12.13.1CPCh. 12.13 - Simplify the code in lines 20-28 as follows: 1....Ch. 12 - Prob. 12.1PECh. 12 - (InputMismatchException) Write a program that...Ch. 12 - (ArrayIndexOutOfBoundsException) Write a program...Ch. 12 - (IllegalArgumentException) Modify the Loan class...Ch. 12 - (IllegalTriangleException) Programming Exercise...Ch. 12 - (NumberFormatException) Listing 6.8 implements the...Ch. 12 - Prob. 12.7PECh. 12 - Prob. 12.8PECh. 12 - Prob. 12.9PECh. 12 - Prob. 12.10PECh. 12 - Prob. 12.11PECh. 12 - (Reformat Java source code) Write a program that...Ch. 12 - (Count characters, words, and lines in a file)...Ch. 12 - (Process scores in a text file) Suppose a text...Ch. 12 - (Write/read data) Write a program to create a file...Ch. 12 - Prob. 12.16PECh. 12 - (Game: hangman) Rewrite Programming Exercise 7.35....Ch. 12 - Prob. 12.18PECh. 12 - (Count words) Write a program that counts the...Ch. 12 - Prob. 12.20PECh. 12 - (Data sorted?) Write a program that reads the...Ch. 12 - Prob. 12.22PECh. 12 - (Process scores in a text file on the Web) Suppose...Ch. 12 - (Create large dataset) Create a data file with...Ch. 12 - (Create a directory) Write a program that prompts...Ch. 12 - Prob. 12.26PECh. 12 - (Replace words) Suppose you have a lot of files in...Ch. 12 - (Rename files) Suppose you have a lot of files in...Ch. 12 - (Rename files) Suppose you have several files in a...Ch. 12 - (Occurrences of each letter) Write a program that...Ch. 12 - (Baby name popularity ranking) The popularity...Ch. 12 - (Ranking summary) Write a program that uses the...
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning