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
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
Which of the methods defined in the Stack class may cause an
exception? What type(s) of exception?
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.
Translate the following expressions from infix to postfix
a. x - y * z
b. a * b + c / ( d - e - f )
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
Number will have a constructor
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,
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,
so that e3 represents the expression "362 - (10*45)".
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.
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.
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.