Showing posts with label calculator using C# or Java. Show all posts
Showing posts with label calculator using C# or Java. Show all posts

Sunday, November 4, 2012

Simple calculator using Java Applet and Event Handling

You can see all event methods in java.awt.Event class
and java.awt.event.ActionListener is an Interface that includes method named actionPerformed and takes ActionEvent as an argument.
Type javap java.awt.event.ActionListener in Command Prompt.


Step 1:Create a java source file with name calc.java that is name of java class and it is mandatory that class-name and file-name must be same.

Applet class must be public.

In java source file commented applet code will not be compile by java compiler but will executed by AppletViewer.

In command addActionListener(this) "this" keyword refers as object of current class. we can also write "new calc()" at place of "this" keyword.For making any TextField ReadOnly we have to use method TextField().setEnabled(false)
 import java.awt.*;  
 import java.applet.*;  
 import java.awt.event.*;  
 //<applet code="calc.class" width=150 height=300></applet>  
 public class calc extends Applet implements ActionListener  
 {  
 Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,ba,bs,br,bc;  
 TextField t1,t2,t3;  
 Label l1,l2,l3;  
 String m="";  
 int i,a;  
      public void init()  
      {  
      l1=new Label("Enter No.");  
      add(l1);  
      t1=new TextField(15);  
      add(t1);  
      b1=new Button("1");  
      b2=new Button("2");  
      b3=new Button("3");  
      b4=new Button("4");  
      b5=new Button("5");  
      b6=new Button("6");  
      b7=new Button("7");  
      b8=new Button("8");  
      b9=new Button("9");  
      b0=new Button("0");  
      ba=new Button("Add");  
      bs=new Button("Sub");  
      br=new Button("Result");  
      bc=new Button("Clear");  
      add(b1);  
      add(b2);  
      add(b3);  
      add(b4);  
      add(b5);  
      add(b6);  
      add(b7);  
      add(b8);  
      add(b9);  
      add(b0);  
      add(ba);  
      add(bs);  
      add(br);  
      add(bc);  
      b1.addActionListener(this);  
      b2.addActionListener(this);  
      b3.addActionListener(this);  
      b4.addActionListener(this);  
      b5.addActionListener(this);  
      b6.addActionListener(this);  
      b7.addActionListener(this);  
      b8.addActionListener(this);  
      b9.addActionListener(this);  
      b0.addActionListener(this);  
      ba.addActionListener(this);  
      bs.addActionListener(this);  
      br.addActionListener(this);  
      bc.addActionListener(this);  
      l2=new Label("Result");  
      add(l2);  
      t2=new TextField(15);  
      add(t2);  
      t2.setEnabled(false);  
      l3=new Label("Exception");  
      add(l3);  
      t3=new TextField(40);  
      add(t3);  
      t3.setEnabled(false);  
      }  
      public void actionPerformed(ActionEvent e)  
      {  
      try{  
      if(e.getActionCommand().equals("1"))  
           {     m+="1";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("2"))  
           {     m+="2";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("3"))  
           {     m+="3";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("4"))  
           {     m+="4";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("5"))  
           {     m+="5";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("6"))  
           {     m+="6";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("7"))  
           {     m+="7";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("8"))  
           {     m+="8";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("9"))  
           {     m+="9";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("0"))  
           {     m+="0";  
                t1.setText(m);}  
      if(e.getActionCommand().equals("Add"))  
           {i=Integer.parseInt(t1.getText());       
                t1.setText("");  
                m="";  
                a=1;}  
      if(e.getActionCommand().equals("Sub"))  
           {i=Integer.parseInt(t1.getText());       
                t1.setText("");  
                m="";  
                a=2;}  
      if(e.getActionCommand().equals("Result"))  
           {     if(a==1)  
                {  
                int j=Integer.parseInt(t1.getText());  
                int k=i+j;  
                Integer z=new Integer(k);  
                String s=z.toString();  
                t2.setText(s);       
                t1.setText("");  
                m="";  
                }  
                else if(a==2)  
                {                 
                int j=Integer.parseInt(t1.getText());  
                int k=i-j;  
                Integer z=new Integer(k);  
                String s=z.toString();  
                t2.setText(s);       
                t1.setText("");  
                m="";  
                }  
                else  
                {}  
           }  
      if(e.getActionCommand().equals("Clear"))  
           {       
                t1.setText("");  
                t2.setText("");  
                t3.setText("");}  
      }  
      catch(Exception e1){  
      String s=e1.toString();  
      t3.setText(s);}  
      }  
 }                  

Step 2: Run cmd(command prompt) and compile java source file, calc.class file'll be build.

then give appletviewer command and applet will pop out.


output:


Download Code Link 1 (3 kb)
Download Code Link 2 (3 kb)













Also Read Simple calculator using Windows form Application with C# in VS2008

Wednesday, March 16, 2011

Simple calculator using Windows form Application with C# in VS2008



File->new->project
the sceenshot given below will be shown and select windows form Appliction
snapshot of form that i have built is shown below


Double click on form1(Design view) to open form1.cs(Source view)

form1.cs

 using System;  
 using System.Collections.Generic;  
 using System.ComponentModel;  
 using System.Data;  
 using System.Drawing;  
 using System.Linq;  
 using System.Text;  
 using System.Windows.Forms;  
 namespace Calculator  
 {  
   public partial class Form1 : Form  
   {  
     public Form1()  
     {  
       InitializeComponent();  
     }  
     float val, val1,val2,val3,val4,answer;  
     int ch,a,b,c,d;  
     bool flag;  
    //Textbox1 is 1st textbox shown in above form for entering value  
    //Textbox2 is 2nd textbox shown in above form ,only used for display result   
     private void button1_Click(object sender, EventArgs e)  
     {  
       textBox1.Text += "1";  
     }  
     private void button2_Click(object sender, EventArgs e)  
     {  
       textBox1.Text +="2";  
     }  
     private void button3_Click(object sender, EventArgs e)  
     {  
       textBox1.Text += "3";  
     }  
     private void button4_Click(object sender, EventArgs e)  
     {  
       textBox1.Text += "4";  
     }  
      private void button5_Click(object sender, EventArgs e)  
     {  
       textBox1.Text += "5";  
     }  
     private void button6_Click(object sender, EventArgs e)  
     {  
       textBox1.Text += "6";  
     }  
     private void button7_Click(object sender, EventArgs e)  
     {  
       textBox1.Text += "7";  
     }  
     private void button8_Click(object sender, EventArgs e)  
     {  
       textBox1.Text += "8";  
     }  
     private void button9_Click(object sender, EventArgs e)  
     {  
       textBox1.Text += "9";  
     }  
     private void button10_Click(object sender, EventArgs e)  
     {  
       textBox1.Text += "0";  
     }  
     private void button12_Click(object sender, EventArgs e)    //ADDITION  
     {  
       ch = 1;  
       if (a == 0)  
       {  
         flag = float.TryParse(textBox1.Text, out val);  
         if (flag == false)  
         {  
           MessageBox.Show("Invaid Value Entered", "Syntax Error Message");  
           textBox1.Focus();  
         }  
         textBox1.Clear();  
         textBox1.Focus();  
         a = 1;  
         b = 2;  
         c = 2;  
         d = 2;  
       }  
       else if (a == 1)  
       {  
         flag = float.TryParse(textBox1.Text, out val1);  
         if (flag == false)  
         {  
           MessageBox.Show("Invaid Value Entered", "Syntax Error Message");  
           textBox1.Focus();  
         }  
         answer = val + val1;  
         textBox1.Text = answer.ToString();  
         textBox2.Text = val.ToString() + "+" + val1.ToString() + "=" + answer.ToString();  
         a = 0;  
         b = 0;  
         c = 0;  
         d = 0;  
       }  
       else  
       {  
         MessageBox.Show("click right button", "Syntax Error");  
       }  
     }  
     private void button13_Click(object sender, EventArgs e)  
     {  
       Close();  
     }  
     private void button14_Click(object sender, EventArgs e)//multiply  
     {  
       ch = 2;  
       if(b==0)  
       {  
       flag = float.TryParse(textBox1.Text, out val2);  
       if (flag == false)  
       {  
         MessageBox.Show("Invaid Value Entered", "Syntax Error Message");  
         textBox1.Focus();  
       }  
       textBox1.Clear();  
       textBox1.Focus();  
         b=1;  
         a=2;  
         c=2;  
         d=2;  
       }  
       else if(b==1)  
       {flag = float.TryParse(textBox1.Text, out val1);  
         if (flag == false)  
         {  
           MessageBox.Show("Invaid Value Entered", "Syntax Error Message");  
           textBox1.Focus();  
         }  
         answer = val2 * val1;  
         textBox1.Text = answer.ToString();  
         textBox2.Text = val2.ToString() + "*" + val1.ToString() + "=" + answer.ToString();  
         b=0;  
         a=0;  
         c=0;  
         d=0;  
       }  
       else  
       {  
         MessageBox.Show("click right button", "Syntax Error");  
       }  
     }  
     private void button15_Click(object sender, EventArgs e)  
     {  
       textBox1.Clear();  
       textBox2.Clear();  
       textBox1.Focus();  
       a = 0;  
       b = 0;  
       c = 0;  
       d = 0;  
     }  
     private void button17_Click(object sender, EventArgs e)//MINUS  
     {  
       ch = 3;  
       if (c == 0)  
       {  
         flag = float.TryParse(textBox1.Text, out val3);  
         if (flag == false)  
         {  
           MessageBox.Show("Invaid Value Entered", "Error Message");  
           textBox1.Focus();  
         }  
         textBox1.Clear();  
         textBox1.Focus();  
         c= 1;  
         a = 2;  
         b = 2;  
         d = 2;  
       }  
       else if(c==1)  
       {  
         flag = float.TryParse(textBox1.Text, out val1);  
         if (flag == false)  
         {  
           MessageBox.Show("Invaid Value Entered", "Syntax Error Message");  
           textBox1.Focus();  
         }  
         answer = val3 - val1;  
         textBox1.Text = answer.ToString();  
         textBox2.Text = val3.ToString() + "-" + val1.ToString() + "=" + answer.ToString();  
         c = 0;  
         a = 0;  
         b = 0;  
         d = 0;  
       }  
       else  
       {  
         MessageBox.Show("click right button", "Syntax Error");  
       }  
     }  
     private void button16_Click(object sender, EventArgs e)    //divide  
     {  
       ch = 4;  
       if (d == 0)  
       {  
        flag = float.TryParse(textBox1.Text, out val4);  
         if (flag == false)  
         {  
           MessageBox.Show("Invaid Value Entered", "Error Message");  
           textBox1.Focus();  
         }  
         textBox1.Clear();  
         textBox1.Focus();  
         d = 1;  
         a = 2;  
         b = 2;  
         c = 2;  
       }  
       else if(d==1)  
       {  
         flag = float.TryParse(textBox1.Text, out val1);  
         if (flag == false)  
         {  
           MessageBox.Show("Invaid Value Entered", "Syntax Error Message");  
           textBox1.Focus();  
         }  
         answer = val4 / val1;  
         textBox1.Text = answer.ToString();  
         textBox2.Text = val4.ToString() + "/" + val1.ToString() + "=" + answer.ToString();  
         a = 0;  
         b = 0;  
         c = 0;  
         d = 0;  
       }  
       else  
       {  
         MessageBox.Show("click right button", "Syntax Error");  
       }  
     }  
       private void button11_Click(object sender, EventArgs e)  
     {  
       switch(ch)  
       {  
         case 1:  flag = float.TryParse(textBox1.Text, out val1);  
         if (flag == false)  
         {  
           MessageBox.Show("Invaid Value Entered", "Syntax Error Message");  
           textBox1.Focus();  
         }  
         answer = val + val1;  
         textBox1.Text = answer.ToString();  
         textBox2.Text = val.ToString() + "+" + val1.ToString() + "=" + answer.ToString();  
         a = 0;  
         b = 0;  
         c = 0;  
         d = 0;  
         break;  
         case 2:  
          flag = float.TryParse(textBox1.Text, out val1);  
         if (flag == false)  
         {  
           MessageBox.Show("Invaid Value Entered", "Syntax Error Message");  
           textBox1.Focus();  
         }  
         answer = val2 * val1;  
         textBox1.Text = answer.ToString();  
         textBox2.Text = val2.ToString() + "*" + val1.ToString() + "=" + answer.ToString();  
         a = 0;  
         b = 0;  
         c = 0;  
         d = 0;  
         break;  
         case 3:  
         flag = float.TryParse(textBox1.Text, out val1);  
         if (flag == false)  
         {  
           MessageBox.Show("Invaid Value Entered", "Syntax Error Message");  
           textBox1.Focus();  
         }  
         answer = val3- val1;  
         textBox1.Text = answer.ToString();  
         textBox2.Text = val3.ToString() + "-" + val1.ToString() + "=" + answer.ToString();  
         a = 0;  
         b = 0;  
         c = 0;  
         d = 0;  
         break;  
         case 4:  
         flag = float.TryParse(textBox1.Text, out val1);  
           if (flag == false)  
           {  
             MessageBox.Show("Invaid Value Entered", "Syntax Error Message");  
             textBox1.Focus();  
           }  
           answer = val4 / val1;  
           textBox1.Text = answer.ToString();  
           textBox2.Text = val4.ToString() + "/" + val1.ToString() + "=" + answer.ToString();  
           a = 0;  
           b = 0;  
           c = 0;  
           d = 0;  
           break;  
       }            
     }  
     private void button18_Click(object sender, EventArgs e)  
     {  
       textBox1.Text += ".";  
     }  
     private void button19_Click(object sender, EventArgs e)  
     {  
       textBox1.Clear();  
       textBox2.Clear();  
       textBox1.Focus();  
     }  
   }  
 }  


Look At the property window of ADD button


In this I have given Text=&ADD that will make A underlined in ADD, this is for we can use ADD button using keyboard by using command ALT+A(underlined Alphabet)

Properties window of textbox2

Set Multiline and Read only Property TRUE in Textbox2 properties window

Popular Posts