0% found this document useful (0 votes)
185 views2 pages

COMP201 Assignment 2

This document describes Assignment 2 for COMP201, which involves building a calculator application that can perform arithmetic on large numbers represented as BigNumbers. The calculator should convert infix expressions to postfix notation and then evaluate them. It should support operations like addition, subtraction, multiplication, division, and modulus on BigNumbers, as well as features like negation and absolute value. The application is required to have a GUI, handle errors gracefully, and optionally support additional operations like factorial and exponentiation. The document provides details on converting expressions to postfix form using a library of classes and evaluating the results.

Uploaded by

catchmeukzn
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
185 views2 pages

COMP201 Assignment 2

This document describes Assignment 2 for COMP201, which involves building a calculator application that can perform arithmetic on large numbers represented as BigNumbers. The calculator should convert infix expressions to postfix notation and then evaluate them. It should support operations like addition, subtraction, multiplication, division, and modulus on BigNumbers, as well as features like negation and absolute value. The application is required to have a GUI, handle errors gracefully, and optionally support additional operations like factorial and exponentiation. The document provides details on converting expressions to postfix form using a library of classes and evaluating the results.

Uploaded by

catchmeukzn
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

COMP201 Assignment 2 - 2010 1.

Purpose

The purpose of this assignment is to use the class BigNumber that you wrote in Assignment 1, together with the postfix expression evaluation algorithm discussed in class, to provide a calculator which can perform calculations on integers of arbitrary precision, as provided for in the BigNumber class. 2. Specification

The calculator should provide a nice GUI interface. A standard infix arithmetic expression is to be entered into the calculator by typing it in through an input box. Ideally you should as well provide keys, as in a standard calculator, which can be pressed to enter the expression. Extra credit will be given if you provide this facility in your calculator. The expression should then be converted to postfix (see Section 3), evaluated using the standard postfix evaluation algorithm, and the result displayed in a suitable format. The operands in the expression should be BigNumbers (apart from a couple of exceptions, for which see below). The following operators should be provided as a minimum addition subtraction multiplication division remainder negation absolute value + * / % | ... | e.g. 100 + 200 e.g. 100 - 200 e.g. 100 * 200 e.g. 100 / 200 e.g. 100 % 200 e.g -100 e.g. |100 - 200|

For extra credit you can add the following operators factorial ! e.g. 100! exponentiation ^ or ** e.g 100 ^ 50 or 100 ** 50 The operand of the factorial operator and the exponent operand of the exponentiation operator should be limited to integer rather than BigNumber values. Your calculator should be robust, so that it should not crash if errors occur. The BigNumber class handles errors, such as, e.g., a division by zero, by throwing an ArithmeticException. The infix to postfix conversion process described in Section 3 handles an illegal infix expression by throwing a ParseException. Any Stack class you use should also handle errors, like, e.g, popping an empty stack, by throwing an appropriate exception. Our MyStackArray and MyStackList classes, for instance, throw a NoSuchElementException, while Javas Stack class throws a StackEmptyException. Your calculator should catch these exceptions, display a suitable error message, and allow the user to enter another expression.

3.

Converting an Infix Expression

I have provided in the Assignment 2 folder on Pelican and CSMoodle a suite of four Java classes, BNExp2PostFix.java, ExpLexer.java, Lexer.java, and Token.java which together provide a method of converting an infix expression string to postfix in the form of an ArrayList of BigNumbers. To use the classes, code the following
/* Create an object to convert the String infix containing the infix expression to postfix */

BNExp2PostFix converter = new BNExp2PostFix(infix); /* Use the BNExp2PostFix object to do the conversion and return the postfix */ ArrayList<BigNumber> postfix = converter.convert();

The returned ArrayList then contains operands as nonnegative BigNumbers (negative BigNumbers are handled by using the minus sign operator followed by the BigNumber). Operators are returned as negative BigNumbers The converter can handle all the nine operators given in Section 2, together with left and right parentheses. In BNExp2PostFix numeric codes are declared for each of the operators , e.g. ADD = -1 for the addition operator + SUB = -2 for the subtraction operator .................. ABS = -9 for the absolute value operator | The sign of the BigNumbers in the returned ArrayList can then be used to differentiate between operands and operators. All operands are positive, while all operators are negative. E.g the infix expression string -|100 - 200| * 40 ^5! would be converted to the postfix expression 100 200 - | - 40 5 ! ^ * and returned as the postfix ArrayList Index 0 1 2 3 4 5 6 7 8 9 BigNumber 100 200 -2 -9 -8 40 5 -7 -6 -3 Meaning Operand 100 Operand 200 Subtract operator code Absolute value operator code Minus sign operator code Operand 40 Operand 5 Factorial operator code Exponentiation operator code Multiplication operator code

Syntax errors in the infix expression are handled by throwing a ParseException, found in the java.text package. The thrown exception object has two methods you can call to get information about the error (see the documentation for ParseException for details). 4. Suggestions

Ideally, of course, you should use your own BigNumber class from Assignment 1. However, if you havent quite got it working fully, and/or you didnt implement all the operators required for this assignment, you can use my version of the class which I gave you as part of Assignment 1. 5. Submission

The completed assignment is to be submitted by 21 October. You will need to submit the properlydocumented source code of your class together with a record of the testing you undertook to show the correctness of your code. Details of exactly how to submit will be provided later.

-2-

You might also like