Assignment-15

Write a program to create a simple calculator using Applet. Source Code: import java.applet.*; import java.awt.*; import java.awt.event.*;//ActionListener Interface public class cal extends Applet implements ActionListener { TextField t1; int u=0,v=0,w=0; Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17; String str1 =""; String str2 =""; String str3 =""; String str =""; String s =""; int i=0; public void init() { t1=new TextField(14); add(t1); b1=new Button("9"); add(b1); b2=new Button("8"); add(b2); b3=new Button("7"); add(b3); b4=new Button("+"); add(b4); b5=new Button("6"); add(b5); b6=new Button("5"); add(b6); b7=new Button("4"); add(b7); b8=new Button("-"); add(b8); b9=new Button("3"); add(b9); b10=new Button("2"); add(b10); b11=new Button("1"); add(b11); b12=new Button("*"); add(b12); b13=new Button("0"); add(b13); b14=new Button("."); add(b14); b15=new Button("="); add(b15); b16=new Button("/"); add(b16); b17=new Button("CLEAR"); add(b17); t1.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); b10.addActionListener(this); b11.addActionListener(this); b12.addActionListener(this); b13.addActionListener(this); b14.addActionListener(this); b15.addActionListener(this); b16.addActionListener(this); b17.addActionListener(this); } public void actionPerformed(ActionEvent ae) { if(ae.getActionCommand().equals("9")) { str3="9"; str+=str3; t1.setText(str); v=1; if(i==1) { w=1; } } else if(ae.getActionCommand().equals("8")) { str3="8"; str+=str3; t1.setText(str); v=1; if(i==1) { w=1; } } else if(ae.getActionCommand().equals("7")) { str3="7"; str+=str3; t1.setText(str); v=1; if(i==1) { w=1; } } else if(ae.getActionCommand().equals("6")) { str3="6"; str+=str3; t1.setText(str); v=1; if(i==1) { w=1; } } else if(ae.getActionCommand().equals("5")) { str3="5"; str+=str3; t1.setText(str); v=1; if(i==1) { w=1; } } else if(ae.getActionCommand().equals("4")) { str3="4"; str+=str3; t1.setText(str); v=1; if(i==1) { w=1; } } else if(ae.getActionCommand().equals("3")) { str3="3"; str+=str3; t1.setText(str); v=1; if(i==1) { w=1; } } else if(ae.getActionCommand().equals("2")) { str3="2"; str+=str3; t1.setText(str); v=1; if(i==1) { w=1; } } else if(ae.getActionCommand().equals("1")) { str3="1"; str+=str3; t1.setText(str); v=1; if(i==1) { w=1; } } else if(ae.getActionCommand().equals("0")) { str3="0"; str+=str3; t1.setText(str); v=1; if(i==1) { w=1; } } else if(ae.getActionCommand().equals(".")) { str3="."; str+=str3; t1.setText(str); v=1; if(i==1) { w=1; } } else if(v==0 && ae.getActionCommand().equals("-")) { str3="-"; str+=str3; t1.setText(str); v=1; } else if(w==0 && i==1 && ae.getActionCommand().equals("-")) { str3="-"; str+=str3; t1.setText(str); v=1; w=1; } else if(ae.getActionCommand().equals("+") || ae.getActionCommand().equals("-") || ae.getActionCommand().equals("*") || ae.getActionCommand().equals("/")) { if(ae.getActionCommand().equals("+")) { s="+"; } else if(ae.getActionCommand().equals("-")) { s="-"; } else if(ae.getActionCommand().equals("*")) { s="*"; } else if(ae.getActionCommand().equals("/")) { s="/"; } t1.setText(""); str=""; i=1; } if(i==0) { str1=str; } if(i==1) { str2=str; } if(ae.getActionCommand().equals("=")) { if(s.equals("+")) { double x=Double.parseDouble(str1); double y=Double.parseDouble(str2); double z=x+y; t1.setText(String.valueOf(z)); } if(s.equals("-")) { double x=Double.parseDouble(str1); double y=Double.parseDouble(str2); double z=x-y; t1.setText(String.valueOf(z)); } if(s.equals("*")) { double x=Double.parseDouble(str1); double y=Double.parseDouble(str2); double z=x*y; t1.setText(String.valueOf(z)); } if(s.equals("/")) { try{ double x=Double.parseDouble(str1); double y=Double.parseDouble(str2); double z=x/y; t1.setText(String.valueOf(z)); } catch(ArithmeticException e) { t1.setText("Error"); } } str=""; } if(ae.getActionCommand().equals("CLEAR")) { t1.setText(""); str1 =""; str2 =""; str3 =""; str =""; s =""; i=0; v=0; w=0; } } }

Share:

0 comments