• Register

Calculator in Java with Source Code: We can develop calculator in java with the assistance of AWT/Swing with occurrence handling. Let us understand the code of making calculator in java.

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


 

2,760 points
7 4