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.
If you need documentation on the GUI classes, you can reach the full Javadoc as well as abbreviated Javadoc for the GUI classes from the class page. Chapter 17 in Savitch may also prove helpful.
After moving into your directory for this course, clone the repository for this project to get a copy of the files:
hg clone /cslab/class/csci245/project2
That should make two levels of directories: project2
,
containing calc
, which contains the following files:
CalculatorFace.java ConcreteCalculatorFace.java Driver.javaThis structure is set up to fit better with Eclipse. If you use Eclipse, create a new Java project, uncheck "Use default location" and fill in the Location with the
project2
directory. If you
want to compile/run from the command line, you should do so from that
directory, with one of
javac calc/*.javaor
java calc/Driver
CalculatorFace
is an interface that defines
the method signatures for the object representing the
calculator window.
ConcreteCalculatorFace
is a class implementing
CalculatorFace
---you do not need ever to look
at thie class (the only reason I'm even giving you the source code
is because Eclipse doesn't play nice with only the classfiles;
the easiest thing was to just give you the source).
ConcreteCalculatorFace
,
you may notice a files ConcreteCalculatorFace$1.class
in the folder.
It is a class that
ConcreteCalculatorFace
makes use of. You don't need to
know anything about it.
Driver
is a short program that shows you what
the window looks like.
Compile and run it, and you will see the calculator window;
it doesn't do anything yet, though.
You may choose to write your project
using this program as a starting point, but that's not necessary.
You don't need to use this file at all.
Make a new project in Eclipse; make it from existing source,
specifically from the folder containing the calc
folder.
As you can see from inspecting the code in
CalculatorFace.java
, the only interaction
with the window available to use is
You may either get the buttons all at once by means of an iterator
(for example, if you want to store them yourself somehow)
or can look them up by calling getButton
with the character that appears on the button.
I have hardwired the "plus or minus" character into
the static variable PLUS_MINUS
of CalculatorFace
.
(Interfaces may have public, static, final variables, but no
other kinds of variables.)
(The buttons do not write to the screen by default; they do nothing without you writing something for it.)
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
CalculatorFace face = new ConcreteCalculatorFact(); JButton plusButton = face.getButton('+'); ActionListener plusListener = new PlusListener(); plusButton.addActionListener(plusListener);
Or, more succinctly:
face.getButton('+').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.
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.
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 described starting on pages 72-76 in Absolute Java.
You also can devise your own formating strategy using String
manipulation.
Be sure that you have committed all of your updates to the Mercurial repository. Then, from your project2 directory, do
/cslab/class/csci245/bin/handin project2 .hg calc/*.java
DUE: Wednesday, Feb 27, 2012, at 5:00 pm.