You are on page 1of 3

Calculator.

java

1 package pack;
2
3 // Java program to create a simple calculator
4 //with basic +, ‐, /, * using java swing elements
5 import java.awt.event.*;
6 import javax.swing.*;
7 import java.awt.*;
8 public class Calculator extends JFrame implements ActionListener {
9 /**
10 *
11 */
12 private static final long serialVersionUID = 1L;
13 // create a frame
14 static JFrame f;
15 // create a textfield
16 static JTextField l;
17 // store operator and operands
18 String s0, s1, s2;
19 // default constructor
20 Calculator()
21 {
22 s0 = s1 = s2 = "";
23 }
24 // main function
25 public static void main(String args[])
26 {
27 // create a frame
28 f = new JFrame("calculator");
29 try {
30 // set look and feel
31 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
32 }
33 catch (Exception e) {
34 System.err.println(e.getMessage());
35 }
36 // create a object of class
37 Calculator c = new Calculator();
38 // create a textfield
39 l = new JTextField(16);
40 // set the textfield to non editable
41 l.setEditable(false);
42 // create number buttons and some operators
43 JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bs, bd, bm, be, beq, beq1;
44 // create number buttons
45 b0 = new JButton("0");
46 b1 = new JButton("1");
47 b2 = new JButton("2");
48 b3 = new JButton("3");
49 b4 = new JButton("4");
50 b5 = new JButton("5");
51 b6 = new JButton("6");
52 b7 = new JButton("7");
53 b8 = new JButton("8");
54 b9 = new JButton("9");
55 // equals button
56 beq1 = new JButton("=");
57 // create operator buttons
58 ba = new JButton("+");
59 bs = new JButton("‐");
60 bd = new JButton("/");
61 bm = new JButton("*");

Page 1
Calculator.java

62 beq = new JButton("C");


63 // create . button
64 be = new JButton(".");
65 // create a panel
66 JPanel p = new JPanel();
67 // add action listeners
68 bm.addActionListener(c);
69 bd.addActionListener(c);
70 bs.addActionListener(c);
71 ba.addActionListener(c);
72 b9.addActionListener(c);
73 b8.addActionListener(c);
74 b7.addActionListener(c);
75 b6.addActionListener(c);
76 b5.addActionListener(c);
77 b4.addActionListener(c);
78 b3.addActionListener(c);
79 b2.addActionListener(c);
80 b1.addActionListener(c);
81 b0.addActionListener(c);
82 be.addActionListener(c);
83 beq.addActionListener(c);
84 beq1.addActionListener(c);
85 // add elements to panel
86 p.add(l);
87 p.add(ba);
88 p.add(b1);
89 p.add(b2);
90 p.add(b3);
91 p.add(bs);
92 p.add(b4);
93 p.add(b5);
94 p.add(b6);
95 p.add(bm);
96 p.add(b7);
97 p.add(b8);
98 p.add(b9);
99 p.add(bd);
100 p.add(be);
101 p.add(b0);
102 p.add(beq);
103 p.add(beq1);
104 // set Background of panel
105 p.setBackground(Color.blue);
106 // add panel to frame
107 f.add(p);
108 f.setSize(200, 220);
109 f.setVisible(true);;
110 }
111 public void actionPerformed(ActionEvent e)
112 {
113 String s = e.getActionCommand();
114 // if the value is a number
115 if ((s.charAt(0) >= '0' && s.charAt(0) <= '9') || s.charAt(0) == '.') {
116 // if operand is present then add to second no
117 if (!s1.equals(""))
118 s2 = s2 + s;
119 else
120 s0 = s0 + s;
121 // set the value of text
122 l.setText(s0 + s1 + s2);

Page 2
Calculator.java

123 }
124 else if (s.charAt(0) == 'C') {
125 // clear the one letter
126 s0 = s1 = s2 = "";
127 // set the value of text
128 l.setText(s0 + s1 + s2);
129 }
130 else if (s.charAt(0) == '=') {
131 double te;
132 // store the value in 1st
133 if (s1.equals("+"))
134 te = (Double.parseDouble(s0) + Double.parseDouble(s2));
135 else if (s1.equals("‐"))
136 te = (Double.parseDouble(s0) ‐ Double.parseDouble(s2));
137 else if (s1.equals("/"))
138 te = (Double.parseDouble(s0) / Double.parseDouble(s2));
139 else
140 te = (Double.parseDouble(s0) * Double.parseDouble(s2));
141 // set the value of text
142 l.setText(s0 + s1 + s2 + "=" + te);
143 // convert it to string
144 s0 = Double.toString(te);
145 s1 = s2 = "";
146 }
147 else {
148 // if there was no operand
149 if (s1.equals("") || s2.equals(""))
150 s1 = s;
151 // else evaluate
152 else {
153 double te;
154 // store the value in 1st
155 if (s1.equals("+"))
156 te = (Double.parseDouble(s0) + Double.parseDouble(s2));
157 else if (s1.equals("‐"))
158 te = (Double.parseDouble(s0) ‐ Double.parseDouble(s2));
159 else if (s1.equals("/"))
160 te = (Double.parseDouble(s0) / Double.parseDouble(s2));
161 else
162 te = (Double.parseDouble(s0) * Double.parseDouble(s2));
163 // convert it to string
164 s0 = Double.toString(te);
165 // place the operator
166 s1 = s;
167 // make the operand blank
168 s2 = "";
169 }
170 // set the value of text
171 l.setText(s0 + s1 + s2);
172 }
173 }
174 }
175

Page 3

You might also like