CSCI 151 - Prelab 3

Due at the start of class on Monday February 26

In lab 3, you'll be writing the internal computations for a GUI-based Calculator program.  In this prelab, you will tackle some of the issues related to the lab.  Please write or type up your solutions, and hand in a paper copy by 10 am on Monday.  Sign the Honor Pledge somewhere on the paper.  Late prelabs are not accepted.

I'd recommend reading chapter 11 in Weiss before doing the prelab.


Your code for lab 3 will use the Stack class found in the java.util package of the java class library.  (You don't need to implement your own Stack class for this lab.)  Look up the documentation and answer question 1.

    Question 1:

    Which of the methods defined in the Stack class may cause an exception?  What type(s) of exception?

infix and postfix notation

In class, we introduced infix and postfix notation, and showed how they are used by compilers and interpreters in processing arithmetic expressions in programming languages.

    Question 2:

    Translate the following expressions from infix to postfix notation:

    a.  x - y * z

    b.   a * b + c / ( d - e - f )

Expressions as objects

The Calculator program models arithmetic expressions using Java objects.  Two classes, Number and BinaryExpression, are used to represent expressions.  Number represents a simple integer value.  BinaryExpression represents an expression consisting of an operator symbol (e.g., +,*,etc.) and two subexpressions.  In the lab, you'll be writing a Number class and a BinaryExpresion class, both of which implement an Expression interface.

Number will have a constructor

Number(int value);

For example, you can create an Expression object representing the number 362 with the code

Expression e1 = new Number(362);

BinaryExpression will have a constructor

BinaryExpression(Operator op, Expression operand1, Expression operand2);

You can create an Expression object representing "10 * 45" with the code

Expression e2 = new BinaryExpression(new Operator("*"), new Number(10), new Number(45));

More complex expressions can be constructed by making a sequence of calls to the constructors.  For example, you can combine the e1 and e2 defined above with the statement

Expression e3 = new BinaryExpression(new Operator("-"), e1, e2);

so that e3 represents the expression "362 - (10*45)".

    Question 3

    Using the constructors described above, write Java code to create an Expression object that represents the expression

    12 * ( 10 + 20 + 30 )

    Obey the usual operator precedence rules.  The code should not actually perform the indicated arithmetic operations.

Handling parentheses

    Question 4:

    Study the infix-to-postifx algorithm from the class notes.  It handles operator precedence and left-to-right associativity, but not parentheses.  Think about how the algorithm could be modified to handle parentheses correctly.

    a.  What action should be taken when the next token is a left parenthesis?

    b.  What action should be taken when the next token is a right parenthesis?

Error checking

It's possbile that the user of the Calculator could enter an invalid infix expression.  In that case, the Calculator should detect the error and display a message to the user.

    Question 5:

    Describe three ways in which an infix expression could be malformed.  Give an example of each.

     Error #1  

     Error #2  

     Error #3  

handing in your prelab

If you adhered to the honor code in this assignment, add the following statement at the top of your prelab.

I have adhered to the Honor Code in this assignment.

Write or type your solutions and hand in a paper copy by the deadline listed at the top of this prelab.  Late prelabs will not be accepted.