Tuesday, November 20, 2012

Java Calculator

Hi all!
         This is my first time posting to the REGNARTS blog.
So, this post as is rightfully titled, explains java applet through a simple calculator program which takes in 2 numbers, adds, subtracts, multiplies and divides them. Also it checks for possible errors in the input of the 2 numbers and displays the error if any.

PRE-REQUISITES: A little java education along with the concept of classes and a little experience with the eclipse java ide. For those who want to download the eclipse ide, the link's here.

Now lets look at the program,


If you see the first few lines before the commented boundary in green, they are called packages. Packages are collections of  related classes and interfaces(You'll learn about interfaces in an another post maybe. :P ) eg) to develop windows programs, applets, networking etc. The Java API consists of many such packages. Here "awt" means Abstract Window Tools, "applet" includes all those methods (btw functions in java are called methods) which are required to run this Applet, and "awt.event" controls any action performed within the applet window.

Now after the green commented boundary you'll see a line "public class AplNum extends Applet implements ActionListener". Here public class means objects of this class can be created in different files that are in the same folder as this file. If you are using some other editor then its important to name the file same as the name of the public class in the file in the file. Same reason you can have only one public class in one java file. Now the name of a class has to start with a capital letter as per proper java standards. It extends Applet implies it inherits properties and elements from the class Applet. The term applet is used for small java apps that are usually used for web designing. And finally it implements ActionListener implies the Applet runs the methods in ActionListener interface, that is used to detect any action happening inside the applet window. It contains one method which we are supposed to redefine in our code called as the "actionPerformed". Again first letter of any method is small case as per java standards!

Now inside the class we declare the members of the class, that is labels, buttons and textfields. Where label is used to display a msg, button used to click on something, and textfield is used to input and output data. Notice that all label elements have their names prefixed with 'l', buttons with 'b' and textfields with 't'. That is good program organisation for you!
now we discuss the init() function which is used to create the Applet. In this function you initialise the various members you have already created. first you use GridLayout to brand a layout to the applet. GridLayout gives a table like layout. Here it gives 8 row, 2 column table, with 15 pixel horizontal spacing and 10 pixel vertical spacing between the members. setSize is used to set the size of the applet window. The members are then added in order to the table. For a label, we initialise it to a default text supplied in "" inside the brackets. Textfields are initialised to size of text allowed in the textfield. And if that textfield is not used for inputting then we need to disallow editing using setEditable(false). And buttons are initialised to default display text. addActionListener(this) implies action performed on those members are detected by the actionListener interface. Finally add() is used to add the member to the respective record in the layout. Like this all members of the class are intitialised.

Now lets check the next function used to input numbers and check for errors!
Try catching is an error handling mechanism in java. Here if any error occurs in java, that catch statement is executed that has the erroneous object as its parameter. getNos() function returns true if the inputs are correct and returns false if inputs are wrong. Here flag is set to false meaning default assumption is that the input is erroneous. Then inside try{} we use getText() of textfield to get any string entered in that textfield. Then we use Integer.parseInt() to convert the string into number. If the string doesnt just contain numbers then it throws an error. All errors belong to the Exception class. This error is caught and respective steps are taken to handle it. In this case, we set the format using different functions and display the error msg in tmsg and clear up the tnum1 and tnum2 for further input.If all the numbers are entered correctly then tmsg is cleared.

Now that we have safely input the numbers, lets look at the actionPerformed function that I previously said we need to redefine to suit our code.
First it checks for the integrity of the input numbers given. Then it displays the respective results. If you click the add button, the actionListener gets activated and it raises the actionPerformed function and if the source of the action is the add Button, the sum will be displayed and an "Addition successfull" msg is shown. Same thing goes for subtract, multiply and divide with respective solutions and msges. But for division it also checks for divide by zero error!

Now that you've written the project, compile and run it in eclipse and you will get the following applet window:
I've already performed the operations so you'll see all the textfields are full!
Now for those who want to run the applet though command prompt and you already have jdk installed in your system and your classpaths set up you can first create the following html file named AplNum.html:
then run the command: javac AplNum.java to compile the file and give the class file. then run the command: AppletViewer AplNum.html in your command window and poof! you'll get the same applet window!
I seriously hope you people try it out, and please comment you're reviews about the post and the program!
Also, I have a question for you, Which editor have I used to code the above program? Answer to win a chance to acquire some guaranteed goodies! :)
I thank Regnarts for allowing me to post on his blog.
Dude, you are not always awesome but then that's the beauty of it! ;)
Yours,
anty_kryst
Follow me at twitter "@anty_kryst" or on facebook "antykrissh".
Bye!