Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
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
(Creating Custom Exception Class) Write a Password class that stores a password string. Passwords must contain at least seven alpha-numeric characters, with at least one letter and one digit. Also write an InvalidPasswordException class that extends Exception. The  Password constructor should throw an InvalidPasswordException, which should report an appropriate message based on a given string (e.g. if the string contains no digits, the exception should report this fact).  Write a GUI program to prompt the user to enter a password and report an InvalidPasswordException if an invalid password is entered. Some sample screenshots are shown below:
(Creating Custom Exception Class) Write a Password class that stores a password string. Passwords must contain at least seven alpha-numeric characters, with at least one letter and one digit. Also write an InvalidPasswordException class that extends Exception. The  Password constructor should throw an InvalidPasswordException, which should report an appropriate message based on a given string (e.g. if the string contains no digits, the exception should report this fact).  Write a GUI program to prompt the user to enter a password and report an InvalidPasswordException if an invalid password is entered. Please teach and help me how to deisgn the program based on the two files given .Some sample screenshots are shown below: InvalidPasswordException.java public class InvalidPasswordException extends Exception{    public InvalidPasswordException (String errMsg){        super(errMsg);    }    }   Password.java public class Password  {private String passwordstr;       public…
Introduction to C++ Programming and Data Structures, 4th Edition by Daniel Liang. (HexFormatException)Implement the hex2Dec function in Programming Exercise 16.1 to throw a HexFormatException if the string is not a hex string .Define a custom exception class named HexFormatException. Write a test program that prompts the user to enter a hex number as a string and displays the number in decimal. If the function throws an exception, display "Not a hex number".

Chapter 12 Solutions

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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education