can someone help me fix my code? /* * To change this template, choose Tools | Te
ID: 3647227 • Letter: C
Question
can someone help me fix my code?/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package math;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.*;
import javax.swing.JFrame;
/**
*
* @author Owner
*/
public class Math {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
private JButton buttons[];
private String operation[] = {"Addition [+]","Subtraction [-]","Multiplication [x]","Division [/]"};
public Math() {
super("Math using JOptionPane");
setSize(320,120);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttons = new JButton[4];
Container pane = getContentPane();
setContentPane(pane);
GridLayout grid = new GridLayout(2,2);
pane.setLayout(grid);
for(int count=0; count<buttons.length; count++) {
buttons[count] = new JButton(operation[count]);
pane.add(buttons[count]);
}
buttons[0].addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
String input1, input2;
int num1, num2, result;
input1 = JOptionPane.showInputDialog("Please Input First Number: ");
input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
num1 = Integer.parseInt(input1);
num2 = Integer.parseInt(input2);
result = num1 + num2;
JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
} catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
);
buttons[1].addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
String input1, input2;
int num1, num2, result;
input1 = JOptionPane.showInputDialog("Please Input First Number: ");
input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
num1 = Integer.parseInt(input1);
num2 = Integer.parseInt(input2);
result = num1 - num2;
JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
} catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
);
//Implemeting Even-Listener on JButton button[2] which is multiplication
buttons[2].addActionListener(
new ActionListener() {
//Handle JButton event if it is clicked
public void actionPerformed(ActionEvent event) {
try {
String input1, input2;
int num1, num2, result;
input1 = JOptionPane.showInputDialog("Please Input First Number: ");
input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
num1 = Integer.parseInt(input1);
num2 = Integer.parseInt(input2);
result = num1 * num2;
JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
} catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
);
//Implemeting Even-Listener on JButton button[3] which is division
buttons[3].addActionListener(
new ActionListener() {
//Handle JButton event if it is clicked
public void actionPerformed(ActionEvent event) {
try {
String input1, input2;
int num1, num2, result;
input1 = JOptionPane.showInputDialog("Please Input First Number: ");
input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
num1 = Integer.parseInt(input1);
num2 = Integer.parseInt(input2);
result = num1 / num2;
JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
} catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
);
buttons[0].setMnemonic('A');
buttons[1].setMnemonic('S');
buttons[2].setMnemonic('M');
buttons[3].setMnemonic('D');
/**Set all the Components Visible.
* If it is set to "false", the components in the container will not be visible.
*/
setVisible(true);
setResizable(false); //Fix window height and width
}
public static void main (String[] args) {
Math pjtf = new Math();
}
}
Explanation / Answer
I have changed most of the errors i hope this works fine /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package math; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JOptionPane; import java.awt.*; import javax.swing.JFrame; /** * * @author Owner */ public class Math { /** * @param args the command line arguments */ public static void main(String[] args) { JButton buttons[]; String operation[] = {"Addition [+]","Subtraction [-]","Multiplication [x]","Division [/]"}; public Math(JButton buttons[]) { setTitle("Math using JOptionPane"); setSize(320,120); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); buttons = new JButton[4]; Container pane = getContentPane(); setContentPane(pane); GridLayout grid = new GridLayout(2,2); pane.setLayout(grid); String operation[] = {"Addition [+]","Subtraction [-]","Multiplication [x]","Division [/]"}; for(int count=0; countRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.