Calculator
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
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