Assignment 3: Exception Handling Solution

$30.00 $26.40

Description

·Source code: Calculator.java

Part 1: Calculator

Your program must be able to read in the following expressions. You may wish to construct a BNF grammar in the style of the cookie exercise as an initial task.

Compile, run, and test your program with at least these expressions:

lu2153c5e6nq tmp bb846c9dbf19f826

let x = 1;

(let x = 1) + x;

(let a = 2) + 3 * a – 5;

(let x = (let y = (let z = 1))) + x + y + z;

1 + (let x = 1) + (let y = 2) + (1 + x) * (1 + y) – (let x = y) – (let y = 1) – x;

1 + (let a = (let b = 1) + b) + a + 1;

(let a = (let a = (let a = (let a = 2) + a) + a) + a) – 9;

(let x = 2) ^ (let y = 3);

(let y = 3) ^ (let x = 2);

Correct return values are 1, 2, 3, 4, 5, 6, 7, 8, and 9 respectively.

Part 2: Exception Handling

Add Java exception handling to your code by defining two exception classes lu2153c5e6nq tmp 5c2742f78d0bdfd1 SyntaxErrorlu2153c5e6nq tmp 62091f830a909662 and

lu2153c5e6nq tmp b40002d3ada54d10

RuntimeError .

lu2153c5e6nq tmp e88c6940790a3fc7

·A lu2153c5e6nq tmp 111d00368107b604 SyntaxErrorlu2153c5e6nq tmp b4babe95fb32bef4 exception should be thrown when an illegal character is found, a closing lu2153c5e6nq tmp 111d00368107b604 )lu2153c5e6nq tmp 17c2e45405672ddd is not found, or lu2153c5e6nq tmp 17c2e45405672ddd a =lu2153c5e6nq tmp 17c2e45405672ddd is not used in a lu2153c5e6nq tmp 17c2e45405672ddd letlu2153c5e6nq tmp 7c62c2988063eef7 expression.

lu2153c5e6nq tmp 6e02ab5f942a673a lu2153c5e6nq tmp 49f4408a5e667bf3

·A lu2153c5e6nq tmp 68e6cbc42c8c4cd6 RuntimeErrorlu2153c5e6nq tmp 68e6cbc42c8c4cd6 exception should be thrown when an identifier is encountered for which no value can be found.

1 of 5 , 9:20 p.m.

Assignment https://eclass.srv.ualberta.ca/mod/assign/view.php?id=3237745

lu2153c5e6nq tmp 908cd6b4c279e9f8 lu2153c5e6nq tmp d844a41b1c550f4b

The exceptions should propagate the error to the main program which prints the diagnostics of the error. You must handle these errors using Java exceptions and the message should be printed by an exception handler in a catch clause.

These will be the exception test cases:

lu2153c5e6nq tmp cfac159ae3c47640

1+(2

* 3;

// syntax error: ‘)’ expected

(let x

5) + x;

// syntax error: ‘=’ expected

(let x

= 5) (let y

= 6);

// syntax error: operator expected

(let x

= 5 let y =

6);

// syntax error: ‘)’ expected

(ler x

= 5) ^ (let

y = 6);

// runtime error: ‘ler’ undefined

(let x

= 5) + y;

// runtime error: ‘y’ undefined

A Working Procedure Example

lu2153c5e6nq tmp f15561c1e04fcdf7

1 + (let x = 1) + (let y = 2) + (1 + x * (1 + y)) – (let x = y) – x;

lu2153c5e6nq tmp d93c9d47d7842d3d

Expression

Stack

Pop & Return

H

1

1

+

|1+|

/

<E

2

1

+ (let x = 1

| let x = 1|

/

x

|1+|

1

3

1

+ (let x = 1)

|1+1|

(returned from

x

“let x = 1”)

/

| 1 + y|

(“let y = 2”

x

4

1

+ (let x = 1) + (let y = 2) + (1 + x * (1 + y

| 1 + x *|

has already

y

|1+1+2+|

returned 2)

3

5

1

+ (let x = 1) + (let y = 2) + (1 + x * (1 + y)

| 1 + x * 3|

(returned from

x

|1+1+2+|

“1 + y”)

y

/

6

1

+ (let x = 1) + (let y = 2) + (1 + x * (1 + y)) – (let x = y

| let x = y|

(“1 + x * 3”

x

|1+1+2+4-|

has already

y

returned 4)

/

(“let x = y”

x

7

1

+ (let x = 1) + (let y = 2) + (1 + x * (1 + y)) – (let x = y) – x

|1+1+2+4-2-2|

has already

y

returned 2)

2 of 5 , 9:20 p.m.

Assignment https://eclass.srv.ualberta.ca/mod/assign/view.php?id=3237745

lu2153c5e6nq tmp d90820a0a272155b

x

8 1 + (let x = 1) + (let y = 2) + (1 + x * (1 + y)) – (let x = y) – x; <Empty> 4

y

Calculator.java lu2153c5e6nq tmp 9667d71871a66f43

lu2153c5e6nq tmp 908c4ee9f5676502 lu2153c5e6nq tmp 206dc368312200af

lu2153c5e6nq tmp 6fa15af765a9b76 lu2153c5e6nq tmp 139c7f882d474c91

5 of 5 , 9:20 p.m.