UQ Students should read the Disclaimer & Warning

Note: This page dates from 2005, and is kept for historical purposes.

# COMP2502 – Assignment Two – Trees

For which I achieved 96.45 out of 100.

## Specification

### Overall Goals

The main focus of this assignment is the understanding of trees and the underlying data structures and algorithms including the influence of preorder, inorder and postorder traversal of trees.

The primary goal of this assignment is the programming of a simple integer calculator. This calculator should read from a file, expression.txt, a simple integer arithmetic expression where all of the input values are separated by spaces. An example is the following expression:

The expression should be parsed and simple mistakes detected. For example if a ) is missing an error notice should be printed. The information contained in the expression should be stored in an appropriate tree. The program should then evaluate the expression and store the resulting value in the same tree by using the equals sign as a binary operator. Finally the program should then print out the equation that results from the evalution of the expression in infix notation with all the appropriate symbols and parentheses included. For our given example the appropriate equation would be:

### Detailed Specifications

Build a simple calculator that can perform the following functions:

1. Parse a space separate expression from a file and check for the following:
1. matching parentheses,
2. the appropriate number of operands (numbers) for each operator (+,-,*,/).
2. Store the expression in an appropriate tree.
3. Calculate the result of evaluating the expression. (This step can be done with preorder or postorder transversal of the tree and appropriate java code.)
4. Store the equals sign as a binary operator in the tree and the result of the evaluation as a leaf in the already constructed tree in (2).
5. Output the equation that results from the evaluation of the expression in a fully-parenthesised version. (This can best be done with inorder transversal of the tree.)

The following names are specified to enable testing of the code:

You are expected to submit the file Calculator.java and any other files that your program needs to work. Your calculator will be tested with several different expression.txt files to see if it satisfies the requirements of the assignment.

### Assessment Criteria

Your assignment will be assessed on the following points:

• 30% on reading and parsing the space separated expression
• 20% on the evaluation of the expression
• 20% on the output of the appropriate equation.
• 30% on the style and elegance of your code including the appropriate use of trees.

Calculator.java

## My Results

 Assignment 2 Not recorded Part A (70%) 66 Part B (30%) 25.4545454545455 Comments Operator missing error not identified correctly Bonus 5 Total 96.4545454545455

27-Dec-2004