Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Program You will be reading a line of data either using .hasNext until user enters -1 number operator number -1 Read in all numbers

Java Program

You will be reading a line of data either using .hasNext until user enters -1

number operator number

-1

Read in all numbers as double, and when input has correct syntax output a double. You will never have more than 3 arguments per line.

You first echo the input followed by = and then output (don't echo -1 when at EOF)...

If you don't get a number, you need to throw a NumberFormatException, When you create the number format exception you will be using the constructor NumberFormatException( "Format is number operator number. You entered: " + BadArgument ); When an illegal number has been typed you need to output toString in NumberFormatException and then read the next line of text. Don't check for other exceptions.

Legal operators are: +,-,* and /. If you need get a legal operator . You need to throw an InputMismatchException, you will be using the constructor IllegalArgumentException( "Legal operators are +,-,* and /. You entered: " + BadArgument ). When an illegal operator has been entered you need to output toString in IllegalArgumentException and then read the next line of text. Don't check for other exceptions.

Third argument needs to be a number repeat 1.

On correct input output the solution.

You will read multiple lines in a file, keep reading until EOF or if you read a single -1 from an input line. When you reach EOF or read in only a -1, call System.exit( -1 ).

For each legal line read print out a line with the result of the operation. For example...

Input Output lines
3 + 2 3 + 2=5.00
2 / 3 2 / 3=0.67
2 + 2abc 2 + 2abc=java.lang.NumberFormatException: Format is number operator number. You entered: 2abc
2 2=java.util.NoSuchElementException
3 ^ 4 3 ^ 4=java.util.InputMismatchException: Legal operators are +,-,* and /. You entered: ^
2 /3 2 /3=java.util.InputMismatchException: Legal operators are +,-,* and /. You entered: /3
2 / 0 2 / 0=Infinity
=java.util.NoSuchElementException
-1 quit no output
----------------------------------------Here's how it should look like:---------------------------------------- 
Sample Run 1 (User enters 4 + 5 then 4 -5 then 4x then -1 "4 + 5 4 - 5 4x -1 " ): 4 + 5 4 + 5=9.00 4 - 5 4 - 5=-1.00 4x 4x=java.lang.NumberFormatException: Format is number operator number. You entered: 4x -1 Sample Run 2 User Enters: "2 + 3 2 + 2 ^ 3 2 / 0 2 + abc -1 "; 2 + 3 2 + 3=5.00 2 + 2 +=java.util.NoSuchElementException 2 ^ 3 2 ^ 3=java.util.InputMismatchException: Legal operators are +,-,* and /. You entered: ^ 2 / 0 2 / 0=Infinity 2 + abc 2 + abc=java.lang.NumberFormatException: Format is number operator number. You entered: abc Sample Run 3 uses the input String data = "2 + 3 3 ^ 7 2 abc + 2 2x * 2 3 + 6 "; 2 + 3=5.00 3 ^ 7=java.util.InputMismatchException: Legal operators are +,-,* and /. You entered: ^ 2=java.util.NoSuchElementException abc + 2=java.lang.NumberFormatException: Format is number operator number. You entered: abc 2x * 2=java.lang.NumberFormatException: Format is number operator number. You entered: 2x 3 + 6=9.00 
--------------------------------------------------------------------------------------------------------------- 

Hints...

I did all parsing and reading using Scanner. (Scanner has built in Pattern checking such as hasNext, and hasNextInt. );

hasNext needs a Pattern not a RegExp string, i.e., hasNext( Pattern.compile( "\\{|\\[" ) );

I put my try loop inside a infinite while loop, you can have multiple catch statements after the while loop.

For all exception except EOF you are outputting a line with toString for the exception.

For floating point numbers, Infinity is default output of number / 0.0, IEEE 754 defines infinity for double, integers don't have infinity.

I'm using the standard "%,.2f" for output of calculations

Note when your throw the above exception, you need to enter the respective imports, Netbeans is not importing them.

You can use javax.script.ScriptEngine.eval to determine value, I didn't.

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

The Database Relational Model A Retrospective Review And Analysis

Authors: C. J. Date

1st Edition

0201612941, 978-0201612943

More Books

Students also viewed these Databases questions

Question

dy dx Find the derivative of the function y=(4x+3)5(2x+1)2.

Answered: 1 week ago