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. But this time you will be encorporating what we learned about the state pattern in your design of the calculator.
In this project you will use the same GUI and other starter code as the other calculator projects.
cp -r ~tvandrun/Public/cs245/proj2/* .
Now you'll be operating under the following rules:
(What's a "degenerate loop"? Something like this:
// I want to check is x < 3 but I can't use an if. // I'll use a while loop instead. I'm so clever... while (x < 3) { System.out.println("Now I know that x is less than three!!!"); break; // ok, break out of this "loop" }
Generally, the point of all this is not to find sneaky ways to build the equivalent of ifs but to use polymorphism.)
Here are the requirements from project 2, repeated here verbatim:
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
variables WINDOW_Y_POSITION
etc at the beginning
of the class ConcreteCalculator
.
Repeat: This is the only reason you would have to modify the file
ConcreteCalculator.java
.
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.
Hint: Don't try to write this entire project at once. Start implementing the number buttons first, making numbers appear on the screen. Then implement the plus button. When that's working, start doing the other operator buttons.
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;
it's described in Absolute Java (check the index, pages
differ with edition) or
the
Java API.
You also can devise your own formating strategy using String
manipulation.
Also, be sure your code is documented according to the specifications given in the style principles.
setup()
(Largely repeated from project 5)
To clarify one part of the framework for this and
other calculator projects, please note that when your submission
is graded, your setUpCalculator()
method will be
called, and it will be given an object implementing the
CalculatorFace
interface you are given, but it
will not be an instance of the ConcreteCalculatorFace
class.
ConcreteCalculatorFace
is given for your own testing
purposes.
That is why your code should not be dependent on that class,
only on the interface.
Also, the main method of SetUp
will not be called,
only the setUpCalculator()
method.
In short, the contract you need to fulfill is
WhensetUpCalculator()
is given an object implementing the interfaceCalculatorFace
, it will instantiate and attach action listeners to the given object (and instantiate any other appopriate classes, ie the "brain" or whatever you call it) so that the given object will behave like a calculator.
Please turn in all the files you wrote or modified (ie, the files you didn't modify don't need to be turned in) to a turn-in directory. To turn in a specific file, do
cp (some file) /cslab.all/ubuntu/cs245/turnin/(your user id)/proj8
DUE: Wednesday, April 29, 5:00 pm. Note that if still have your two late days, then this makes Friday, Dec 12 (the last day of classes) the effective due date. Moreover, this project overlaps with project 9 (which will be a short project); project 9 will be due May 1, and no late days may be applied to project 9.