CalcApplet

Calculator Applet by: Tyler Brazill

--CalcApplet.java- import javax.swing.JApplet; import javax.swing.JPanel; import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.SwingConstants;

//author: Tyler Brazill public class CalcApplet extends JApplet{ public JLabel lblAnswer; public CalcApplet {

JPanel panel = new JPanel; getContentPane.add(panel, BorderLayout.CENTER); panel.setLayout(null);

lblAnswer = new JLabel("0"); lblAnswer.setHorizontalAlignment(SwingConstants.RIGHT); lblAnswer.setBounds(10, 31, 240, 24); panel.add(lblAnswer); CalcActionListener myListener = new CalcActionListener(lblAnswer);

JButton btn1 = new JButton("1"); btn1.addActionListener(myListener); btn1.setBounds(10, 66, 40, 40); panel.add(btn1);

JButton btn2 = new JButton("2"); btn2.addActionListener(myListener); btn2.setBounds(60, 66, 40, 40); panel.add(btn2);

JButton btn3 = new JButton("3"); btn3.addActionListener(myListener); btn3.setBounds(110, 66, 40, 40); panel.add(btn3);

JButton btn4 = new JButton("4"); btn4.addActionListener(myListener); btn4.setBounds(10, 117, 40, 40); panel.add(btn4);

JButton btn5 = new JButton("5"); btn5.addActionListener(myListener); btn5.setBounds(60, 117, 40, 40); panel.add(btn5);

JButton btn6 = new JButton("6"); btn6.addActionListener(myListener); btn6.setBounds(110, 117, 40, 40); panel.add(btn6);

JButton btn7 = new JButton("7"); btn7.addActionListener(myListener); btn7.setBounds(10, 168, 40, 40); panel.add(btn7);

JButton btn8 = new JButton("8"); btn8.addActionListener(myListener); btn8.setBounds(60, 168, 40, 40); panel.add(btn8);

JButton btn9 = new JButton("9"); btn9.addActionListener(myListener); btn9.setBounds(110, 168, 40, 40); panel.add(btn9);

JButton btn0 = new JButton("0"); btn0.addActionListener(myListener); btn0.setBounds(60, 219, 40, 40); panel.add(btn0);

JButton btnDecimal = new JButton("."); btnDecimal.addActionListener(myListener); btnDecimal.setBounds(10, 219, 40, 40); panel.add(btnDecimal);

JButton btnNeg = new JButton("(-)"); btnNeg.addActionListener(myListener); btnNeg.setBounds(110, 219, 40, 40); panel.add(btnNeg);

JButton btnEnter = new JButton("ENTER"); btnEnter.addActionListener(myListener); btnEnter.setBounds(160, 219, 90, 40); panel.add(btnEnter);

JButton btnAdd = new JButton("+"); btnAdd.addActionListener(myListener); btnAdd.setBounds(160, 66, 40, 40); panel.add(btnAdd);

JButton btnSub = new JButton("-"); btnSub.addActionListener(myListener); btnSub.setBounds(210, 66, 40, 40); panel.add(btnSub);

JButton btnMult = new JButton("*"); btnMult.addActionListener(myListener); btnMult.setBounds(160, 117, 40, 40); panel.add(btnMult);

JButton btnDivide = new JButton("/"); btnDivide.addActionListener(myListener); btnDivide.setBounds(210, 117, 40, 40); panel.add(btnDivide);

JButton btnClear = new JButton("CLEAR"); btnClear.addActionListener(myListener); btnClear.setBounds(160, 168, 90, 40); panel.add(btnClear);

} }

-CalcActionListener.java- import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JLabel;

//author: Tyler Brazill public class CalcActionListener implements ActionListener{ private double answer; public JLabel lblAnswer; private String numberString; private char lastSign; public CalcActionListener(JLabel lbl){ answer = 0; numberString = ""; lblAnswer = lbl; lastSign = '?'; }

@Override public void actionPerformed(ActionEvent e) { JButton ourButton = (JButton)e.getSource; char symbol = ourButton.getText.charAt(0);

switch(symbol){ case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0': case '.': case '(': if(symbol == '('){ numberString += '-'; }else{ numberString += symbol; } break; case '+': case '-': case '/': case '*': calcAnswer; lastSign = symbol; break; case 'E': calcAnswer; lastSign = '?'; break; case 'C': answer = 0; lastSign = '?'; numberString = ""; break; }

if(numberString.equals("")){ lblAnswer.setText(answer + ""); }else{ lblAnswer.setText(numberString); } } private void calcAnswer{ double num = Double.parseDouble(numberString); numberString = "";

switch(lastSign){ case '+': answer += num; break; case '-': answer -= num; break; case '*': answer *= num; break; case '/': answer /= num; break; case '?': answer = num; break; } } }