I am in the final stages of developing my NON-HOMEWORK algebra-solving application.
Here is the source code:
import java.io.*;
import java.util.*;
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class singleVariableSolver extends JPanel implements ActionListener {
public static void main (String[] args) {
//Setting Variables
String equation;
Float coefficient;
String afterVariable1;
int operatorCheck;
String afterVariable2;
String afterVariable3;
Float equals;
Float constant = null;
Float divisor;
Float solution;
String textSolution;
//Beginning of GUI portion
//Creating, showing, and laying out of frame
JFrame singleVariableFrame = new JFrame("Single Variable Equation Solver");
singleVariableFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
singleVariableFrame.setVisible(true);
singleVariableFrame.setLayout(null);
singleVariableFrame.setResizable(false);
//Content
JLabel equationLabel = new JLabel("Equation: ");
JLabel solutionLabel = new JLabel("Solution: ");
JTextField equationTextfield = new JTextField(15);
//Instructions
JLabel instructions1 = new JLabel("Please insert your equation that you want solved of");
JLabel instructions2 = new JLabel("the format '4x+2=10'. Please use 'x' for your variable.");
//Solve Button
JButton solveButton = new JButton("Solve!");
solveButton.setPreferredSize(new Dimension(100,30));
JTextArea solutionTextArea = new JTextArea("", 1, 15);
solutionTextArea.setEditable(false);
//Add components
singleVariableFrame.add(instructions1);
singleVariableFrame.add(instructions2);
singleVariableFrame.add(equationLabel);
singleVariableFrame.add(equationTextfield);
singleVariableFrame.add(solveButton);
singleVariableFrame.add(solutionLabel);
singleVariableFrame.add(solutionTextArea);
//Absolute Positioning
Insets insets = singleVariableFrame.getInsets();
Dimension size = instructions1.getPreferredSize();
size = instructions1.getPreferredSize();
instructions1.setBounds(130 + insets.left, 75 + insets.top, size.width, size.height);
size = instructions2.getPreferredSize();
instructions2.setBounds(128 + insets.left, 90 + insets.top, size.width, size.height);
size = equationLabel.getPreferredSize();
equationLabel.setBounds(150 + insets.left, 150 + insets.top, size.width, size.height);
size = equationTextfield.getPreferredSize();
equationTextfield.setBounds(210 + insets.left, 150 + insets.top, size.width, size.height);
size = solveButton.getPreferredSize();
solveButton.setBounds(240 + insets.left, 195 + insets.top, size.width, size.height);
size = solutionLabel.getPreferredSize();
solutionLabel.setBounds(150 + insets.left, 250 + insets.top, size.width, size.height);
size = solutionTextArea.getPreferredSize();
solutionTextArea.setBounds(213 + insets.left, 250 + insets.top, size.width, size.height);
//Setting size of frame
singleVariableFrame.setSize(600 + insets.left + insets.right,
400 + insets.top + insets.bottom);
singleVariableFrame.setVisible(true);
//Setting listener to button
solveButton.setActionCommand("solve");
solveButton.addActionListener(this);
//Setting button tip
solveButton.setToolTipText("Click this button to solve the equation.");
equation = "";
String[] brokenUp = equation.split("x");
coefficient = Float.parseFloat(brokenUp[0]);
afterVariable1 = brokenUp[1];
operatorCheck = equation.indexOf("+");
if (operatorCheck > 0) {
String[] brokenUp2 = afterVariable1.split("\\+");
afterVariable2 = brokenUp2[1];
String[] brokenUp3 = afterVariable2.split("=");
constant = Float.parseFloat(brokenUp3[0]);
}
else {}
operatorCheck = equation.indexOf("-");
if (operatorCheck > 0) {
String[] brokenUp2 = afterVariable1.split("-");
afterVariable2 = brokenUp2[1];
String[] brokenUp3 = afterVariable2.split("=");
constant = Float.parseFloat(brokenUp3[0]);
}
else {}
String[] brokenUp4 = equation.split("=");
equals = Float.parseFloat(brokenUp4[1]);
divisor = equals-constant;
solution = divisor/coefficient;
JOptionPane.showMessageDialog(null, "x = " + solution);
}
public void actionPerformed(ActionEvent e) {
if ("solve".equals(e.getActionCommand())) {
equation = equationTextfield.getText();
textSolution = String.valueOf(solution);
solutionTextArea.append(textSolution);
}
}
}
I think I have everything worked out, and if you have any questions as to what each thing does, just ask. But here are the errors I am getting:
singleVariableSolver.java:109: non-static variable this cannot be referenced fro
m a static context
solveButton.addActionListener(this);
^
singleVariableSolver.java:171: cannot find symbol
symbol : variable equation
location: class singleVariableSolver
equation = equationTextfield.getText();
^
singleVariableSolver.java:171: cannot find symbol
symbol : variable equationTextfield
location: class singleVariableSolver
equation = equationTextfield.getText();
^
singleVariableSolver.java:172: cannot find symbol
symbol : variable textSolution
location: class singleVariableSolver
textSolution = String.valueOf(solution);
^
singleVariableSolver.java:172: cannot find symbol
symbol : variable solution
location: class singleVariableSolver
textSolution = String.valueOf(solution);
^
singleVariableSolver.java:173: cannot find symbol
symbol : variable textSolution
location: class singleVariableSolver
solutionTextArea.append(textSolution);
^
singleVariableSolver.java:173: cannot find symbol
symbol : variable solutionTextArea
location: class singleVariableSolver
solutionTextArea.append(textSolution);
^
6 errors
I know why these last six errors are happening - they are happening because I need to reference exactly what I variables/fields I am using in this ActionPerformed method. But I don't know how to fix it. But the first error... I have no idea what to do.
Anyone have any ideas?
THIS IS NOT A HOMEWORK ASSIGNMENT!!!
This post has been edited by wolfman29: 25 June 2009 - 11:30 PM

New Topic/Question
Reply




MultiQuote





|