Thank you for the response but i managed to get through it. atleast i think it works. It wasn't too too hard. i didn't use my teachers hints but is im pretty sure recursive so he should accept it.
heres the code if anyone wants to look at it, its pretty rough with no comments and i didn't make too great an effort to compact it so there may be a lot of redundancy:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class RecursionCalc extends JFrame {
//Swing components//
JPanel Panel;
JTextField Equation;
JButton Calculate;
JTextField Output;
//--------------//
//String and string track//
int get = 0;
String Eq;
//-------------//
RecursionCalc(){
//GUI//
Panel = new JPanel();
Equation = new JTextField(10);
Output = new JTextField(10);
Calculate = new JButton("Calculate");
Calculate.addActionListener(new CalculateListener());
Panel.add(Equation);
Panel.add(Calculate);
Panel.add(Output);
add(Panel);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//--------------//
}
//First runEq, this one does the math//
public double runEq(double first, String op){
double temp;//Stores temporary doubles, for passing back
String a = getNext();//Either a number or a (
if(a.equals("(")){
temp = runEq();//Calls the empty one because the parenthesis starts an entire sub equation
}
else{
temp = Double.parseDouble(a);//Otherwise gets its double value from a
}
if(get >= Eq.length()){//If its at the end then it has to do math because no more Operations
System.out.println("P3");
System.out.println("First = " + first);
System.out.println("Temp = " + temp);
//Do math
if(op.equals("+")){
return first + temp;
}
else if(op.equals("-")){
return first - temp;
}
else if(op.equals("*")){
return first * temp;
}
else{
return first / temp;
}
}
else{//Not at the end
String b = getNext();
if(b.equals(")")){//If it sees an end parenthesis its like the end of an equation except just a sub equation
System.out.println("P4");
System.out.println("First = " + first);
System.out.println("Temp = " + temp);
//Do math
if(op.equals("+")){
return first + temp;
}
else if(op.equals("-")){
return first - temp;
}
else if(op.equals("*")){
return first * temp;
}
else{
return first / temp;
}
}
else if(getPower(op) > getPower(b) || getPower(op) == getPower(b)){//if the first op is greater or equal does math of last number and first number from here
System.out.println("P5");
System.out.println("First = " + first);
System.out.println("Temp = " + temp);
System.out.println("b = " + b);
//Do math
if(op.equals("+")){
return runEq(first + temp, b);
}
else if(op.equals("-")){
return runEq(first - temp, b);
}
else if(op.equals("*")){
return runEq(first * temp, b);
}
else{
return runEq(first / temp, b);
}
}
else if(getPower(op) < getPower(b)){//If last op is less, it does math of last number and op with the next set
System.out.println("P6");
System.out.println("First = " + first);
System.out.println("temp = " + temp);
System.out.println("b = " + b);
//Do math
if(op.equals("+")){
return first + runEq(temp,b);
}
else if(op.equals("-")){
return first - runEq(temp,b);
}
else if(op.equals("*")){
return first * runEq(temp,b);
}
else{
return first / runEq(temp,b);
}
}
}
return 0;
}
public Double runEq(){//Only called at beginning or after parenthesis so it can't do any math
System.out.println("NEW STEP");
double temp;
String a = getNext();
if(a.equals('(')){//Start new subequation
System.out.println("P1");
temp = runEq();
}
else{//Temp gets turned straight to double
temp = Double.parseDouble(a);
}
String b = getNext();
System.out.println("P2");
System.out.println("Temp = " + temp);
System.out.println("b = " + b);
return runEq(temp, b);//Continue equation
}
public int getPower(String a){
if(a.equals("+") || a.equals("-")){//Just the power levels associated with different math operations
return 1;
}
else{
return 2;
}
}
public String getNext(){
String temp = "";
//Runs through string until not a number anymore
if(get<Eq.length()&&(Eq.charAt(get) < '0' || Eq.charAt(get) > '9') && Eq.charAt(get) != '.'){
temp = temp + Eq.charAt(get);
get++;
}
//The ops I'm working with are only 1 character long so this method works. Not particularly portable
else while(get<Eq.length()&&((Eq.charAt(get) >= '0' && Eq.charAt(get) <= '9')||Eq.charAt(get) == '.')){
temp = temp + Eq.charAt(get);
get++;
}
return temp;
}
public class CalculateListener implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
Eq = Equation.getText();//Get Equation
Output.setText(Double.toString(runEq()));//Messy function, starts equation, turns the final returned value to string, then outputs it
get = 0;//Reset
}
}
public static void main(String[] args) {
new RecursionCalc();//Start
}
}
edit: now it uses doubles
edit 2: added comments, and it need to be string literals