Calculator Program in Java
Simple calculator program written in java for learning purpose..
Calculator.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | // Calculator.java import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; /** *This is a simple calculator program written in java. * *@author: Tapas Jena *@mail: tapas.friends@gmail.com */ public class Calculator extends JApplet { //initialization public void init() { CalculatorPanel calc = new CalculatorPanel(); getContentPane().add(calc); } } /* * This the class which actually makes the Calculator components and arrange them. */ class CalculatorPanel extends JPanel implements ActionListener { //Declare required components JButton n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,plus,minus,mul,div,dot, equal; static JTextField result = new JTextField("0", 45); static String lastCommand = null; JOptionPane p = new JOptionPane(); double preRes = 0, secVal = 0, res; private static void assign(String no) { if ((result.getText()).equals("0")) result.setText(no); else if (lastCommand == "=") { result.setText(no); lastCommand = null; } else result.setText(result.getText() + no); } //Default constructor public CalculatorPanel() { setLayout(new BorderLayout()); result.setEditable(false); result.setSize(300, 200); add(result, BorderLayout.NORTH); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(4, 4)); n7 = new JButton("7"); panel.add(n7); n7.addActionListener(this); n8 = new JButton("8"); panel.add(n8); n8.addActionListener(this); n9 = new JButton("9"); panel.add(n9); n9.addActionListener(this); div = new JButton("/"); panel.add(div); div.addActionListener(this); n4 = new JButton("4"); panel.add(n4); n4.addActionListener(this); n5 = new JButton("5"); panel.add(n5); n5.addActionListener(this); n6 = new JButton("6"); panel.add(n6); n6.addActionListener(this); mul = new JButton("*"); panel.add(mul); mul.addActionListener(this); n1 = new JButton("1"); panel.add(n1); n1.addActionListener(this); n2 = new JButton("2"); panel.add(n2); n2.addActionListener(this); n3 = new JButton("3"); panel.add(n3); n3.addActionListener(this); minus = new JButton("-"); panel.add(minus); minus.addActionListener(this); dot = new JButton("."); panel.add(dot); dot.addActionListener(this); n0 = new JButton("0"); panel.add(n0); n0.addActionListener(this); equal = new JButton("="); panel.add(equal); equal.addActionListener(this); plus = new JButton("+"); panel.add(plus); plus.addActionListener(this); add(panel, BorderLayout.CENTER); }//constructor end //Define Actions and calculation part public void actionPerformed(ActionEvent ae) { if (ae.getSource() == n1) assign("1"); else if (ae.getSource() == n2) assign("2"); else if (ae.getSource() == n3) assign("3"); else if (ae.getSource() == n4) assign("4"); else if (ae.getSource() == n5) assign("5"); else if (ae.getSource() == n6) assign("6"); else if (ae.getSource() == n7) assign("7"); else if (ae.getSource() == n8) assign("8"); else if (ae.getSource() == n9) assign("9"); else if (ae.getSource() == n0) assign("0"); else if (ae.getSource() == dot) { if (((result.getText()).indexOf(".")) == -1) result.setText(result.getText() + "."); }else if (ae.getSource() == minus) { preRes = Double.parseDouble(result.getText()); lastCommand = "-"; result.setText("0"); }else if (ae.getSource() == div) { preRes = Double.parseDouble(result.getText()); lastCommand = "/"; result.setText("0"); }else if (ae.getSource() == equal) { secVal = Double.parseDouble(result.getText()); if (lastCommand.equals("/")) res = preRes / secVal; else if (lastCommand.equals("*")) res = preRes * secVal; else if (lastCommand.equals("-")) res = preRes - secVal; else if (lastCommand.equals("+")) res = preRes + secVal; result.setText(" " + res); lastCommand = "="; }else if (ae.getSource() == mul) { preRes = Double.parseDouble(result.getText()); lastCommand = "*"; result.setText("0"); }else if (ae.getSource() == plus) { preRes = Double.parseDouble(result.getText()); lastCommand = "+"; result.setText("0"); } }//actionPerformed end } |