Thursday, May 24, 2007

Hey Friends

Recently I e-mailed a simple basic calculator to our batch mail. (My Last Post). Actually my intention was to build some kind of a community with most of our batch mates. But from the comments I got (Verbally and by e-mails) it says that My idea was wrong. Actually its hard work on as a unit for something like this isn't it folks. any way I should say thank you for the guys and gals who comment on it giving me good advices. And special thanks goes to the guys who criticized me :-) it's fun.Actually I found some people who like to work together building our own software for simple use. That's something to be happy about.

Monday, May 21, 2007

KCalc - A simple Calculator Made by ME

Hey Friends, this is my first java application(at least this) made to do simple calculations. The code is available below and I invite all of u to look at the Kcalc and to do any improvements as u wish . But please be kind enough to put it as a comment so we can continue improvement.

Here is the code__________________

 /*  
 * KCalc.java  
 * A simple calculator  
 * version 0.1 on 27/04/2007  
 * author K. Guniyangoda  
 * e-mail : kalguni@yahoo.com  
 *  
 * Special Thanks to T.Yamazaki  
 * e-mail : T.Yamazaki  
 * The logic for calculation derived from his  
 * Calc applet version 0.0 made on 96/01/19  
 */  
 import java.awt.*;  
 import java.awt.event.*;  
 import javax.swing.*;  
 public class KCalc extends JFrame {  
   private Container container;  
   private GridBagLayout layout;  
   private GridBagConstraints constraints;  
   private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, b_dot,  
       b_minus, b_plus, b_divide, b_mul, b_equal, b_mod, b_clear;  
   private JTextField textField;  
   String string1,string2, Opcode;  
   boolean flag;  
   double value1, value2;  
   /* set up the GUI */  
   public KCalc() {  
     super( "KCalc");     // JFrame constructor; Name the Frame as KCalc  
     // get content pane and set its layout to gridbaglayout  
     container = getContentPane();  
     layout = new GridBagLayout();  
     container.setLayout(layout);  
     //initiate gridbag constraints  
     constraints = new GridBagConstraints();  
     constraints.fill = GridBagConstraints.HORIZONTAL;  
     //adding the text field  
     textField = new JTextField("0");  
     textField.setEditable(true);  
     textField.setHorizontalAlignment(JTextField.RIGHT);  
     constraints.ipady = 20;  
     addComponent(textField, 0, 0, 5, 1, 2, 2, 2, 2);  
     //adding buttons to the panel  
     b7 = new JButton( "7" );  
     addComponent(b7, 0, 1, 1, 1, 10, 2, 2, 2);  
     b8 = new JButton( "8" );  
     addComponent(b8, 1, 1, 1, 1, 10, 2, 2, 2);  
     b9 = new JButton( "9" );  
     addComponent(b9, 2, 1, 1, 1, 10, 2, 2, 7);  
     b4 = new JButton( "4" );  
     addComponent(b4, 0, 2, 1, 1, 2, 2, 2, 2);  
     b5 = new JButton( "5" );  
     addComponent(b5, 1, 2, 1, 1, 2, 2, 2, 2);  
     b6 = new JButton( "6" );  
     addComponent(b6, 2, 2, 1, 1, 2, 2, 2, 7);  
     b1 = new JButton( "1" );  
     addComponent(b1, 0, 3, 1, 1, 2, 2, 2, 2);  
     b2 = new JButton( "2" );  
     addComponent(b2, 1, 3, 1, 1, 2, 2, 2, 2);  
     b3 = new JButton( "3" );  
     addComponent(b3, 2, 3, 1, 1, 2, 2, 2, 7);  
     b0 = new JButton( "0" );  
     addComponent(b0, 0, 4, 1, 1, 2, 2, 2, 2);  
     b_dot = new JButton( "." );  
     addComponent(b_dot, 1, 4, 2, 1, 2, 2, 2, 7);  
     b_divide = new JButton( "/" );  
     addComponent(b_divide, 3, 1, 1, 1, 10, 2, 2, 2);  
     b_clear = new JButton("CL");  
     b_clear.setToolTipText("Clear the Screen!");  
     addComponent(b_clear, 4, 1, 1, 1, 10, 2, 2, 2);  
     b_mod = new JButton( "%" );  
     addComponent(b_mod, 3, 2, 1, 1, 2, 2, 2, 2);  
     b_minus = new JButton( "-" );  
     addComponent(b_minus, 4, 2, 1, 1, 2, 2, 2, 2);  
     b_mul = new JButton( "*" );  
     addComponent(b_mul, 3, 3, 1, 1, 2, 2, 2, 2);  
     b_plus = new JButton( "+" );  
     addComponent(b_plus, 4, 3, 1, 1, 2, 2, 2, 2);  
     b_equal = new JButton( "=" );  
     b_equal.setToolTipText("Gives the result !");  
     addComponent(b_equal, 3, 4, 2, 1, 2, 2, 2, 2);  
     // register event handlers  
     ButtonHandler handler = new ButtonHandler();  
     textField.addActionListener( handler );  
     b7.addActionListener( handler );  
     b8.addActionListener( handler );  
     b9.addActionListener( handler );  
     b4.addActionListener( handler );  
     b5.addActionListener( handler );  
     b6.addActionListener( handler );  
     b1.addActionListener( handler );  
     b2.addActionListener( handler );  
     b3.addActionListener( handler );  
     b0.addActionListener( handler );  
     b_dot.addActionListener( handler );  
     b_divide.addActionListener( handler );  
     b_clear.addActionListener( handler );  
     b_mod.addActionListener( handler );  
     b_minus.addActionListener( handler );  
     b_mul.addActionListener( handler );  
     b_plus.addActionListener( handler );  
     b_equal.addActionListener( handler );  
     //settings the parameters for the JFrame  
     setLocation(360,150);  
     setSize( 300, 350 );  
     setResizable(false);  
     setVisible( true );  
     flag= true;   // use to determine whether to add the value to current or a new value  
     value1 = 0.0d ;    //  
     value2 = 0.0d ;  
     Opcode = "" ;  
   } //end constructor KCalc  
   //method to set constraints on  
   private void addComponent(Component component,  
       int column, int row, int width, int height,  
       int top, int bottom, int left, int right){  
     //set gridx and gridy  
     constraints.gridx = column;  
     constraints.gridy = row;  
     // set gridwidth and gridheight  
     constraints.gridwidth = width;  
     constraints.gridheight = height;  
     //set the space around a component  
     constraints.insets = new Insets(top, bottom, left, right);  
     // set constraints and add component  
     layout.setConstraints( component, constraints );  
     container.add( component );  
   }  
   //method to perform the calculations  
   private double Calculation(String Opcode, double value1, double value2) {  
     if ("+".equals(Opcode)) value1 = value1 + value2;  
     else if ("-".equals(Opcode)) value1 = value1 - value2;  
     else if ("*".equals(Opcode)) value1 = value1 * value2;  
     else if ("/".equals(Opcode)) value1 = value1 / value2;  
     else if ("%".equals(Opcode)) value1 = value1 % value2;  
     else value1 = value2;  
     return value1;  
   }  
   public static void main( String args[] ) {  
     KCalc application = new KCalc();  
     application.setDefaultCloseOperation(  
         JFrame.EXIT_ON_CLOSE );  
   }  
   // inner class for button event handling  
   private class ButtonHandler implements ActionListener {  
     // handle button event  
     public void actionPerformed( ActionEvent event ) {  
       // set the textField to 0 when The CL button Pressed  
       if( event.getSource() == b_clear ){  
         value1 = 0.0d ;  
         value2 = 0.0d ;  
         Opcode = "";  
         flag = true;  
         textField.setText("0");  
       }  
       // Action when a numeric button and "." been pressed.  
       else  
         if ((event.getSource() == b0) | (event.getSource() == b1) | (event.getSource() == b2)  
         | (event.getSource() == b3) | (event.getSource() == b4) | (event.getSource() == b5)  
         | (event.getSource() == b6) | (event.getSource() == b7) | (event.getSource() == b8)  
         | (event.getSource() == b9) | (event.getSource() == b_dot)) {  
         if (flag)  
           string2 = event.getActionCommand();  
         else  
           string2 = textField.getText() + event.getActionCommand() ;  
         textField.setText(string2);  
         flag = false;  
         }  
       // Code for handling operation buttons  
         else  
           if ((event.getSource() == b_plus) | (event.getSource() == b_minus)  
           | (event.getSource() == b_mul) | (event.getSource() == b_divide)  
           | (event.getSource() == b_mod)|(event.getSource() == b_equal)) {  
         string1 = textField.getText();         // get the current available value in textField to string 1  
         value2 = Double.parseDouble(string1);      // get the string1's value as a double to value2  
         value1 = Calculation(Opcode, value1, value2);  // in first run it only pass the value2 value to the Calculation method  
         Double vTemp = new Double(value1);       // in first run the Calculation method will return value2 as dreg 1  
                                 // and here it will assign to vTemp  
         string2 = vTemp.toString();           // get the value of vTemp to string2  
         textField.setText(string2);           // set the text field according to vTemp  
         Opcode = event.getActionCommand();   // get the operation which happend  
         flag = true;                  // set the flag to true so the program waits for a new set of values  
           }  
     }  
   } // end private inner class ButtonHandler  
 }//end the class KCalc