Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How could the given program be finished in Java? Write a program that reads in two numbers from the user and performs several mathematical operations

How could the given program be finished in Java?

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Write a program that reads in two numbers from the user and performs several mathematical operations on them. Your program should handle the following exceptions: - If either number is not a valid integer, your program should throw a NumberFormatException. - If the second number is zero, your program should throw an ArithmeticException. - If the result of any operation is outside the range of an integer, your program should throw an IllegalArgumentException. Your program should catch each of these exceptions and print an appropriate error message to the console. If no exceptions are thrown, your program should print the results of the mathematical operations. Here are the instructions to complete the assignment: - In your main program, use a Scanner object to read in two numbers from the user as strings. - Use the Integer.parseInt() method to convert each string to an integer. This will throw a NumberFormatException if the input is not a valid integer. - Perform the following mathematical operations on the numbers: addition, subtraction, multiplication, and division. If the second number is zero, throw an ArithmeticException. If the result of any operation is outside the range of an integer, throw an IllegalArgumentException. - Use a try-catch block to catch each of the exceptions thrown by the program. - If no exceptions are thrown, print the results of the mathematical operations to the console. 1I TODo: Handle any exceptions that are thrown and print an aporopriate error ilessage to the console public static int add(int a, int b) throws IllegalArgumentException \{ 1) TODO: Perform the addition operation on the two integers /I IQDo: If the result is outside the range of an integer throw an IllegalarguientException with an. agproplate error message II TODO: Return the result \} public static int subtract(int a, int b) throws IllegalArgumentException \{ 1/ IoDo: Perform the subtraction operation on the two integers 1/ 1000: If the result is outside the range of an integer throw an Illegalarqument Exception with an, aporopriate error message 1) TODO: Return the result 3 public static int multiply(int a, int b) throws IllegalArgumentException \{ 1/ Topo: Perform the multiplication operation on the two integers 1) TODo: If the result is outside the range of an integer throw an Illegalargumentexception with an, aporopiate error message 1/ TODO: Return the result \}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions