Java Calculator

This is a discussion on Java Calculator within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Hi every one i just started my java class.Its been a week since the class and since i have some ...

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    Java Calculator

    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
    Code:
    /*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
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Now try writing this program using the Swing API!!!
    Mr. C: Author and Instructor

  3. #3
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    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.!!!!
    Last edited by MethodMan; 11-11-2002 at 11:13 PM.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  4. #4
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    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 ...

    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 ...
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  5. #5
    Banned Cgawd's Avatar
    Join Date
    Oct 2002
    Posts
    405
    ****** i cant take this world with dummies ...
    look whos talking, you made 11 typos, maybe you should check yourself before you criticize others...

  6. #6
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    Originally posted by Cgawd
    look whos talking, you made 11 typos, maybe you should check yourself before you criticize others...
    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
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  7. #7
    Shadow12345
    Guest
    Machod pahen ke chuut = I spell like a chipmunk

  8. #8
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    Originally posted by Shadow12345
    Machod pahen ke chuut = I spell like a chipmunk
    lol hahah you do ??? lol
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C#, Java, C++
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-05-2004, 03:06 PM
  2. The Java language is being expanded
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 06-11-2004, 10:07 PM
  3. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  4. Java woes
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 07-06-2003, 01:37 AM
  5. How to use Java with C++
    By Arrow Mk 84 in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2003, 04:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21