|
| 1 | +import java.awt.*; |
| 2 | +import java.awt.event.*; |
| 3 | +class MyCalc extends WindowAdapter implements ActionListener{ |
| 4 | + Frame f; |
| 5 | +Label l1; |
| 6 | +Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0; |
| 7 | +Button badd,bsub,bmult,bdiv,bmod,bcalc,bclr,bpts,bneg,bback; |
| 8 | +double xd; |
| 9 | +double num1,num2,check; |
| 10 | + |
| 11 | +MyCalc(){ |
| 12 | + f= new Frame("MY CALCULATOR"); |
| 13 | +// INSTANTIATING COMPONENETS |
| 14 | +l1=new Label(); |
| 15 | +l1.setBackground(Color.LIGHT_GRAY); |
| 16 | +l1.setBounds(50,50,260,60); |
| 17 | + |
| 18 | + |
| 19 | +b1=new Button("1"); |
| 20 | + b1.setBounds(50,340,50,50); |
| 21 | +b2=new Button("2"); |
| 22 | + b2.setBounds(120,340,50,50); |
| 23 | +b3=new Button("3"); |
| 24 | + b3.setBounds(190,340,50,50); |
| 25 | +b4=new Button("4"); |
| 26 | + b4.setBounds(50,270,50,50); |
| 27 | +b5=new Button("5"); |
| 28 | + b5.setBounds(120,270,50,50); |
| 29 | +b6=new Button("6"); |
| 30 | + b6.setBounds(190,270,50,50); |
| 31 | +b7=new Button("7"); |
| 32 | + b7.setBounds(50,200,50,50); |
| 33 | +b8=new Button("8"); |
| 34 | + b8.setBounds(120,200,50,50); |
| 35 | +b9=new Button("9"); |
| 36 | + b9.setBounds(190,200,50,50); |
| 37 | +b0=new Button("0"); |
| 38 | + b0.setBounds(120,410,50,50); |
| 39 | +bneg=new Button("+/-"); |
| 40 | + bneg.setBounds(50,410,50,50); |
| 41 | +bpts=new Button("."); |
| 42 | + bpts.setBounds(190,410,50,50); |
| 43 | +bback=new Button("back"); |
| 44 | + bback.setBounds(120,130,50,50); |
| 45 | + |
| 46 | +badd=new Button("+"); |
| 47 | + badd.setBounds(260,340,50,50); |
| 48 | +bsub=new Button("-"); |
| 49 | + bsub.setBounds(260,270,50,50); |
| 50 | +bmult=new Button("*"); |
| 51 | + bmult.setBounds(260,200,50,50); |
| 52 | +bdiv=new Button("/"); |
| 53 | + bdiv.setBounds(260,130,50,50); |
| 54 | +bmod=new Button("%"); |
| 55 | + bmod.setBounds(190,130,50,50); |
| 56 | +bcalc=new Button("="); |
| 57 | + bcalc.setBounds(245,410,65,50); |
| 58 | +bclr=new Button("CE"); |
| 59 | + bclr.setBounds(50,130,65,50); |
| 60 | + |
| 61 | + |
| 62 | +b1.addActionListener(this); |
| 63 | +b2.addActionListener(this); |
| 64 | +b3.addActionListener(this); |
| 65 | +b4.addActionListener(this); |
| 66 | +b5.addActionListener(this); |
| 67 | +b6.addActionListener(this); |
| 68 | +b7.addActionListener(this); |
| 69 | +b8.addActionListener(this); |
| 70 | +b9.addActionListener(this); |
| 71 | +b0.addActionListener(this); |
| 72 | + |
| 73 | +bpts.addActionListener(this); |
| 74 | +bneg.addActionListener(this); |
| 75 | +bback.addActionListener(this); |
| 76 | + |
| 77 | +badd.addActionListener(this); |
| 78 | +bsub.addActionListener(this); |
| 79 | +bmult.addActionListener(this); |
| 80 | +bdiv.addActionListener(this); |
| 81 | +bmod.addActionListener(this); |
| 82 | +bcalc.addActionListener(this); |
| 83 | +bclr.addActionListener(this); |
| 84 | + |
| 85 | +f.addWindowListener(this); |
| 86 | +//ADDING TO FRAME |
| 87 | +f.add(l1); |
| 88 | +f.add(b1); f.add(b2); f.add(b3); f.add(b4); f.add(b5);f.add(b6); f.add(b7); f.add(b8);f.add(b9);f.add(b0); |
| 89 | + |
| 90 | +f.add(badd); f.add(bsub); f.add(bmod); f.add(bmult); f.add(bdiv); f.add(bmod);f.add(bcalc); |
| 91 | + |
| 92 | +f.add(bclr); f.add(bpts);f.add(bneg); f.add(bback); |
| 93 | + |
| 94 | +f.setSize(360,500); |
| 95 | +f.setLayout(null); |
| 96 | +f.setVisible(true); |
| 97 | +} |
| 98 | + //FOR CLOSING THE WINDOW |
| 99 | +public void windowClosing(WindowEvent e) { |
| 100 | + f.dispose(); |
| 101 | +} |
| 102 | + |
| 103 | +public void actionPerformed(ActionEvent e){ |
| 104 | + String z,zt; |
| 105 | + //NUMBER BUTTON |
| 106 | +if(e.getSource()==b1){ |
| 107 | + zt=l1.getText(); |
| 108 | + z=zt+"1"; |
| 109 | + l1.setText(z); |
| 110 | +} |
| 111 | +if(e.getSource()==b2){ |
| 112 | +zt=l1.getText(); |
| 113 | +z=zt+"2"; |
| 114 | +l1.setText(z); |
| 115 | +} |
| 116 | +if(e.getSource()==b3){ |
| 117 | + zt=l1.getText(); |
| 118 | + z=zt+"3"; |
| 119 | + l1.setText(z); |
| 120 | +} |
| 121 | +if(e.getSource()==b4){ |
| 122 | + zt=l1.getText(); |
| 123 | + z=zt+"4"; |
| 124 | + l1.setText(z); |
| 125 | +} |
| 126 | +if(e.getSource()==b5){ |
| 127 | + zt=l1.getText(); |
| 128 | + z=zt+"5"; |
| 129 | + l1.setText(z); |
| 130 | +} |
| 131 | +if(e.getSource()==b6){ |
| 132 | + zt=l1.getText(); |
| 133 | + z=zt+"6"; |
| 134 | + l1.setText(z); |
| 135 | +} |
| 136 | +if(e.getSource()==b7){ |
| 137 | + zt=l1.getText(); |
| 138 | + z=zt+"7"; |
| 139 | + l1.setText(z); |
| 140 | +} |
| 141 | +if(e.getSource()==b8){ |
| 142 | + zt=l1.getText(); |
| 143 | + z=zt+"8"; |
| 144 | + l1.setText(z); |
| 145 | +} |
| 146 | +if(e.getSource()==b9){ |
| 147 | + zt=l1.getText(); |
| 148 | + z=zt+"9"; |
| 149 | + l1.setText(z); |
| 150 | +} |
| 151 | +if(e.getSource()==b0){ |
| 152 | + zt=l1.getText(); |
| 153 | + z=zt+"0"; |
| 154 | + l1.setText(z); |
| 155 | +} |
| 156 | + |
| 157 | +if(e.getSource()==bpts){ //ADD DECIMAL PTS |
| 158 | + zt=l1.getText(); |
| 159 | + z=zt+"."; |
| 160 | + l1.setText(z); |
| 161 | +} |
| 162 | +if(e.getSource()==bneg){ //FOR NEGATIVE |
| 163 | + zt=l1.getText(); |
| 164 | + z="-"+zt; |
| 165 | + l1.setText(z); |
| 166 | +} |
| 167 | + |
| 168 | +if(e.getSource()==bback){ // FOR BACKSPACE |
| 169 | + zt=l1.getText(); |
| 170 | + try{ |
| 171 | + z=zt.substring(0, zt.length()-1); |
| 172 | + }catch(StringIndexOutOfBoundsException f){return;} |
| 173 | + l1.setText(z); |
| 174 | +} |
| 175 | + //AIRTHMETIC BUTTON |
| 176 | +if(e.getSource()==badd){ //FOR ADDITION |
| 177 | + try{ |
| 178 | + num1=Double.parseDouble(l1.getText()); |
| 179 | + }catch(NumberFormatException f){ |
| 180 | + l1.setText("Invalid Format"); |
| 181 | + return; |
| 182 | + } |
| 183 | + z=""; |
| 184 | + l1.setText(z); |
| 185 | + check=1; |
| 186 | +} |
| 187 | +if(e.getSource()==bsub){ //FOR SUBTRACTION |
| 188 | + try{ |
| 189 | + num1=Double.parseDouble(l1.getText()); |
| 190 | + }catch(NumberFormatException f){ |
| 191 | + l1.setText("Invalid Format"); |
| 192 | + return; |
| 193 | + } |
| 194 | + z=""; |
| 195 | + l1.setText(z); |
| 196 | + check=2; |
| 197 | +} |
| 198 | +if(e.getSource()==bmult){ //FOR MULTIPLICATION |
| 199 | + try{ |
| 200 | + num1=Double.parseDouble(l1.getText()); |
| 201 | + }catch(NumberFormatException f){ |
| 202 | + l1.setText("Invalid Format"); |
| 203 | + return; |
| 204 | + } |
| 205 | + z=""; |
| 206 | + l1.setText(z); |
| 207 | + check=3; |
| 208 | +} |
| 209 | +if(e.getSource()==bdiv){ //FOR DIVISION |
| 210 | + try{ |
| 211 | + num1=Double.parseDouble(l1.getText()); |
| 212 | + }catch(NumberFormatException f){ |
| 213 | + l1.setText("Invalid Format"); |
| 214 | + return; |
| 215 | + } |
| 216 | + z=""; |
| 217 | + l1.setText(z); |
| 218 | + check=4; |
| 219 | +} |
| 220 | +if(e.getSource()==bmod){ //FOR MOD/REMAINDER |
| 221 | + try{ |
| 222 | + num1=Double.parseDouble(l1.getText()); |
| 223 | + }catch(NumberFormatException f){ |
| 224 | + l1.setText("Invalid Format"); |
| 225 | + return; |
| 226 | + } |
| 227 | + z=""; |
| 228 | + l1.setText(z); |
| 229 | + check=5; |
| 230 | +} |
| 231 | + //RESULT BUTTON |
| 232 | +if(e.getSource()==bcalc){ |
| 233 | + try{ |
| 234 | + num2=Double.parseDouble(l1.getText()); |
| 235 | + }catch(Exception f){ |
| 236 | + l1.setText("ENTER NUMBER FIRST "); |
| 237 | + return; |
| 238 | + } |
| 239 | + if(check==1) |
| 240 | + xd =num1+num2; |
| 241 | + if(check==2) |
| 242 | + xd =num1-num2; |
| 243 | + if(check==3) |
| 244 | + xd =num1*num2; |
| 245 | + if(check==4) |
| 246 | + xd =num1/num2; |
| 247 | + if(check==5) |
| 248 | + xd =num1%num2; |
| 249 | + l1.setText(String.valueOf(xd)); |
| 250 | +} |
| 251 | + //FOR CLEARING THE LABEL and Memory |
| 252 | +if(e.getSource()==bclr){ |
| 253 | + num1=0; |
| 254 | + num2=0; |
| 255 | + check=0; |
| 256 | + xd=0; |
| 257 | + z=""; |
| 258 | + l1.setText(z); |
| 259 | + } |
| 260 | + |
| 261 | +} |
| 262 | +//MAIN METHOD where objects of MyCalc is instantaiated |
| 263 | + public static void main(String args[]){ |
| 264 | + new MyCalc(); |
| 265 | + } |
| 266 | +} |
0 commit comments