Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Homework help, don't understand teach's hint, need some direction

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Homework help, don't understand teach's hint, need some direction

    this is the assignment:

    10. Recursive Equation Evaluator
    Write a method that will evaluate an algebraic equation that is stored in a string and return its value. The string should contain the equation represented in text. The equation may contain floating point numeric values, the four basic algebraic operations ( +, -, * and / ), and sub expressions surrounded by parentheses. Evaluation should be performed using the standard rules of precedence:

    1. Aub expressions in parentheses are evaluated first from inner most to outermost.

    2. Within a sub expression or the expression, multiplication and division are evaluated next from left to right.

    3. Addition and subtraction are evaluated next from left to right.

    The following rules will help in guiding your programming:

    Expression ==> Term { '+' | '-' Term }

    Term ==> Factor { '*' | '/' Factor }

    Factor ==> Number | '(' Expression ')'

    In English, this would be read as:

    An Expression consists of a Term followed by zero or more sequences consisting of a plus or minus operator followed by another Term.

    A Term consists of a Factor followed by zero or more sequences consisting of a multiply or divide operator followed by another Factor.

    A Factor consists of a number or a sub-expression which consists of a left parenthesis followed by an Expression (see the definition above) and finally followed by a right parenthesis. The fact that a Factor can be composed of another Expression enclosed in parentheses is where the recursion comes in.

    You should also write a main program method that can be used to test your equation calculator. The main method should display a window with a text field for entering the equation, a calculate button, and a text field for displaying the answer. All calculations should be performed using either float or double values and arithmetic.

    /end assignment

    i've coded a fully functioning calculator with just straight iterative process and with shunting yard algorithm but i'm totally stumped by this. I don't have any code because its like gibberish to me. I need i guess 4 methods (main, expression, factor, and term) and i think all the math is done in the factor part. I just don't get it. ill post whatever i get done as i work through it but any help is appreciated.

    I'm starting out with this equation on paper to try and figure this out: 4+4/(4-3)
    this sucks...i don't know what to call first? its just a number and a plus for gods sake!

    thanks
    -crazy
    Last edited by crazed8s; December 8th, 2010 at 02:52 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Homework help, don't understand teach's hint, need some direction

    Though it may be slightly different, this other program of mine, though it didn't work fully on the toStirng(), might help.

    Evaluating Expressions

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Homework help, don't understand teach's hint, need some direction

    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
    Last edited by crazed8s; December 8th, 2010 at 07:54 PM.

Similar Threads

  1. Dont understand what to write.. :/
    By johnwalker in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 09:31 PM
  2. Direction,
    By Time in forum Algorithms & Recursion
    Replies: 2
    Last Post: May 21st, 2010, 06:21 PM
  3. Homework help
    By cd247 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2009, 06:56 PM
  4. need help with homework!
    By programmer12345 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 27th, 2009, 06:34 AM
  5. [SOLVED] What is cast operator and how to use it?
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 27th, 2009, 07:11 AM