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