JAVA WEEK7
/*
Week 7 :
Write a Java program that works as a simple calculator.
Use a grid layout to arrange buttons for th...
JAVA WEEK7
bc.addActionListener(this);
beql.addActionListener(this);
//setLayout(new GridLayout(4,4));
}
public void actio...
JAVA WEEK7
    }
     if(ae.getSource()==b9)
    {
      int k=Integer.parseInt(t.getText());
      k=k*10+9;
    t.setTex...
Upcoming SlideShare
Loading in...5
×

Java Week7 Notepad

296

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
296
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Java Week7 Notepad"

  1. 1. JAVA WEEK7 /* Week 7 : Write a Java program that works as a simple calculator. Use a grid layout to arrange buttons for the digits and for the +, -,*, % operations. Add a text field to display the result. */ import java.awt.event.*; import java.applet.Applet; import java.awt.*; /* < applet code= LabPro14 Width=300 Height=300 > < /applet > */ public class LabPro14 extends Applet implements ActionListener { TextField t;String a;int p=0,tmp=0; Button bo,b1,b2,b3,b4,b5,b6,b7,b8,b9; Button badd,bsub,bmul,bdiv,bper,beql,bc; public void init() { t = new TextField(50); bo = new Button("0"); 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"); badd = new Button("+"); bsub = new Button("-"); bmul = new Button("*"); bdiv = new Button("/"); bper = new Button("%"); bc = new Button("c"); beql = new Button("="); add(t);add(bo);add(b1); add(b2);add(b3);add(b4); add(b5);add(b6);add(b7); add(b8);add(b9);add(badd); add(bsub);add(bmul);add(bdiv); add(bper);add(bc);add(beql); bo.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); badd.addActionListener(this); bsub.addActionListener(this); bmul.addActionListener(this); bdiv.addActionListener(this); bper.addActionListener(this); Page 1
  2. 2. JAVA WEEK7 bc.addActionListener(this); beql.addActionListener(this); //setLayout(new GridLayout(4,4)); } public void actionPerformed(ActionEvent ae) { if(ae.getSource()==bc) { t.setText("0"); } if(ae.getSource()==bo) { int k=Integer.parseInt(t.getText()); k=k*10+0; t.setText(String.valueOf(k)); } if(ae.getSource()==b1) { int k=Integer.parseInt(t.getText()); k=k*10+1; t.setText(String.valueOf(k)); } if(ae.getSource()==b2) { int k=Integer.parseInt(t.getText()); k=k*10+2; t.setText(String.valueOf(k)); } if(ae.getSource()==b3) { int k=Integer.parseInt(t.getText()); k=k*10+3; t.setText(String.valueOf(k)); } if(ae.getSource()==b4) { int k=Integer.parseInt(t.getText()); k=k*10+4; t.setText(String.valueOf(k)); } if(ae.getSource()==b5) { int k=Integer.parseInt(t.getText()); k=k*10+5; t.setText(String.valueOf(k)); } if(ae.getSource()==b6) { int k=Integer.parseInt(t.getText()); k=k*10+6; t.setText(String.valueOf(k)); } if(ae.getSource()==b7) { int k=Integer.parseInt(t.getText()); k=k*10+7; t.setText(String.valueOf(k)); } if(ae.getSource()==b8) { int k=Integer.parseInt(t.getText()); k=k*10+8; t.setText(String.valueOf(k)); Page 2
  3. 3. JAVA WEEK7 } if(ae.getSource()==b9) { int k=Integer.parseInt(t.getText()); k=k*10+9; t.setText(String.valueOf(k)); } if(ae.getSource()==badd) { tmp=Integer.parseInt(t.getText()); p=1; t.setText("0"); } if(ae.getSource()==bsub) { tmp=Integer.parseInt(t.getText()); p=2; t.setText("0"); } if(ae.getSource()==bmul) { tmp=Integer.parseInt(t.getText()); p=3; t.setText("0"); } if(ae.getSource()==bdiv) { tmp=Integer.parseInt(t.getText()); p=4; t.setText("0"); } if(ae.getSource()==bper) { tmp=Integer.parseInt(t.getText()); p=5; t.setText("0"); } if(ae.getSource()==beql) { float newval=Integer.parseInt(t.getText()); float res=0; switch(p) { case 1: res=tmp+newval; break; case 2: res=tmp-newval; break; case 3: res=tmp*newval; break; case 4: res=tmp/newval; break; case 5: res=tmp%newval; break; } t.setText(String.valueOf(res)); } } } Page 3

×