Fun With Java Seminar
Homework #4: A Java Calculator
Contents:
[Background and Motivation]
[What to Read]
[Directions]
[The Problems]
Due: have parts 1. and 2. of the assignment done for Friday, July 11,
and bring your listings to the seminar to share with others.
Be prepared to discuss what worked and what did not work for you
as you were developing your program.
We also discussed this on July 15.
We'll discuss the design patterns and OO coding part on July 18.
Java is one of the first programming languages to have
a standard windowing framework as a standard part of the language.
In Java, this is called the Abstract Window Toolkit (AWT).
In this homework, you'll exercise the AWT and become more familiar with it.
To massage your skills with the GUI facilities of Java, you will
develop a little desktop calculator - in stages, of course. All
"input" to the calculator will be through the "keys" in the UI you
provide.
See chapters 7 and 10 of Just Java
by van der Linden (Prentice-Hall, 1997).
Another thing to read would be the
Writing Applets chapter of
The Java Tutorial by Mary Campione
and Kathy Walrath (Addison-Wesley),
but see also their
notes on the AWT in JDK 1.1
(as opposed to the 1.0.2 that they usually discuss).
See also the documentation for the packages
java.applet
and
awt
for details.
(The hyperlinks here are for the JDK 1.1.2 documentation.)
This homework, like the others in this seminar,
will be open-ended and somewhat ill-defined.
we'll try to give you some idea of the difficulty/effort involved
in each part.
Do what you have time for.
Bring a printout of your code to class, and be prepared to discuss it
with other people in the class.
- (easy)
Develop a simple calculator (as an application) that just handles
integer arithmetic (+, -, *, /). You will, of course, need to include
an "=" key, along with the 10 digits. Don't worry about anything in
this version except having the calculator behave correctly for
"correct" input. So in this version you do not have to be concerned
with overflow, underflow, zero divide, ... . Just get it so the user
can enter a number on the display, enter an operation, enter another
number, enter "=", and see the result.
- Make your calculator an applet.
- Extend the calculator so the user sees "E" in the display if any
error condition has been encountered. This might occur, for example, if
the user tries to perform an operation and not enough numbers have
been entered. Note that Java handles overflow, underflow, and division
by zero itself, in accord with the IEEE floating point standard.
- Extend the calculator so that it handles "real" numbers. Let's
assume the same sort of semantics we have for mixed-mode arithmentic
expressions that we have in Java.
- Look at the interpreter pattern and other patterns in the Design
Patterns book (which is on reserve). Try to make your code more
OO by using intelligent objects. Pay particular attention to the
idea of the model-view-controller pattern, so that your calculator
engine and its registers are separated from the user interface.
- Let's add a key labelled EXPR to the calculator, - for "expression".
When the user "clicks" on this key, they can enter an expression
"template" that can contain parenthesis,
operators, and "?", to represent where a value would go in the
expression. So, for example, the user could enter the expression:
((? + ?) * ?) - ?
Let's allow the user to store up to 10 such expressions. (You decide
on how this should work for the user.) When the user wants to
evaluate an expression, they are prompted for the values in the
order in which they appear in the expression. When the last value
is entered, the expression is evaluated.
Be clever here. What can you do to help the user "edit" the
expression as it is entered? The user will not be able to "see"
the entire expression in the display - how can we help them
"scroll back and forth" in the expression?
[Back to the Fun with Java Seminar homepage]
Last update: $Date: 1997/07/20 21:37:54 $
Albert Baker