Results 1 to 2 of 2
  1. #1
    dacoolest is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Question recursive function to create many objects?

    I want to create a program to evaluate math expressions. First, the expression has to be split into small sections of strings while the split string contains a single value.

    A class is created that takes the whole expression as a string. And its recursive functions should check whether the selected string contains more than a single value. If then, they should be splitted again and assign to newly created similar objects.

    I have tried to create the class, string splitting part as well. But stuck at how to create more objects as the function is being re-executed? Thanks.

    here is the code;


    Java Code:
    package test3;
    
    /**
     *
     * @author 
     */
    public class ArithmeticExpression {
        
        String expression;
        char operator;
        String left;
        String right;
        double value;
        
        public ArithmeticExpression(String str) {
            
            boolean brackets = false;
            int opCount = 0;
            boolean bracketFound = false;
            char ch = ' ';
            int leftBracketPosition = 0;
            int rightBracketPosition = 0;
            
            int opIndex = 0;
            
            
            for (int i = 0; i < str.length(); i++)
            {
                
                // investiage the string...
                ch = str.charAt(i);
                if (ch == '(' && bracketFound != true)
                {
                    bracketFound = true;
                    leftBracketPosition = i;
                }
                else if (ch == ')')
                {
                    
                    rightBracketPosition = i;
                }
                if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
                {
                    
                    opCount++;
                }  
            }
            //decide the opIndex
            if (bracketFound == true)
            {
                int num = 0;
                //System.out.println(str.charAt(rightBracketPosition));
    
                left = str.substring(leftBracketPosition, rightBracketPosition+1);
                if (str.charAt(rightBracketPosition+1) == '+' || str.charAt(rightBracketPosition+1) == '-' || str.charAt(rightBracketPosition+1) == '*' || str.charAt(rightBracketPosition+1) == '/')
                {
                    opIndex = rightBracketPosition+2;
                    operator = str.charAt(rightBracketPosition+1);
                    right = str.substring(rightBracketPosition+2, str.length());
                }
                num = Character.getNumericValue(str.charAt(rightBracketPosition+1));
                if (num>=0 && num<=9)
                {
                    operator = '*';
                    right = str.substring(rightBracketPosition+1, str.length());
                }
            }
        }
        
        public boolean checkExpression(String str)
        {
            char ch = ' ';
            boolean status = true;
            for (int i = 0; i < str.length(); i ++)
            {
                ch = str.charAt(i);
                if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '(' || ch == ')')
                {
                    status = false;
                }
            }
            return status; 
        }
        
        public void evaluate()
        {
            String leftLeft;
            String leftRight;
            String rightLeft;
            String rightRight;
            
            int leftValue;
            int rightValue;
            
            if(checkExpression(left)==true & checkExpression(right) == true)
            {
                calculate();
            }
            else
            {
                if(checkExpression(left) == false)
                {
                    
                }
            }
        }
        
        public void calculate()
        {
            
        }
        
        public void display()
        {
            System.out.println(left);
            System.out.println(right);
            System.out.println(operator);
        }
        
        
    }

    Java Code:
    package test3;
    
    
    public class Test3 {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            ArithmeticExpression ar = new ArithmeticExpression("(2+1)2+5");
            ar.display();
        }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,355
    Blog Entries
    7
    Rep Power
    20

    Default Re: recursive function to create many objects?

    I wrote a blog article on expression parsing; maybe you like to read it (there's a "blog" button near the top of this page). The article also contains source code that works.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Urgent Help Java Recursive Function Please
    By khehrap in forum New To Java
    Replies: 19
    Last Post: 11-16-2011, 12:17 PM
  2. Time a recursive function
    By überfuzz in forum New To Java
    Replies: 1
    Last Post: 03-25-2011, 08:27 AM
  3. recursive function
    By jayden in forum New To Java
    Replies: 11
    Last Post: 09-02-2010, 03:00 PM
  4. Understanding this recursive function
    By LifeWithJava in forum New To Java
    Replies: 3
    Last Post: 12-30-2008, 05:26 AM
  5. Help with recursive function in java
    By cachi in forum Advanced Java
    Replies: 2
    Last Post: 07-31-2007, 06:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •