Question: this code is for my calculator code so it can work and im suppose to do this to alll my buttons but i dont know

this code is for my calculator code so it can work and im suppose to do this to alll my buttons but i dont know how to do that.

Objective: This assignment will give you practice with action event handling. The programming assignment: In this assignment you are to add functionality to the calculator created in lab 08. You have to make the calculator a full function calculator. Some programming hints:

1. Implement an action listener.

public void actionPerformed(ActionEvent e)

{ char c = e.getActionCommand().charAt(0);

switch(c)

{ case 0:

case 9: if (newNumber) { display c in the text field; newNumber = false;} else append c to the end of the text field; return;

case +: opnd1 = Double.parseDouble(tf.getText()); newNumber = true; operator = +; return;

case =: opnd2 = Double.parseDouble(tf.getText()); switch (operator) { case /: res = opnd1 / opnd2; break; case *: res = opnd1 * opnd2; break; case -: res = opnd1 - opnd2; break; case +: res = opnd1 + opnd2; break; }

Display res in the textfield; newNumber = true; return; } }

this is my code for my calculator

import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class JavaCalculator extends JFrame{ public JButton jbtNum1; public JButton jbtNum2; public JButton jbtNum3; public JButton jbtNum4; public JButton jbtNum5; public JButton jbtNum6; public JButton jbtNum7; public JButton jbtNum8; public JButton jbtNum9; public JButton jbtNum0; public JButton jbtEqual; public JButton jbtAdd; public JButton jbtSubtract; public JButton jbtMultiply; public JButton jbtDivide; public JButton jbtClear; public JButton jbtDecimal; public JTextField jtfResult; public javax.swing.JButton[] button = new javax.swing.JButton[18]; public JavaCalculator(){ super ("Calculator"); Container contentPane = getContentPane(); contentPane.setLayout(new GridLayout(0,1)); contentPane.add(new JLabel("CSc 20 Lab 08",JLabel.CENTER)); jtfResult = new JTextField("0.", 30); jtfResult.setHorizontalAlignment(JTextField.RIGHT); JPanel p2 = new JPanel(); p2.add(jtfResult); contentPane.add(p2); JPanel p3 = new JPanel(new GridLayout(0,4,2,2)); p3.add(jbtNum7 = new JButton("7")); p3.add(jbtNum8 = new JButton("8")); p3.add(jbtNum9 = new JButton("9")); p3.add(jbtDivide = new JButton("/")); contentPane.add(p3); JPanel p4 = new JPanel(new GridLayout(0,4,2,2)); p4.add(jbtNum4 = new JButton("4")); p4.add(jbtNum5 = new JButton("5")); p4.add(jbtNum6 = new JButton("6")); p4.add(jbtMultiply = new JButton("*")); contentPane.add(p4); JPanel p5 = new JPanel(new GridLayout(0,4,2,2)); p5.add(jbtNum1 = new JButton("1")); p5.add(jbtNum2 = new JButton("2")); p5.add(jbtNum3 = new JButton("3")); p5.add(jbtSubtract = new JButton("-")); contentPane.add(p5); JPanel p6 = new JPanel(new GridLayout(0,2,2,2)); p6.add(jbtNum0 = new JButton("0")); contentPane.add(p6); JPanel p7 = new JPanel(new GridLayout(0,2,2,2)); p7.add(jbtDecimal = new JButton(".")); p7.add(jbtAdd = new JButton("+")); p6.add(p7); contentPane.add(p6); JPanel p8 = new JPanel(new GridLayout(0,2,2,2)); p8.add(jbtEqual = new JButton("=")); p8.add(jbtClear = new JButton("C")); contentPane.add(p8); } public static void main(String[] args){ JavaCalculator calc = new JavaCalculator(); calc.pack(); calc.setSize(375, 250); calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); calc.setVisible(true); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!