|
| 1 | +import java.awt.*; |
| 2 | +import java.awt.event.*; |
| 3 | +/*********************************************/ |
| 4 | + |
| 5 | +public class MyCalculator extends Frame |
| 6 | +{ |
| 7 | + |
| 8 | +public boolean setClear=true; |
| 9 | +double number, memValue; |
| 10 | +char op; |
| 11 | + |
| 12 | +String digitButtonText[] = {"7", "8", "9", "4", "5", "6", "1", "2", "3", "0", "+/-", "." }; |
| 13 | +String operatorButtonText[] = {"/", "sqrt", "*", "%", "-", "1/X", "+", "=" }; |
| 14 | +String memoryButtonText[] = {"MC", "MR", "MS", "M+" }; |
| 15 | +String specialButtonText[] = {"Backspc", "C", "CE" }; |
| 16 | + |
| 17 | +MyDigitButton digitButton[]=new MyDigitButton[digitButtonText.length]; |
| 18 | +MyOperatorButton operatorButton[]=new MyOperatorButton[operatorButtonText.length]; |
| 19 | +MyMemoryButton memoryButton[]=new MyMemoryButton[memoryButtonText.length]; |
| 20 | +MySpecialButton specialButton[]=new MySpecialButton[specialButtonText.length]; |
| 21 | + |
| 22 | +Label displayLabel=new Label("0",Label.RIGHT); |
| 23 | +Label memLabel=new Label(" ",Label.RIGHT); |
| 24 | + |
| 25 | +final int FRAME_WIDTH=325,FRAME_HEIGHT=325; |
| 26 | +final int HEIGHT=30, WIDTH=30, H_SPACE=10,V_SPACE=10; |
| 27 | +final int TOPX=30, TOPY=50; |
| 28 | +/////////////////////////// |
| 29 | +MyCalculator(String frameText)//constructor |
| 30 | +{ |
| 31 | +super(frameText); |
| 32 | + |
| 33 | +int tempX=TOPX, y=TOPY; |
| 34 | +displayLabel.setBounds(tempX,y,240,HEIGHT); |
| 35 | +displayLabel.setBackground(Color.BLUE); |
| 36 | +displayLabel.setForeground(Color.WHITE); |
| 37 | +add(displayLabel); |
| 38 | + |
| 39 | +memLabel.setBounds(TOPX, TOPY+HEIGHT+ V_SPACE,WIDTH, HEIGHT); |
| 40 | +add(memLabel); |
| 41 | + |
| 42 | +// set Co-ordinates for Memory Buttons |
| 43 | +tempX=TOPX; |
| 44 | +y=TOPY+2*(HEIGHT+V_SPACE); |
| 45 | +for(int i=0; i<memoryButton.length; i++) |
| 46 | +{ |
| 47 | +memoryButton[i]=new MyMemoryButton(tempX,y,WIDTH,HEIGHT,memoryButtonText[i], this); |
| 48 | +memoryButton[i].setForeground(Color.RED); |
| 49 | +y+=HEIGHT+V_SPACE; |
| 50 | +} |
| 51 | + |
| 52 | +//set Co-ordinates for Special Buttons |
| 53 | +tempX=TOPX+1*(WIDTH+H_SPACE); y=TOPY+1*(HEIGHT+V_SPACE); |
| 54 | +for(int i=0;i<specialButton.length;i++) |
| 55 | +{ |
| 56 | +specialButton[i]=new MySpecialButton(tempX,y,WIDTH*2,HEIGHT,specialButtonText[i], this); |
| 57 | +specialButton[i].setForeground(Color.RED); |
| 58 | +tempX=tempX+2*WIDTH+H_SPACE; |
| 59 | +} |
| 60 | + |
| 61 | +//set Co-ordinates for Digit Buttons |
| 62 | +int digitX=TOPX+WIDTH+H_SPACE; |
| 63 | +int digitY=TOPY+2*(HEIGHT+V_SPACE); |
| 64 | +tempX=digitX; y=digitY; |
| 65 | +for(int i=0;i<digitButton.length;i++) |
| 66 | +{ |
| 67 | +digitButton[i]=new MyDigitButton(tempX,y,WIDTH,HEIGHT,digitButtonText[i], this); |
| 68 | +digitButton[i].setForeground(Color.BLUE); |
| 69 | +tempX+=WIDTH+H_SPACE; |
| 70 | +if((i+1)%3==0){tempX=digitX; y+=HEIGHT+V_SPACE;} |
| 71 | +} |
| 72 | + |
| 73 | +//set Co-ordinates for Operator Buttons |
| 74 | +int opsX=digitX+2*(WIDTH+H_SPACE)+H_SPACE; |
| 75 | +int opsY=digitY; |
| 76 | +tempX=opsX; y=opsY; |
| 77 | +for(int i=0;i<operatorButton.length;i++) |
| 78 | +{ |
| 79 | +tempX+=WIDTH+H_SPACE; |
| 80 | +operatorButton[i]=new MyOperatorButton(tempX,y,WIDTH,HEIGHT,operatorButtonText[i], this); |
| 81 | +operatorButton[i].setForeground(Color.RED); |
| 82 | +if((i+1)%2==0){tempX=opsX; y+=HEIGHT+V_SPACE;} |
| 83 | +} |
| 84 | + |
| 85 | +addWindowListener(new WindowAdapter() |
| 86 | +{ |
| 87 | +public void windowClosing(WindowEvent ev) |
| 88 | +{System.exit(0);} |
| 89 | +}); |
| 90 | + |
| 91 | +setLayout(null); |
| 92 | +setSize(FRAME_WIDTH,FRAME_HEIGHT); |
| 93 | +setVisible(true); |
| 94 | +} |
| 95 | +////////////////////////////////// |
| 96 | +static String getFormattedText(double temp) |
| 97 | +{ |
| 98 | +String resText=""+temp; |
| 99 | +if(resText.lastIndexOf(".0")>0) |
| 100 | + resText=resText.substring(0,resText.length()-2); |
| 101 | +return resText; |
| 102 | +} |
| 103 | +//////////////////////////////////////// |
| 104 | +public static void main(String []args) |
| 105 | +{ |
| 106 | +new MyCalculator("Calculator - JavaTpoint"); |
| 107 | +} |
| 108 | +} |
| 109 | + |
| 110 | +/*******************************************/ |
| 111 | + |
| 112 | +class MyDigitButton extends Button implements ActionListener |
| 113 | +{ |
| 114 | +MyCalculator cl; |
| 115 | + |
| 116 | +////////////////////////////////////////// |
| 117 | +MyDigitButton(int x,int y, int width,int height,String cap, MyCalculator clc) |
| 118 | +{ |
| 119 | +super(cap); |
| 120 | +setBounds(x,y,width,height); |
| 121 | +this.cl=clc; |
| 122 | +this.cl.add(this); |
| 123 | +addActionListener(this); |
| 124 | +} |
| 125 | +//////////////////////////////////////////////// |
| 126 | +static boolean isInString(String s, char ch) |
| 127 | +{ |
| 128 | +for(int i=0; i<s.length();i++) if(s.charAt(i)==ch) return true; |
| 129 | +return false; |
| 130 | +} |
| 131 | +///////////////////////////////////////////////// |
| 132 | +public void actionPerformed(ActionEvent ev) |
| 133 | +{ |
| 134 | +String tempText=((MyDigitButton)ev.getSource()).getLabel(); |
| 135 | + |
| 136 | +if(tempText.equals(".")) |
| 137 | +{ |
| 138 | + if(cl.setClear) |
| 139 | + {cl.displayLabel.setText("0.");cl.setClear=false;} |
| 140 | + else if(!isInString(cl.displayLabel.getText(),'.')) |
| 141 | + cl.displayLabel.setText(cl.displayLabel.getText()+"."); |
| 142 | + return; |
| 143 | +} |
| 144 | + |
| 145 | +int index=0; |
| 146 | +try{ |
| 147 | + index=Integer.parseInt(tempText); |
| 148 | + }catch(NumberFormatException e){return;} |
| 149 | + |
| 150 | +if (index==0 && cl.displayLabel.getText().equals("0")) return; |
| 151 | + |
| 152 | +if(cl.setClear) |
| 153 | + {cl.displayLabel.setText(""+index);cl.setClear=false;} |
| 154 | +else |
| 155 | + cl.displayLabel.setText(cl.displayLabel.getText()+index); |
| 156 | +}//actionPerformed |
| 157 | +}//class defination |
| 158 | + |
| 159 | +/********************************************/ |
| 160 | + |
| 161 | +class MyOperatorButton extends Button implements ActionListener |
| 162 | +{ |
| 163 | +MyCalculator cl; |
| 164 | + |
| 165 | +MyOperatorButton(int x,int y, int width,int height,String cap, MyCalculator clc) |
| 166 | +{ |
| 167 | +super(cap); |
| 168 | +setBounds(x,y,width,height); |
| 169 | +this.cl=clc; |
| 170 | +this.cl.add(this); |
| 171 | +addActionListener(this); |
| 172 | +} |
| 173 | +/////////////////////// |
| 174 | +public void actionPerformed(ActionEvent ev) |
| 175 | +{ |
| 176 | +String opText=((MyOperatorButton)ev.getSource()).getLabel(); |
| 177 | + |
| 178 | +cl.setClear=true; |
| 179 | +double temp=Double.parseDouble(cl.displayLabel.getText()); |
| 180 | + |
| 181 | +if(opText.equals("1/x")) |
| 182 | + { |
| 183 | + try |
| 184 | + {double tempd=1/(double)temp; |
| 185 | + cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));} |
| 186 | + catch(ArithmeticException excp) |
| 187 | + {cl.displayLabel.setText("Divide by 0.");} |
| 188 | + return; |
| 189 | + } |
| 190 | +if(opText.equals("sqrt")) |
| 191 | + { |
| 192 | + try |
| 193 | + {double tempd=Math.sqrt(temp); |
| 194 | + cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));} |
| 195 | + catch(ArithmeticException excp) |
| 196 | + {cl.displayLabel.setText("Divide by 0.");} |
| 197 | + return; |
| 198 | + } |
| 199 | +if(!opText.equals("=")) |
| 200 | + { |
| 201 | + cl.number=temp; |
| 202 | + cl.op=opText.charAt(0); |
| 203 | + return; |
| 204 | + } |
| 205 | +// process = button pressed |
| 206 | +switch(cl.op) |
| 207 | +{ |
| 208 | +case '+': |
| 209 | + temp+=cl.number;break; |
| 210 | +case '-': |
| 211 | + temp=cl.number-temp;break; |
| 212 | +case '*': |
| 213 | + temp*=cl.number;break; |
| 214 | +case '%': |
| 215 | + try{temp=cl.number%temp;} |
| 216 | + catch(ArithmeticException excp) |
| 217 | + {cl.displayLabel.setText("Divide by 0."); return;} |
| 218 | + break; |
| 219 | +case '/': |
| 220 | + try{temp=cl.number/temp;} |
| 221 | + catch(ArithmeticException excp) |
| 222 | + {cl.displayLabel.setText("Divide by 0."); return;} |
| 223 | + break; |
| 224 | +}//switch |
| 225 | + |
| 226 | +cl.displayLabel.setText(MyCalculator.getFormattedText(temp)); |
| 227 | +//cl.number=temp; |
| 228 | +}//actionPerformed |
| 229 | +}//class |
| 230 | + |
| 231 | +/****************************************/ |
| 232 | + |
| 233 | +class MyMemoryButton extends Button implements ActionListener |
| 234 | +{ |
| 235 | +MyCalculator cl; |
| 236 | + |
| 237 | +///////////////////////////////// |
| 238 | +MyMemoryButton(int x,int y, int width,int height,String cap, MyCalculator clc) |
| 239 | +{ |
| 240 | +super(cap); |
| 241 | +setBounds(x,y,width,height); |
| 242 | +this.cl=clc; |
| 243 | +this.cl.add(this); |
| 244 | +addActionListener(this); |
| 245 | +} |
| 246 | +//////////////////////////////////////////////// |
| 247 | +public void actionPerformed(ActionEvent ev) |
| 248 | +{ |
| 249 | +char memop=((MyMemoryButton)ev.getSource()).getLabel().charAt(1); |
| 250 | + |
| 251 | +cl.setClear=true; |
| 252 | +double temp=Double.parseDouble(cl.displayLabel.getText()); |
| 253 | + |
| 254 | +switch(memop) |
| 255 | +{ |
| 256 | +case 'C': |
| 257 | + cl.memLabel.setText(" ");cl.memValue=0.0;break; |
| 258 | +case 'R': |
| 259 | + cl.displayLabel.setText(MyCalculator.getFormattedText(cl.memValue));break; |
| 260 | +case 'S': |
| 261 | + cl.memValue=0.0; |
| 262 | +case '+': |
| 263 | + cl.memValue+=Double.parseDouble(cl.displayLabel.getText()); |
| 264 | + if(cl.displayLabel.getText().equals("0") || cl.displayLabel.getText().equals("0.0") ) |
| 265 | + cl.memLabel.setText(" "); |
| 266 | + else |
| 267 | + cl.memLabel.setText("M"); |
| 268 | + break; |
| 269 | +}//switch |
| 270 | +}//actionPerformed |
| 271 | +}//class |
| 272 | + |
| 273 | +/*****************************************/ |
| 274 | + |
| 275 | +class MySpecialButton extends Button implements ActionListener |
| 276 | +{ |
| 277 | +MyCalculator cl; |
| 278 | + |
| 279 | +MySpecialButton(int x,int y, int width,int height,String cap, MyCalculator clc) |
| 280 | +{ |
| 281 | +super(cap); |
| 282 | +setBounds(x,y,width,height); |
| 283 | +this.cl=clc; |
| 284 | +this.cl.add(this); |
| 285 | +addActionListener(this); |
| 286 | +} |
| 287 | +////////////////////// |
| 288 | +static String backSpace(String s) |
| 289 | +{ |
| 290 | +String Res=""; |
| 291 | +for(int i=0; i<s.length()-1; i++) Res+=s.charAt(i); |
| 292 | +return Res; |
| 293 | +} |
| 294 | + |
| 295 | +////////////////////////////////////////////////////////// |
| 296 | +public void actionPerformed(ActionEvent ev) |
| 297 | +{ |
| 298 | +String opText=((MySpecialButton)ev.getSource()).getLabel(); |
| 299 | +//check for backspace button |
| 300 | +if(opText.equals("Backspc")) |
| 301 | +{ |
| 302 | +String tempText=backSpace(cl.displayLabel.getText()); |
| 303 | +if(tempText.equals("")) |
| 304 | + cl.displayLabel.setText("0"); |
| 305 | +else |
| 306 | + cl.displayLabel.setText(tempText); |
| 307 | +return; |
| 308 | +} |
| 309 | +//check for "C" button i.e. Reset |
| 310 | +if(opText.equals("C")) |
| 311 | +{ |
| 312 | +cl.number=0.0; cl.op=' '; cl.memValue=0.0; |
| 313 | +cl.memLabel.setText(" "); |
| 314 | +} |
| 315 | + |
| 316 | +//it must be CE button pressed |
| 317 | +cl.displayLabel.setText("0");cl.setClear=true; |
| 318 | +}//actionPerformed |
| 319 | +}//class |
0 commit comments