GeekInterview.com
Series: Subject: Topic:
Question: 276 of 994

Code for creating a simple Calculator in Java Script.

Asked by: cindy7alex | Member Since Apr-2006 | Asked on: Oct 29th, 2006

View all questions by cindy7alex   View all answers by cindy7alex

Showing Answers 1 - 2 of 2 Answers

import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
public class calculator1 implements ActionListener,Runnable
 {
 JLabel l1,l2;
  String s1="",s2="",s3="",s4="",s5="";
 String correct[]={"","","","","","","","","",""};
 String named[]={"C","r","e","a","t","e","d"," ","By"," ","B","H","A","S","K","A",R"," ","C","H","A","U","H","A","A","N"," "," "," "," "," "," "," "," "," "," "};
 String  chk[]=new String[100];
 int mid=40,pm=0,dc=0,cdk=0,bn=0,D3=0,k4=0,d4=5,n1=0;
 double  k1=0,k2=0,rm=0,k3=0,gtt=0;
 JFrame f1;
 Thread datimeThread,tname;
 Date date;
 GregorianCalendar calendar;
 String strDate,strTime,strStatus;
  JPanel p1;
  JTextField tf;
  JButton b0;
  JButton b1;
  JButton b2;
  JButton b3;
  JButton b4;
  JButton b5;
  JButton b6;
  JButton b7;
  JButton b8;
  JButton b9;
  JButton plus;
  JButton min;
  JButton div;
  JButton mul;
  JButton per;
  JButton point;
  JButton ac;
  JButton can;
  JButton corr;
  JButton dz;
  JButton eq;
  JButton check;
  JButton gt;
//*************************************************************************************************  
  calculator1()
  { 
   f1=new JFrame("Calculator");
   p1=new JPanel();
   tf=new JTextField(15);
   b0=new JButton("0");
   b1=new JButton("1");
   b2=new JButton("2");
   b3=new JButton("3");
   b4=new JButton("4");
   b5=new JButton("5");
   b6=new JButton("6");
   b7=new JButton("7");
   b8=new JButton("8");
   b9=new JButton("9");
   plus=new JButton("+");
   min=new JButton("-");
   div=new JButton("/");
   mul=new JButton("*");
   per=new JButton("%");
   point=new JButton(".");
   ac=new JButton("AC");
   can=new JButton("C");
   corr=new JButton(" Correct ");
   dz=new JButton("00");
   eq=new JButton("                       =                       ");
   check=new JButton("Replay");
   gt=new JButton("GT");
   l1=new JLabel("");
   l2=new JLabel("");
                 ac.setForeground(Color.red);
   can.setForeground(Color.red);
   corr.setForeground(Color.blue);
   gt.setForeground(Color.blue);
  
  p1.add("East",l2);
  p1.add(tf);
  p1.add(ac);
  p1.add(can);
  p1.add(check);
  p1.add(corr);
  p1.add(b1);
  p1.add(b2);
  p1.add(b3);
  p1.add(b4);
  p1.add(b5);
  p1.add(b6);
  p1.add(b7);
  p1.add(b8);
  p1.add(b9);
  p1.add(b0);
  p1.add(dz);
  p1.add(plus);
  p1.add(min);
  p1.add(div);
  p1.add(mul);
  p1.add(per);
  p1.add(point);
  p1.add(gt);
  p1.add(eq);
  tf.setText("0");
  p1.add(l1);
  
  b0.addActionListener(this);
  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);
  dz.addActionListener(this);
  min.addActionListener(this);
  div.addActionListener(this);
  per.addActionListener(this);
  mul.addActionListener(this);
  eq.addActionListener(this);
  plus.addActionListener(this);
  point.addActionListener(this);
  corr.addActionListener(this);
  ac.addActionListener(this);
  can.addActionListener(this);
  gt.addActionListener(this);
  datimeThread=new Thread(this);
  check.addActionListener(this);
  datimeThread=new Thread(this);
  datimeThread.start();
  tname=new Thread(this);
  tname.start();
  f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  f1.getContentPane().add(p1);
  f1.setResizable(false);
  f1.setSize(210,325);
  f1.setVisible(true);
        }
//***************************  END OF CONTRUCTOR  ****************************************
 public static void main(String args[])
 {
 calculator1 app=new calculator1();
 }


//**************************** END OF MAIN  begain of action per  *******************************************

 public void actionPerformed(ActionEvent e)
 {
   if(e.getSource()==b0)
   {
                   s1="0";
                s2=s2+s1;
     correct[pm]=s1;pm++; 
   }

   if(e.getSource()==b1)
   {
                   s1="1";
                s2=s2+s1;correct[pm]=s1;pm++;  
   }

  if(e.getSource()==b2)
   {
                   s1="2";
                s2=s2+s1;correct[pm]=s1;pm++;  
   }
  if(e.getSource()==b3)
   {
                   s1="3";
                s2=s2+s1;correct[pm]=s1;pm++;  
   }
  if(e.getSource()==b4)
   {
                   s1="4";
                s2=s2+s1;correct[pm]=s1;pm++;  
   }
   
  if(e.getSource()==b5)
   {
                   s1="5";
                s2=s2+s1;correct[pm]=s1;pm++;  
   }

  if(e.getSource()==b6)
   {
                   s1="6";
                s2=s2+s1;correct[pm]=s1;pm++;  
   }

  if(e.getSource()==b7)
   {
                   s1="7";
                s2=s2+s1;correct[pm]=s1;pm++;  
   }

  if(e.getSource()==b8)
   {
                   s1="8";
                s2=s2+s1;correct[pm]=s1;pm++;  
   }

  if(e.getSource()==b9)
   {
                   s1="9";
                s2=s2+s1;correct[pm]=s1;pm++;  
   }

  if(e.getSource()==dz)
   {
                   s1="00";
                s2=s2+s1;
   correct[pm]=s1;
   pm++;  
   }

  if(e.getSource()==point)
   {
                   s1=".";
                s2=s2+s1;
   correct[pm]=s1;pm++;  
   }
 
 

   tf.setText(s2);
 
 


//*************************************** Begain AC Button************************************************************************************
     if(e.getSource()==ac)
  {
   k3=0;
   k2=0;
   tf.setText("0");
   s2="";
   pm=0;
   check.setText("Replay");
   cdk=0;
   bn=0;
   gtt=0;
   rm=0;
    for(int hj=0;hj<100;hj++)
     {
     chk[hj]="";
     }
  }
//*******************************BeGAIN CANCEL BUTTON************************************************************************
   if(e.getSource()==can)
  {
  tf.setText("0");s2="";pm=0;
  }                  
 
//******************************  Begain Correct Button  ********************************************************************************************
 if(e.getSource()==corr)
  {   
  pm=pm-1;s2="";

    for(dc=0;dc<pm; dc++)
    {
     s2=s2+correct[dc];
    }
  tf.setText(s2);
  } 
//******************************* Begain Plus Button*************************************************************************************
 if(e.getSource()==plus)
  {
     if(k4==12 && s2=="")
    {
    s2=s4;k4=0;
    }
 
  chk[cdk]="+ " +s2;cdk++;
  k1=Double.parseDouble(s2);
  s2=""; 
     if(mid==2)
     {
    k2=k2-k1;
     } 
 
    if(mid==3)
     {
    k2=k2*k1;
     } 

    if(mid==4)
     {
    k2=k2/k1;
     } 
  
                  if(k3==0||mid==1)
    {  
                  k2=k2+k1;
     k3=5;
    }

  tf.setText(""+k2);
  mid=1;
  pm=0; 
  }

//***********************************BEGAIN OF MINUS BUTTON****************************************************************
 if(e.getSource()==min)
  {
   if(k4==12 && s2=="")
   {
   s2=s4;k4=0;
   }

   chk[cdk]="- " +s2;cdk++;
   k1=Double.parseDouble(s2);
   s2=""; 
    if(mid==1)
    {
   k2=k2+k1;
    } 
 
   if(mid==3)
    {
   k2=k2*k1;
    } 

   if(mid==4)
    {
   k2=k2/k1;
    } 


                  if(k3==0||mid==2)
   {
      if(k3==0)
                         {
    k2=k2+k1;k3=5;
    }
    else
    {
    k2=k2-k1;
    } 
            
   }

                tf.setText(""+k2);
  mid=2;
  pm=0; 
  }
//*********************************BEGAIN OF MULT BUTTON*****************************************************************
  if(e.getSource()==mul)
  {

   if(k4==12 && s2=="")
   {
   s2=s4;k4=0;
   }


  chk[cdk]="* " +s2;cdk++;
  k1=Double.parseDouble(s2);
  s2=""; 
    if(mid==1)
    {
   k2=k2+k1;
    } 
 
   if(mid==2)
    {
   k2=k2-k1;
    } 

   if(mid==4)
    {
   k2=k2/k1;
    } 


                  if(k3==0||mid==3)
    {
     if(k3==0)
                           {
     k2=k2+k1;
     k3=5;
     }
     else
     {
     k2=k2*k1;
     } 
            
    }
                tf.setText(""+k2);
  mid=3;
  pm=0; 
  }
//*******************************Begain OF divion botton********************************************************************
  if(e.getSource()==div)
  {
   if(k4==12 && s2=="")
   {
   s2=s4;k4=0;
   }
   chk[cdk]="/ " +s2;cdk++;
   k1=Double.parseDouble(s2);
   s2=""; 
    if(mid==1)
    {
   k2=k2+k1;
    } 
 
   if(mid==2)
    {
   k2=k2-k1;
    } 

   if(mid==3)
    {
   k2=k2*k1;
    } 


                  if(k3==0||mid==4)
   {
    if(k3==0)
                         {
    k2=k2+k1;
    k3=5;
    }
    else
    {
    k2=k2/k1;
    } 
            
   }
                tf.setText(""+k2);
  mid=4;
  pm=0;
  }
//********************************Begain Of equal button*****************************************************************
  if(e.getSource()==eq)
  {
  chk[cdk]="= " +s2;cdk++;
  k1=Double.parseDouble(s2);
  s2=""; 
    if(mid==1)
    {
   k2=k2+k1;
    } 
 
   if(mid==2)
    {
   k2=k2-k1;
    } 

   if(mid==4)
    {
   k2=k2/k1;
    } 


                  if(mid==3)
   {
   k2=k2*k1; 
    }
  
  
                tf.setText(""+k2);
  chk[cdk]="ANS IS : " +k2;
  cdk++;
  mid=25;
  rm=k2;
  s4=""+k2;
  k2=0;
  k3=0;
  pm=0;  
  D3=5; 
  k4=12;
  d4=5; 
  }
//***********************************Begain Of Persentage button************************************************************************    
                if(e.getSource()==per)
  {
  chk[cdk]="% "+s2;
  cdk++;
  k1=Double.parseDouble(s2);
  s2=""; 
    if(mid==1)
    {
   k2=k2+(k2*(k1/100));
    } 
 
   if(mid==2)
    {
   k2=k2-(k2*(k1/100));
    } 

   if(mid==4)
    {
   k2=k2*(k1/100);
    } 


                  if(k3==0||mid==3)
   {
    if(k3==0)
                         {
    k2=0;
    k3=5;
    }
    else
    {
    k2=k2*(k1/100);
    } 
            
   }
                tf.setText(""+k2);
  mid=40;
  pm=0;
  s4=""+k2;
  k2=0;
  k3=0;
  k4=12;
  }
//**********************************Begain Of Replay button***************************************************************************
 
 if(e.getSource()==check)
  {
  tf.setText(chk[bn]);
  bn++;
  check.setText("Rep:"+bn);
  if(cdk==bn)
  {
  bn=0;
   if(D3==5)
   {
   check.setText("Ans");
   }
   }  


  }

  if(cdk==0)
  {
  check.setText("Replay");
  }  

//*****************************Add in GT by Pressing '=' button*****************************************************************


 if(d4==5)
 {

 gtt=gtt+rm;
 d4=0;

 }

//******************************Begain of grand totel Button************************************************

 if(e.getSource()==gt)
 {
 tf.setText(""+gtt);
 }
//***********************end of event methord *************************************

 }
//****************** begin display methord***************************************************
 public void display()
 {
 date =new Date();
 calendar=new GregorianCalendar();
 calendar.setTime(date);
 strTime=calendar.get(Calendar.HOUR)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND);
 strDate=(calendar.get(Calendar.MONTH)+1)+"/"+calendar.get(Calendar.DATE)+"/"+calendar.get(Calendar.YEAR);
 strStatus=strTime+"      "+strDate;
 l1.setText(strStatus);
 }

 public void nameop()
 {
 s5=s5+named[n1];
 l2.setText("                 "+s5);
 if(n1==31)
 {
 n1=-1;s5="";
 }
 ++n1;
 }
//***********************Begain of run methord****************************************

 public void run()
 {

  while(datimeThread !=null)
  { 
 display();nameop();
 
   try
   {

    datimeThread.sleep(300);
   tname.sleep(100);
   }
   catch(InterruptedException e)
   {
   System.out.println("Thread interrupted");
   }
  }

 }
//**************************End of run methord******************************************

}
//********************End of Calculator1 class**********************************

  
Login to rate this answer.

import java.util.*;
import java.awt.event.*;
import javax.swing.*;
public class calculator1 implements ActionListener Runnable
{
JLabel l1 l2;
String s1 "" s2 "" s3 "" s4 "" s5 "";
String correct[] {"" "" "" "" "" "" "" "" "" ""};
String named[] {"C" "r" "e" "a" "t" "e" "d" " " "By" " " "B" "H" "A" "S" "K" "A" R" " " "C" "H" "A" "U" "H" "A" "A" "N" " " " " " " " " " " " " " " " " " " " "};
String chk[] new String[100];
int mid 40 pm 0 dc 0 cdk 0 bn 0 D3 0 k4 0 d4 5 n1 0;
double k1 0 k2 0 rm 0 k3 0 gtt 0;
JFrame f1;
Thread datimeThread tname;
Date date;
GregorianCalendar calendar;
String strDate strTime strStatus;
JPanel p1;
JTextField tf;
JButton b0;
JButton b1;
JButton b2;
JButton b3;
JButton b4;
JButton b5;
JButton b6;
JButton b7;
JButton b8;
JButton b9;
JButton plus;
JButton min;
JButton div;
JButton mul;
JButton per;
JButton point;
JButton ac;
JButton can;
JButton corr;
JButton dz;
JButton eq;
JButton check;
JButton gt;
//*************************************************************************************************
calculator1()
{
f1 new JFrame("Calculator");
p1 new JPanel();
tf new JTextField(15);
b0 new JButton("0");
b1 new JButton("1");
b2 new JButton("2");
b3 new JButton("3");
b4 new JButton("4");
b5 new JButton("5");
b6 new JButton("6");
b7 new JButton("7");
b8 new JButton("8");
b9 new JButton("9");
plus new JButton("+");
min new JButton("-");
div new JButton("/");
mul new JButton("*");
per new JButton(" ");
point new JButton(".");
ac new JButton("AC");
can new JButton("C");
corr new JButton(" Correct ");
dz new JButton("00");
eq new JButton(" ");
check new JButton("Replay");
gt new JButton("GT");
l1 new JLabel("");
l2 new JLabel("");
ac.setForeground(Color.red);
can.setForeground(Color.red);
corr.setForeground(Color.blue);
gt.setForeground(Color.blue);

p1.add("East" l2);
p1.add(tf);
p1.add(ac);
p1.add(can);
p1.add(check);
p1.add(corr);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(b0);
p1.add(dz);
p1.add(plus);
p1.add(min);
p1.add(div);
p1.add(mul);
p1.add(per);
p1.add(point);
p1.add(gt);
p1.add(eq);
tf.setText("0");
p1.add(l1);

b0.addActionListener(this);
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);
dz.addActionListener(this);
min.addActionListener(this);
div.addActionListener(this);
per.addActionListener(this);
mul.addActionListener(this);
eq.addActionListener(this);
plus.addActionListener(this);
point.addActionListener(this);
corr.addActionListener(this);
ac.addActionListener(this);
can.addActionListener(this);
gt.addActionListener(this);
datimeThread new Thread(this);
check.addActionListener(this);
datimeThread new Thread(this);
datimeThread.start();
tname new Thread(this);
tname.start();
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.getContentPane().add(p1);
f1.setResizable(false);
f1.setSize(210 325);
f1.setVisible(true);
}
//*************************** END OF CONTRUCTOR ****************************************
public static void main(String args[])
{
calculator1 app new calculator1();
}

//**************************** END OF MAIN begain of action per *******************************************
public void actionPerformed(ActionEvent e)
{
if(e.getSource() b0)
{
s1 "0";
s2 s2+s1;
correct[pm] s1;pm++;
}
if(e.getSource() b1)
{
s1 "1";
s2 s2+s1;correct[pm] s1;pm++;
}
if(e.getSource() b2)
{
s1 "2";
s2 s2+s1;correct[pm] s1;pm++;
}
if(e.getSource() b3)
{
s1 "3";
s2 s2+s1;correct[pm] s1;pm++;
}
if(e.getSource() b4)
{
s1 "4";
s2 s2+s1;correct[pm] s1;pm++;
}

if(e.getSource() b5)
{
s1 "5";
s2 s2+s1;correct[pm] s1;pm++;
}
if(e.getSource() b6)
{
s1 "6";
s2 s2+s1;correct[pm] s1;pm++;
}
if(e.getSource() b7)
{
s1 "7";
s2 s2+s1;correct[pm] s1;pm++;
}
if(e.getSource() b8)
{
s1 "8";
s2 s2+s1;correct[pm] s1;pm++;
}
if(e.getSource() b9)
{
s1 "9";
s2 s2+s1;correct[pm] s1;pm++;
}
if(e.getSource() dz)
{
s1 "00";
s2 s2+s1;
correct[pm] s1;
pm++;
}
if(e.getSource() point)
{
s1 ".";
s2 s2+s1;
correct[pm] s1;pm++;
}
tf.setText(s2); 

//*************************************** Begain AC Button************************************************************************************
if(e.getSource() ac)
{
k3 0;
k2 0;
tf.setText("0");
s2 "";
pm 0;
check.setText("Replay");
cdk 0;
bn 0;
gtt 0;
rm 0;
for(int hj 0;hj<100;hj++)
{
chk[hj] "";
}
}
//*******************************BeGAIN CANCEL BUTTON************************************************************************
if(e.getSource() can)
{
tf.setText("0");s2 "";pm 0;
}

//****************************** Begain Correct Button ********************************************************************************************
if(e.getSource() corr)
{
pm pm-1;s2 "";

for(dc 0;dc<pm; dc++)
{
s2 s2+correct[dc];
}
tf.setText(s2);
}
//******************************* Begain Plus Button*************************************************************************************
if(e.getSource() plus)
{
if(k4 12 && s2 "")
{
s2 s4;k4 0;
}

chk[cdk] "+ " +s2;cdk++;
k1 Double.parseDouble(s2);
s2 "";
if(mid 2)
{
k2 k2-k1;
}

if(mid 3)
{
k2 k2*k1;
}
if(mid 4)
{
k2 k2/k1;
}

if(k3 0||mid 1)
{
k2 k2+k1;
k3 5;
}
tf.setText(""+k2);
mid 1;
pm 0;
}
//***********************************BEGAIN OF MINUS BUTTON****************************************************************
if(e.getSource() min)
{
if(k4 12 && s2 "")
{
s2 s4;k4 0;
}
chk[cdk] "- " +s2;cdk++;
k1 Double.parseDouble(s2);
s2 "";
if(mid 1)
{
k2 k2+k1;
}

if(mid 3)
{
k2 k2*k1;
}
if(mid 4)
{
k2 k2/k1;
}

if(k3 0||mid 2)
{
if(k3 0)
{
k2 k2+k1;k3 5;
}
else

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.