|
Hour 24: Making Your Knowledge Add Up
A SOLUTION TO THE MINICALC PROJECT
- One solution to the calculator assignment is contained in
MiniCalc.java, MiniCalc.class,
CalcApplet.java,
CalcApplet.class and
CalcApplet.html.
- This project uses a
MiniCalc component that can
be added to applets or applications as needed. The
CalcApplet files contain a simple applet that's used
to display MiniCalc as part of the applet's
interface. This is handled by making MiniCalc a
subclass of JPanel , the most basic Swing
container.
- The
MiniCalc class contains three instance
variables, which are defined in the following statements:
char operator = 0;
float storedValue = 0;
boolean clearNext = false;
The operator variable is used to keep track of the
current mathematical operation: addition, subtraction,
multiplication, or division. It does this by storing the text of
the key that was pressed: '+', '-', '*', or '/'. The
storedvalue variable is used to keep track of the
first number involved in an operation (if you pressed the '5',
'+', and '7' keys, storedvalue would equal 5 when the
equals key was pressed. The clearNext variable is
used to indicate when the display should be cleared after the next
key is pressed. This is needed after every operation so that a new
calculation can be performed.
- No other variables are needed for the class itself. The class
stores the current calculation and the final result in the text
field at the top of the interface.
- If you have questions about this solution, contact
the author. I hope you'll publish the source code to your own
solution for this project, so I can offer a link to it on this
site. I look forward to seeing what readers are able to
create.
READER QUESTIONS
|