PDA

View Full Version : Java Calculator



datainjector
11-11-2002, 06:28 PM
Hi every one i just started my java class.Its been a week since the class and since i have some programming knowlage with C it want a big deal to creat it ..All my friends were impresed coz we havent even started AWT programming (win API) and i just made this here is the code ...Save it as
Calculator.java
if u have java compiler installed goto your command prompt and type javac Calculator.java
then java Calculator


/*Calculator in java*/
import java.awt.*;
import java.util.*;
import java.awt.event.*;

public class Calculator extends Frame implements ActionListener
{
public Button btn[];
public String numstr[] = {"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+","CE"};
public TextField tf = new TextField();
Panel p2 = new Panel (new GridLayout(5,4,2,2));
String st, store ;
String str[] = new String[3];
String storestr;
float num[] = new float[2];
int sum;
int cnt=0;
StringTokenizer Tok;

public Calculator()
{
super("Calculator");
st = new String();
add("North",tf);
add("Center",p2);
p2.setBackground(Color.gray);

btn = new Button[numstr.length];
for ( int i=0; i < numstr.length; i++ ){
btn[ i ] = new Button (numstr[ i ]);
btn[ i ].addActionListener( this );
p2.add(btn[i]);

}
btn[16].setForeground(Color.red);
btn[0].setForeground(Color.blue);
btn[1].setForeground(Color.blue);
btn[2].setForeground(Color.blue);
btn[4].setForeground(Color.blue);
btn[5].setForeground(Color.blue);
btn[6].setForeground(Color.blue);
btn[8].setForeground(Color.blue);
btn[9].setForeground(Color.blue);
btn[10].setForeground(Color.blue);
btn[12].setForeground(Color.blue);

}

public void actionPerformed( ActionEvent ae )
{


store = ae.getActionCommand();
if ( store.equals("=") ){
storestr = tf.getText();
Tok= new StringTokenizer(storestr, "*+/-", true);
while( Tok.hasMoreTokens() ){
str[cnt] = Tok.nextToken();
cnt++;
}
num[0] = Float.parseFloat( str[0] );
num[1] = Float.parseFloat( str[2] );

if ( str[1].equals("*") ){
sum = (int)(num[0] * num[1]);
tf.setText(String.valueOf(sum));
}
else if ( str[1].equals("+") ){
sum =(int) (num[0] + num[1]);
tf.setText(String.valueOf(sum));
}
else if ( str[1].equals("-") ){
sum =(int)( num[0] - num[1]);
tf.setText(String.valueOf(sum));
}
else if ( str[1].equals("/") ){
sum = (int)(num[0] / num[1]);
tf.setText(String.valueOf(sum));
}

}
else if ( store.equals("CE") )
{
st = "";
store = "";
cnt = 0;
tf.setText("0");
}
else{
st += store;
tf.setText(st);
}


}

public static void main(String args[])
{
Calculator f = new Calculator();
f.setSize(200,200);
f.show();

}
}


Peace

Mister C
11-11-2002, 10:32 PM
Now try writing this program using the Swing API!!!

MethodMan
11-11-2002, 11:10 PM
I dont know why you would want to show off your java code at a C board but anywho.

A few errors:
I couldnt close it using the X

8/9 returned 0, when it should return 0.8888888888888889, according to my windows calculator

2.2 + 3 = 5, so decimal addition doesnt work either

I just tried a few things and got these errors, who knows, there could be more.!!!!

datainjector
11-12-2002, 03:13 PM
Originally posted by MethodMan
I dont know why you would want to show off your java code at a C board but anywho.

A few errors:
I couldnt close it using the X

8/9 returned 0, when it should return 0.8888888888888889, according to my windows calculator

2.2 + 3 = 5, so decimal addition doesnt work either

I just tried a few things and got these errors, who knows, there could be more.!!!!

Showoff hummm...well that interesting ...very interesting ... what does that have to mean isnt this a general discuttion board ... ****** i cant take this world with dummies ... :mad:

well yeas i dint introduce float calculation so thats whi it game u zero thas not an error .. click x and it does not close ....not my fault i havent done windowsevent listener .....

Mister C i will try swing but at the movement we are just using awt plain ...

Cgawd
11-12-2002, 04:53 PM
****** i cant take this world with dummies ...
look whos talking, you made 11 typos, maybe you should check yourself before you criticize others...:o

datainjector
11-12-2002, 05:44 PM
Originally posted by Cgawd
look whos talking, you made 11 typos, maybe you should check yourself before you criticize others...:o

Machod pahen ke chuut ...do u know what it means ..well english is not my first lang and if a person makes a typo that does not make him a dummy or does it wize chuut:eek:

Shadow12345
11-12-2002, 07:04 PM
Machod pahen ke chuut = I spell like a chipmunk

datainjector
11-12-2002, 08:39 PM
Originally posted by Shadow12345
Machod pahen ke chuut = I spell like a chipmunk

lol hahah you do ??? lol