|Seventh Assignment: Calculator
CIT 591, David Matuszek, Fall 2001
Purposes of this assignment:
You are to implement a calculator. This calculator has the following features:
I am providing a
CalculatorChip class which does all the mathematical
operations for you. Your job is to write the GUI for this chip. Download CalculatorChip.java
and use it in your project.
Your GUI should include:
TextField(for displaying results).
F, for the sixteen hexadecimal digits.
Buttons for add, subtract, multiply, divide, and equals.
Buttons to clear the current entry and to clear everything.
Buttons to set the mode to octal, decimal, or hexadecimal.
These buttons should call appropriate methods in a
In addition, you have the following requirements:
Fshould be inactive.
How to get started:
Use BlueJ to create an Applet. You can ignore most of the methods that it creates;
the only one of interest in the
public void init() method.
This is the applet method you will modify to create the GUI. (Of course, you
are welcome to create additional methods.) The applet and its
provide both the controller (the buttons) and the view (the text
init() method, create the
TextField and the
Buttons you will need. Figure out how you will nest containers (
and which layout manager you want to attach to each container. Add all your
components (containers included) to the appropriate containers (your Applet,
this, is the largest container). You might want to look at
Make an instance of the
CalculatorChip class. Look at the methods
it provides, then attach listeners to your buttons. Each listener should call
the appropriate method in the calculator chip, and send the result to the text
field. Notice that the calculator chip, which represents the model, does
not use your controller or your view; it is completely independent of them.
When you attach one listener to multiple buttons, you need to find out which
button caused the listener to be invoked. Remember that the listener gets an
ActionEvent object as a parameter, and it contains the information
you need.. The
ActionEvent class is in the
package, and it has two methods, either of which you can use:
getSource(), inherited from the superclass
EventObject, will return the actual object (such as a button) that caused the event. You can compare this object (using
==) with your own buttons.
getActionCommand()returns the label on the button (as a
I strongly recommend that you get the applet working first, then worry about its appearance. Remember that the AWT is fairly primitive. You can choose your layout managers, but you can't tell them what to do. "Not too ugly" may be the best you can achieve. Don't spend a lot of time on appearance and leave "making it work" to the last minute. Functionality is more important than appearance.
I expect most students will get 100 on this project. If your applet is particularly nice looking or particularly ugly, you might gain or lose a few points. You can earn some extra credit by adding functionality to the calculator--but do not change its essential nature. It must remain integer only, AWT only, and it must support the given bases.