Monday, September 24, 2007

Calculator How-To Using Java 2 Part 1

Ok. Here is a little guide for those who wanna make a calculator using java 2 for whatever reasons. My guide is broken up as steps. Each step is crucial, please follow them carefully.


1. First draft out your gui layout.

Illustrated Layout below


Note: Of course you can always come out with our own fancy design.


In the above layout, you have the basic digit buttons and the 4 operator buttons( + - X* / ).
And yeah, never forget your decimal operator and your = operator. These are few neccessary buttons that you will need to make any kind of calculators.


2. Using Eclipse's Visual Editor to create your layout.
If you do not have Eclipse, download the latest version here.
Note: If you have no clue of how to use the Visual Editor. A tutorial can be found here.
It is always good to google it around to find what you need.


3. Get your buttons working.
In order for your buttons to be functional, You will need Action Listener.
A simple tutorial on this topic can be found here.


4. How a real calculator works (Implementation of Stack)
Illustration below


This is how a calculator works. When a digit button is pressed on the calculator, the value of the button goes into the stack. For operation 5 + 2, the value will enter the stack first then follow by 2. When the + operation is encountered, the 2 values ( 5 and 2 ) will be popped out from the stack. The result of 5 + 2 is then computed and pushed back into the stack.

I shall end my little guide here. The 4 steps might be short, but it can be quite a lot for you to digest. Just take your time to understand.

I will release the source code in the next coming guide.

Posted by Zack at 10:45 PM