Question
I cant seem to figure out how to use the variable in my main method in another class outside of the main method. If i
I cant seem to figure out how to use the variable in my main method in another class outside of the main method. If i could grab the variables in the main method, i think i would be able to finish the assignment. Any insight or a better way of doing it would be appreciated. utilizing Java 8 and Netbeans
THIS IS THE ASSIGNMENT
Write A Java jFrame program that creates a calculator with
X Y and Z . where X and Y are inputs for two numbers and Z is an output for the answers.
Have the buttons + - * / alsohave it to fill in the x and y and based on what operation is pushed to the operation and put the answer in z
package icthomework3;
THIS IS MY CODE
import javax.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.JFrame;
public class IctHomework3 { // start public class Ict Homework 3
public static void main(String[] args) {// start main method String x1; String y1; double x; double y; double z; JFrame frame1 = new JFrame("CALCULATOR +-*/"); JPanel panel = new JPanel(); frame1.setBounds(400, 250, 400, 250); frame1.add(panel); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton addition = new JButton("+"); // Creating the JButton to display "+" JButton subtraction = new JButton("-");// Creating the JButton to display "-" JButton multiplication = new JButton("*");// Creating the JButton to display "*" JButton division = new JButton("/");// Creating the JButton to display "/" panel.add(addition); // Adding Jbutton1 to the JFrame panel.add(subtraction);// Adding Jbutton2 to the JFrame panel.add(multiplication);// Adding Jbutton3 to the JFrame panel.add(division);// Adding Jbutton3 to the JFrame x1 = JOptionPane.showInputDialog(null, "Please Enter first number: "); x = Double.parseDouble(x1); // parsing the string data into a double variable so that it can be used in arithmetic equations y1 = JOptionPane.showInputDialog(null, "Please Enter second number: "); y = Double.parseDouble(y1); // parsing the string data into a double variable so that it can be used in arithmetic equations panel.add(new JLabel("Please choose what you would like to do with the two numbers")); // Text Panel to show assignment name frame1.setVisible(true); addition.addActionListener(new ActionAddition());
}// End Main method
static class ActionAddition implements ActionListener {// Start Static Class
@Override public void actionPerformed(ActionEvent e) { JFrame frame2 = new JFrame("CALCULATOR +-*/"); frame2.setVisible(true); JPanel panel2 = new JPanel(); frame2.setBounds(400, 250, 400, 250); frame2.add(panel2); panel2.add(new JLabel("The answer is: ")); // Text Panel to show assignment name frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }//End static class
}//End public class icthomework 3
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started