The goal of this project is to put the state pattern to use.
You will be writing one last calculator program for this class. This time we'll be back to a normal calculator, as in your first stab. This time, however, you ought to be able to do a much better job of designing your code. In particular, you will be incorporating what we have learned about the state pattern in your design of the calculator.
In this project you will use the same GUI as last time. So you will need to again clone the starting repository for project 2:
hg clone /cslab/class/csci245/project2 project5
Now you'll be operating under the following rules:
Here are the requirements from project 2:
Make this program work just like a hand-held four-function calculator. You can find a decent example for reference here. Don't try to implement anything fancy; don't use Ubuntu's built-in calculator as a reference.
ConcreteCalculatorFace
has a constructor that
takes no parameters (as used in the example above).
A window automatically appears.
I've worked on the window so that it appears nicely on the machines in the
lab.
If you work on this project on your own computer, the window's size might not be right
(it isn't, for example, on my Macintosh).
You can either adjust it by mouse whenever you run the program, or
you can play around with the setSize()
and setLocation()
methods of CalculatorFace
.
If you do, please remove these adjustments before turning in your code,
since your code will be graded in the lab.
If you do all your work in the lab, you will not need to use these methods.
In this project, you want your program to work like a real-life hand-held calculator. This is actually pretty hard to do. Even the best students rarely get full marks on the first try of this project. (Calculators will be a recurring theme in the projects of this course.)
Specifically, here are some expectations for your calculator:
3 + 5 = + 8 + 2 =
,
then the displayed result will be 18 (after the first = is pressed,
it will display 8; after the third + is pressed, it will display 16).
If, on the other hand, the user keys in 3 + 5 = 8 + 2 =
, the
final displayed result will be 10.
C
should reset the calculator
at any time.
1 / 9
,
the answer should be rounded so that it fits in the calculator's
screen.
Important: The "screen" on the calculator is only 15
characters wide.
Java displays double
numbers using more than 15 characters.
Make sure that when you display results that you do not run off the end
of the screen.
You need to think about how to format your results.
There are several ways to do this.
One way is to use the DecimalFormat
class described starting on pages 72-76 in Absolute Java.
You also can devise your own formating strategy using String
manipulation.
Turn in your code by changing into your directory (eg,
project5
). Make sure that you have everything committed
to your repository. Then turn it in with
/cslab/class/csci245/bin/handin project5 .hg calc/*.java
DUE: Friday, May 3, 5:00 pm.