|
| 1 | +// Java program to create a simple calculator |
| 2 | +// with basic +, -, /, * using java swing elements |
| 3 | + |
| 4 | +import java.awt.event.*; |
| 5 | +import javax.swing.*; |
| 6 | +import java.awt.*; |
| 7 | +class calculator extends JFrame implements ActionListener { |
| 8 | + // create a frame |
| 9 | + static JFrame f; |
| 10 | + |
| 11 | + // create a textfield |
| 12 | + static JTextField l; |
| 13 | + |
| 14 | + // store oprerator and operands |
| 15 | + String s0, s1, s2; |
| 16 | + |
| 17 | + // default constrcutor |
| 18 | + calculator() |
| 19 | + { |
| 20 | + s0 = s1 = s2 = ""; |
| 21 | + } |
| 22 | +// main function |
| 23 | + public static void main(String args[]) |
| 24 | + { |
| 25 | + // create a frame |
| 26 | + f = new JFrame("calculator"); |
| 27 | + try { |
| 28 | + // set look and feel |
| 29 | + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
| 30 | + } |
| 31 | + catch (Exception e) { |
| 32 | + System.err.println(e.getMessage()); |
| 33 | + } |
| 34 | + // create a object of class |
| 35 | + calculator c = new calculator(); |
| 36 | + |
| 37 | + // create a textfield |
| 38 | + l = new JTextField(16); |
| 39 | + |
| 40 | + // set the textfield to non editable |
| 41 | + l.setEditable(false); |
| 42 | + |
| 43 | + // create number buttons and some operators |
| 44 | + JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bs, bd, bm, be, beq, beq1; |
| 45 | + // create number buttons |
| 46 | + b0 = new JButton("0"); |
| 47 | + b1 = new JButton("1"); |
| 48 | + b2 = new JButton("2"); |
| 49 | + b3 = new JButton("3"); |
| 50 | + b4 = new JButton("4"); |
| 51 | + b5 = new JButton("5"); |
| 52 | + b6 = new JButton("6"); |
| 53 | + b7 = new JButton("7"); |
| 54 | + b8 = new JButton("8"); |
| 55 | + b9 = new JButton("9"); |
| 56 | + |
| 57 | + // equals button |
| 58 | + beq1 = new JButton("="); |
| 59 | + |
| 60 | + // create operator buttons |
| 61 | + ba = new JButton("+"); |
| 62 | + bs = new JButton("-"); |
| 63 | + bd = new JButton("/"); |
| 64 | + bm = new JButton("*"); |
| 65 | + beq = new JButton("C"); |
| 66 | + |
| 67 | + // create . button |
| 68 | + be = new JButton("."); |
| 69 | + // create a panel |
| 70 | + JPanel p = new JPanel(); |
| 71 | + |
| 72 | + // add action listeners |
| 73 | + bm.addActionListener(c); |
| 74 | + bd.addActionListener(c); |
| 75 | + bs.addActionListener(c); |
| 76 | + ba.addActionListener(c); |
| 77 | + b9.addActionListener(c); |
| 78 | + b8.addActionListener(c); |
| 79 | + b7.addActionListener(c); |
| 80 | + b6.addActionListener(c); |
| 81 | + b5.addActionListener(c); |
| 82 | + b4.addActionListener(c); |
| 83 | + b3.addActionListener(c); |
| 84 | + b2.addActionListener(c); |
| 85 | + b1.addActionListener(c); |
| 86 | + b0.addActionListener(c); |
| 87 | + be.addActionListener(c); |
| 88 | + beq.addActionListener(c); |
| 89 | + beq1.addActionListener(c); |
| 90 | + |
| 91 | + // add elements to panel |
| 92 | + p.add(l); |
| 93 | + p.add(ba); |
| 94 | + p.add(b1); |
| 95 | + p.add(b2); |
| 96 | + p.add(b3); |
| 97 | + p.add(bs); |
| 98 | + p.add(b4); |
| 99 | + p.add(b5); |
| 100 | + p.add(b6); |
| 101 | + p.add(bm); |
| 102 | + p.add(b7); |
| 103 | + p.add(b8); |
| 104 | + p.add(b9); |
| 105 | + p.add(bd); |
| 106 | + p.add(be); |
| 107 | + p.add(b0); |
| 108 | + p.add(beq); |
| 109 | + p.add(beq1); |
| 110 | + |
| 111 | + // set Background of panel |
| 112 | + p.setBackground(Color.blue); |
| 113 | + |
| 114 | + // add panel to frame |
| 115 | + f.add(p); |
| 116 | + |
| 117 | + f.setSize(200, 220); |
| 118 | + f.show(); |
| 119 | + } |
| 120 | + public void actionPerformed(ActionEvent e) |
| 121 | + { |
| 122 | + String s = e.getActionCommand(); |
| 123 | + |
| 124 | + // if the value is a number |
| 125 | + if ((s.charAt(0) >= '0' && s.charAt(0) <= '9') || s.charAt(0) == '.') { |
| 126 | + // if operand is present then add to second no |
| 127 | + if (!s1.equals("")) |
| 128 | + s2 = s2 + s; |
| 129 | + else |
| 130 | + s0 = s0 + s; |
| 131 | + |
| 132 | + // set the value of text |
| 133 | + l.setText(s0 + s1 + s2); |
| 134 | + } |
| 135 | + else if (s.charAt(0) == 'C') { |
| 136 | + // clear the one letter |
| 137 | + s0 = s1 = s2 = ""; |
| 138 | + |
| 139 | + // set the value of text |
| 140 | + l.setText(s0 + s1 + s2); |
| 141 | + } |
| 142 | + else if (s.charAt(0) == '=') { |
| 143 | + double te; |
| 144 | + |
| 145 | + // store the value in 1st |
| 146 | + if (s1.equals("+")) |
| 147 | + te = (Double.parseDouble(s0) + Double.parseDouble(s2)); |
| 148 | + else if (s1.equals("-")) |
| 149 | + te = (Double.parseDouble(s0) - Double.parseDouble(s2)); |
| 150 | + else if (s1.equals("/")) |
| 151 | + te = (Double.parseDouble(s0) / Double.parseDouble(s2)); |
| 152 | + else |
| 153 | + te = (Double.parseDouble(s0) * Double.parseDouble(s2)); |
| 154 | + |
| 155 | + // set the value of text |
| 156 | + l.setText(s0 + s1 + s2 + "=" + te); |
| 157 | + |
| 158 | + // convert it to string |
| 159 | + s0 = Double.toString(te); |
| 160 | + |
| 161 | + s1 = s2 = ""; |
| 162 | + } |
| 163 | + else { |
| 164 | + // if there was no operand |
| 165 | + if (s1.equals("") || s2.equals("")) |
| 166 | + s1 = s; |
| 167 | + // else evaluate |
| 168 | + else { |
| 169 | + double te; |
| 170 | + |
| 171 | + // store the value in 1st |
| 172 | + if (s1.equals("+")) |
| 173 | + te = (Double.parseDouble(s0) + Double.parseDouble(s2)); |
| 174 | + else if (s1.equals("-")) |
| 175 | + te = (Double.parseDouble(s0) - Double.parseDouble(s2)); |
| 176 | + else if (s1.equals("/")) |
| 177 | + te = (Double.parseDouble(s0) / Double.parseDouble(s2)); |
| 178 | + else |
| 179 | + te = (Double.parseDouble(s0) * Double.parseDouble(s2)); |
| 180 | + // convert it to string |
| 181 | + s0 = Double.toString(te); |
| 182 | + |
| 183 | + // place the operator |
| 184 | + s1 = s; |
| 185 | + |
| 186 | + // make the operand blank |
| 187 | + s2 = ""; |
| 188 | + } |
| 189 | + // set the value of text |
| 190 | + l.setText(s0 + s1 + s2); |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments