Calculator project in Java Program



Calculator project in Java Program

1.      /*********************************************
2.      Save this file as MyCalculator.java
3.      to compile it use
4.      java MyCalculator.java
5.      to use the calcuator do this
6.      java My Calculator
7.      Creator-Jyotish kumar

8.      **********************************************/
9.      import java.awt.*;
10. import java.awt.event.*;
11. /*********************************************/
12.  
13. public class MyCalculator extends Frame
14. {
15.  
16. public boolean setClear=true;
17. double number, memValue;
18. char op;
19.  
20. String digitButtonText[] = {"7""8""9""4""5""6""1""2""3""0""+/-""." };
21. String operatorButtonText[] = {"/""sqrt""*""%""-""1/X""+""=" };
22. String memoryButtonText[] = {"MC""MR""MS""M+" };
23. String specialButtonText[] = {"Backspc""C""CE" };
24.  
25. MyDigitButton digitButton[]=new MyDigitButton[digitButtonText.length];
26. MyOperatorButton operatorButton[]=new MyOperatorButton[operatorButtonText.length];
27. MyMemoryButton memoryButton[]=new MyMemoryButton[memoryButtonText.length];
28. MySpecialButton specialButton[]=new MySpecialButton[specialButtonText.length];
29.  
30. Label displayLabel=new Label("0",Label.RIGHT);
31. Label memLabel=new Label(" ",Label.RIGHT);
32.  
33. final int FRAME_WIDTH=325,FRAME_HEIGHT=325;
34. final int HEIGHT=30, WIDTH=30, H_SPACE=10,V_SPACE=10;
35. final int TOPX=30, TOPY=50;
36. ///////////////////////////
37. MyCalculator(String frameText)//constructor
38. {
39. super(frameText);

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,memoryButtonText[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,operatorButtonText[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.                     MyDigitButton(int x,int y, int width,int height,String cap, MyCalculator clc)
124.                     {
125.                     super(cap);
126.                     setBounds(x,y,width,height);
127.                     this.cl=clc;
128.                     this.cl.add(this);
129.                     addActionListener(this);
130.                     }
131.                     ////////////////////////////////////////////////
132.                     static boolean isInString(String s, char ch)
133.                     {
134.                     for(int i=0; i<s.length();i++) if(s.charAt(i)==ch) return true;
135.                     return false;
136.                     }
137.                     /////////////////////////////////////////////////
138.                     public void actionPerformed(ActionEvent ev)
139.                     {
140.                     String tempText=((MyDigitButton)ev.getSource()).getLabel();
141.                      
142.                     if(tempText.equals("."))
143.                     {
144.                     if(cl.setClear)
145.                     {cl.displayLabel.setText("0.");cl.setClear=false;}
146.                     else if(!isInString(cl.displayLabel.getText(),'.'))
147.                     cl.displayLabel.setText(cl.displayLabel.getText()+".");
148.                     return;
149.                     }
150.                      
151.                     int index=0;
152.                     try{
153.                     index=Integer.parseInt(tempText);
154.                     }catch(NumberFormatException e){return;}
155.                      
156.                     if (index==0 && cl.displayLabel.getText().equals("0")) return;
157.                      
158.                     if(cl.setClear)
159.                     {cl.displayLabel.setText(""+index);cl.setClear=false;}
160.                     else
161.                     cl.displayLabel.setText(cl.displayLabel.getText()+index);
162.                     }//actionPerformed
163.                     }//class defination
164.                      
165.                     /********************************************/
166.                      
167.                     class MyOperatorButton extends Button implements ActionListener
168.                     {
169.                     MyCalculator cl;
170.                      
171.                     MyOperatorButton(int x,int y, int width,int height,String cap, MyCalculator clc)
172.                     {
173.                     super(cap);
174.                     setBounds(x,y,width,height);
175.                     this.cl=clc;
176.                     this.cl.add(this);
177.                     addActionListener(this);
178.                     }
179.                     ///////////////////////
180.                     public void actionPerformed(ActionEvent ev)
181.                     {
182.                     String opText=((MyOperatorButton)ev.getSource()).getLabel();
183.                      
184.                     cl.setClear=true;
185.                     double temp=Double.parseDouble(cl.displayLabel.getText());
186.                      
187.                     if(opText.equals("1/x"))
188.                     {
189.                     try
190.                     {double tempd=1/(double)temp;
191.                     cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}
192.                     catch(ArithmeticException excp)
193.                     {cl.displayLabel.setText("Divide by 0.");}
194.                     return;
195.                     }
196.                     if(opText.equals("sqrt"))
197.                     {
198.                     try
199.                     {double tempd=Math.sqrt(temp);
200.                     cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}
201.                     catch(ArithmeticException excp)
202.                     {cl.displayLabel.setText("Divide by 0.");}
203.                     return;
204.                     }
205.                     if(!opText.equals("="))
206.                     {
207.                     cl.number=temp;
208.                     cl.op=opText.charAt(0);
209.                     return;
210.                     }
211.                     // process = button pressed
212.                     switch(cl.op)
213.                     {
214.                     case '+':
215.                     temp+=cl.number;break;
216.                     case '-':
217.                     temp=cl.number-temp;break;
218.                     case '*':
219.                     temp*=cl.number;break;
220.                     case '%':
221.                     try{temp=cl.number%temp;}
222.                     catch(ArithmeticException excp)
223.                     {cl.displayLabel.setText("Divide by 0."); return;}
224.                     break;
225.                     case '/':
226.                     try{temp=cl.number/temp;}
227.                     catch(ArithmeticException excp)
228.                     {cl.displayLabel.setText("Divide by 0."); return;}
229.                     break;
230.                     }//switch
231.                      
232.                     cl.displayLabel.setText(MyCalculator.getFormattedText(temp));
233.                     //cl.number=temp;
234.                     }//actionPerformed
235.                     }//class
236.                      
237.                     /****************************************/
238.                      
239.                     class MyMemoryButton extends Button implements ActionListener
240.                     {
241.                     MyCalculator cl;
242.                      
243.                     /////////////////////////////////
244.                     MyMemoryButton(int x,int y, int width,int height,String cap, MyCalculator clc)
245.                     {
246.                     super(cap);
247.                     setBounds(x,y,width,height);
248.                     this.cl=clc;
249.                     this.cl.add(this);
250.                     addActionListener(this);
251.                     }
252.                     ////////////////////////////////////////////////
253.                     public void actionPerformed(ActionEvent ev)
254.                     {
255.                     char memop=((MyMemoryButton)ev.getSource()).getLabel().charAt(1);
256.                      
257.                     cl.setClear=true;
258.                     double temp=Double.parseDouble(cl.displayLabel.getText());
259.                      
260.                     switch(memop)
261.                     {
262.                     case 'C':
263.                     cl.memLabel.setText(" ");cl.memValue=0.0;break;
264.                     case 'R':
265.                     cl.displayLabel.setText(MyCalculator.getFormattedText(cl.memValue));break;
266.                     case 'S':
267.                     cl.memValue=0.0;
268.                     case '+':
269.                     cl.memValue+=Double.parseDouble(cl.displayLabel.getText());
270.                     if(cl.displayLabel.getText().equals("0") || cl.displayLabel.getText().equals("0.0")  )
271.                     cl.memLabel.setText(" ");
272.                     else
273.                     cl.memLabel.setText("M");
274.                     break;
275.                     }//switch
276.                     }//actionPerformed
277.                     }//class
278.                      
279.                     /*****************************************/
280.                      
281.                     class MySpecialButton extends Button implements ActionListener
282.                     {
283.                     MyCalculator cl;
284.                      
285.                     MySpecialButton(int x,int y, int width,int height,String cap, MyCalculator clc)
286.                     {
287.                     super(cap);
288.                     setBounds(x,y,width,height);
289.                     this.cl=clc;
290.                     this.cl.add(this);
291.                     addActionListener(this);
292.                     }
293.                     //////////////////////
294.                     static String backSpace(String s)
295.                     {
296.                     String Res="";
297.                     for(int i=0; i<s.length()-1; i++) Res+=s.charAt(i);
298.                     return Res;
299.                     }
300.                      
301.                     //////////////////////////////////////////////////////////
302.                     public void actionPerformed(ActionEvent ev)
303.                     {
304.                     String opText=((MySpecialButton)ev.getSource()).getLabel();
305.                     //check for backspace button
306.                     if(opText.equals("Backspc"))
307.                     {
308.                     String tempText=backSpace(cl.displayLabel.getText());
309.                     if(tempText.equals(""))
310.                     cl.displayLabel.setText("0");
311.                     else
312.                     cl.displayLabel.setText(tempText);
313.                     return;
314.                     }
315.                     //check for "C" button i.e. Reset
316.                     if(opText.equals("C"))
317.                     {
318.                     cl.number=0.0; cl.op=' '; cl.memValue=0.0;
319.                     cl.memLabel.setText(" ");
320.                     }
321.                      
322.                     //it must be CE button pressed
323.                     cl.displayLabel.setText("0");cl.setClear=true;
324.                     }//actionPerformed
325.                     }//class
326.                      
327.                     /*********************************************
328.                     Features not implemented and few bugs
329.                      
330.                     i)  No coding done for "+/-" button.
331.                     ii) Menubar is not included.
332.                     iii)Not for Scientific calculation
333.                     iv)Some of the computation may lead to unexpected result
334.                     due to the representation of Floating point numbers in computer
335.                     is an approximation to the given value that can be stored
336.                     physically in memory.
337.                     ***********************************************/




Post a comment

0 Comments