Who Knows All.. ?

If the answer is No one. Then this blog is for U. I'm going to put all the things I know in Java

Java Calculator– My first App

This is the first GUI I have ever created. This calculator only works on single digit, and only performs single operation. Your comments are most welcome and appreciated.

Figure

Click on the image above to see calculator image ,due to some reason image is not appearing.

Source Code:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
* <APPLET code = AGCalculator width = 200 height = 200>
</APPLET>
*/
public class AGCalculator extends Applet implements ActionListener
{
static int n;
Button plus, minus, mul,div,equal;
Button[] button = new Button[15];
TextField result = new TextField(10);
String str,msg;
double num1 = -1, num2 = -1,num3;
char chOperator = ‘$’,ch;
public void init()
{
GridLayout gl = new GridLayout(4,4,4,4);
setLayout(gl);
setFont(new Font(“Verdana”,Font.BOLD,24));
add(result);
for(int i = 0; i<10; i++)
{
button[i] = new Button(“”+i);
add(button[i]);
button[i].addActionListener(this);
}
plus = new Button(“+”);
minus = new Button(“-“);
mul = new Button(“*”);
div = new Button(“/”);
equal = new Button(“=”);
add(plus);
add(minus);
add(mul);
add(div);
add(equal);
plus.addActionListener(this);
minus.addActionListener(this);
result.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
equal.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
str = ae.getActionCommand();
ch = str.charAt(0);
if(Character.isDigit(ch))
{
if (num1 == -1)
{
num1 = Integer.parseInt(str);
}
else
{
num2 = Integer.parseInt(str);
}
result.setText(str);
}
else if(str == “+”)
{
chOperator = str.charAt(0);
result.setText(str);
}
else if(str == “-“)
{
chOperator = str.charAt(0);
result.setText(str);
}
else if(str == “*”)
{
chOperator = str.charAt(0);
result.setText(str);
}
else if(str == “/”)
{
chOperator = str.charAt(0);
result.setText(str);
}
else if(str == “=”)
{
switch (chOperator)
{
case ‘+’ :
num3 = num1 + num2;
result.setText(“”+num1+”+”+num2+”=”+num3);
break;
case ‘-‘ :
num3 = num1 – num2;
result.setText(“”+num1+”-“+num2+”=”+num3);
break;
case ‘*’ :
num3 = num1 * num2;
result.setText(“”+num1+”*”+num2+”=”+num3);
break;
case ‘/’ :
num3 = num1 / num2;
result.setText(“”+num1+”/”+num2+”=”+num3);
break;
}
chOperator = ‘$’;
num1 = -1;
num2 = -1;
}
}
}

Advertisements

6 comments on “Java Calculator– My first App

  1. krishna
    June 29, 2012

    Excellent Agesh..

  2. Pavan Somu
    July 6, 2012

    Kewl 🙂

  3. Leonard Marks
    July 6, 2012

    great post

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Information

This entry was posted on June 29, 2012 by in Uncategorized and tagged , , , , , , , .

Browse blog by date

June 2012
M T W T F S S
« Dec   Aug »
 123
45678910
11121314151617
18192021222324
252627282930  

Blog Stats

  • 1,105 hits
%d bloggers like this: