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, checkout
the code for project 1
(a proj1
directory will be created automatically).
svn checkout file:///cslab/class/cs245/projects/proj2
You should get the following files:
CalculatorFace.java ConcreteCalculatorFace.class ConcreteCalculatorFace$1.class
CalculatorFace
is an interface that defines
the method signatures for the object representing the
calculator window.
ConcreteCalculatorFace
is a class implementing
CalculatorFace
(I am not giving you the source code).
ConcreteCalculatorFace$1
is another class that
ConcreteCalculatorFace
makes use of. You don't need to
know anything about it.
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.
(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), look at the Java API,
and look at the code given in lab 3 for the version control system.
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, like we did in lab 3.
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
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. I don't think I've ever had a student get full marks on the first try of this project. (Calculators will be a recurring theme in the projects of this course.)
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 page 73 in Absolute Java.
You also can devise your own formating strategy using String
manipulation.
Don't submit your program using svn (you shouldn't be able to anyway if you try). Instead, copy all the files you made or modified to a turn-in directory I've made for you.
cp
filename
/cslab/class/cs245/turnin/proj2
{elizabeth,ben,neile,tim,hudson,stephen}
DUE: Fri, Feb 15, 5:00 pm.