The goal of this project is to review the basics of object-oriented programming and to give you an initial try at system design.
In this project you will make a program that emulates a 4-function calculator. This program will use a GUI; a window looking like a calculator will appear on the screen, and the user can manipulate it by clicking on the buttons.
I have made the "face" of the calculator already-- the GUI is given to you. You need to write the rest of the program, which will interact with the window.
After moving into your directory for this course, copy the given files for this project:
cp -r ~tvandrun/Public/cs245/proj2/calc .
You should get a folder calc
the following files:
CalculatorFace.java PlainCalculatorFace.java NoSuchButtonException.java SetUp.java
CalculatorFace
is an interface that defines
the method signatures for an object representing the
calculator window.
In particular this contains everything you need to know to
interact with the calculator window.
PlainCalculatorFace
is a class implementing
CalculatorFace
, that is, it makes/models the window.
You do not need to modify this class.
In fact, you do not need even to look at this class.
Again, the interface CalculatorFace
gives you everything you
need to know to interact with this class or any other class that
implements the interface.
When we grade the project submissions we will not use
PlainCalculatorFace
.
We will use another class that implements the interface.
If you work on this project on your own computer instead of the lab
machines, then there is one change you might make
to PlainCalculatorFace
so that
the window will appear correctly on another platform;
see the documentation in the file for details.
PlainCalculatorFace
,
you may notice a file PlainCalculatorFace$1.class
in the folder.
It is a class that
PlainCalculatorFace
makes use of. You don't need to
know anything about it.
NoSuchButtonException
is an exception class
that will be thrown if your code uses the calculator face incorrectly.
SetUp
is a short program that shows you what
the window looks like.
When you compile and run it, and you will see the calculator window;
it doesn't do anything yet (besides display a greeting).
You will modify the method setUpCalculator()
;
details below.
Make a new project in Eclipse; make it from existing source,
specifically from the folder containing the calc
folder.
If you're not confident in starting projects in Eclipse, ask for help.
Inspect the signatures in
CalculatorFace.java
.
You will see that the only
way you can use the window is to do one of two things:
Run SetUp
to see what the
calculator window looks like.
Note that the buttons do not do anything out of the box.
They will not do anything unless you attach action listeners to them.
You will have to write action listeners for the buttons.
If you have never used the ActionListener
interface
(or other GUI components), you can read the relevant sections in the
textbook (from chapter 17, particularly chapter 17.2), and look at the Java API.
Here's a quick review/overview:
An action listener is an object that is "attached" to a GUI component--
for example, a button.
Action listener classes implement the interface
ActionListener
in the package awt.event
,
which has the method actionPerformed(ActionEvent ae)
.
For our purposes, you can ignore the parameter (but don't let that stop
you from finding out how to use it, if you're curious).
When the button is pressed by the user, the method actionPerformed()
is invoked.
There are two basic strategies for using action listeners, and the
choice between them is up to you (as we go on this semester, we'll discuss
some of the trade-offs in a decision like this):
Either you can write very busy actionPerformed()
methods,
which do a lot of the actual work of the calculator themselves;
or you can write simple actionPerformed()
methods
which merely call another method in a central object.
So, if we suppose you have written a class PlusListener
that implements ActionListener
and
handles the pressing of the plus button, you can attach an object to the button
by
face.addActionListener('+', new PlusListener());
Don't hesitate to ask for help if you would like a fuller explanation or some guidance.
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.
I've worked on PlainCalculatorFace
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 right, for example, on my Macbook).
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
PlainCalculatorFace.java
.
Your code should be dependent only on the CalculatorFace
interface.
Getting your calculator to work perfectly right 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.
Finally, as has been mentioned in class, this is a point where many students realize they never understood what the static parts of a class were all about---which usually stems from having not yet mastered the concepts of class and instance. So if something is wrong and Eclipse suggests you make some variable static, don't do it. Something else is wrong; ask for help if need be. Specifically, any static variable must include in its documentation an explanation why that variable is static.
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)/proj2
To turn every file in whatever directory you are currently in, do
cp * /cslab.all/ubuntu/cs245/turnin/(your user id)/proj2
DUE: 5:00 pm Wednesday, Feb 25, 2015, 5:00 pm. Note that this is also the duedate of Project 3.