?
Solved

java classes and methods

Posted on 2006-04-18
2
Medium Priority
?
177 Views
Last Modified: 2013-11-23
In our java application we have a class Andlogical and the code is...........


package com.rite.framework.condition;

import com.rite.framework.exception.SystemException;

public class AndLogical
    implements Logical
{

    public AndLogical(Logical lhsLogical, Logical rhsLogical)
    {
        this(lhsLogical, rhsLogical, false);
    }

    public AndLogical(Logical lhsLogical, Logical rhsLogical, boolean withBrackets)
    {
        logicals = (new Logical[] {
            lhsLogical, rhsLogical
        });
        this.withBrackets = withBrackets;
    }

    public AndLogical(Logical logicals[])
    {
        this(logicals, false);
    }

    public AndLogical(Logical logicals[], boolean withBrackets)
    {
        this.logicals = logicals;
        this.withBrackets = withBrackets;
    }

    public String evaluate()
        throws SystemException
    {
        String str = "";
        if(logicals != null)
        {
            for(int i = 0; i < logicals.length; i++)
            {
                Logical logical = logicals[i];
                if(logical != null)
                    str = str + logical.evaluate();
                if(i < logicals.length - 1)
                    str = str + " and ";
            }

        }
        if(withBrackets)
            str = "(" + str + ")";
        return str;
    }

    private Logical logicals[];
    private boolean withBrackets;
}



OrLogical Class .......................

package com.rite.framework.condition;

import com.rite.framework.exception.SystemException;

public class OrLogical
    implements Logical
{

    public OrLogical(Logical lhsLogical, Logical rhsLogical)
    {
        this(lhsLogical, rhsLogical, false);
    }

    public OrLogical(Logical lhsLogical, Logical rhsLogical, boolean withBrackets)
    {
        logicals = (new Logical[] {
            lhsLogical, rhsLogical
        });
        this.withBrackets = withBrackets;
    }

    public OrLogical(Logical logicals[])
    {
        this(logicals, false);
    }

    public OrLogical(Logical logicals[], boolean withBrackets)
    {
        this.logicals = logicals;
        this.withBrackets = withBrackets;
    }

    public String evaluate()
        throws SystemException
    {
        String str = "";
        if(logicals != null)
        {
            for(int i = 0; i < logicals.length; i++)
            {
                Logical logical = logicals[i];
                if(logical != null)
                    str = str + logical.evaluate();
                if(i < logicals.length - 1)
                    str = str + " or ";
            }

        }
        if(withBrackets)
            str = "(" + str + ")";
        return str;
    }

    private Logical logicals[];
    private boolean withBrackets;
}


and like this equalcondition,greaterthancondition,lessthancondition,likecondition,notlikecondition,notequalscondition  and other classes(implementing database query operators and conditions).
we are using these classes to generate sql queries(generic based on user input) for reporting purpose.

what's going on in the constructors,what is the functionality of this evaluate method,i m not getting these things
please explain and help.
thanks
0
Comment
Question by:riteinfotech
2 Comments
 
LVL 2

Accepted Solution

by:
gauravkrtomar earned 500 total points
ID: 16484753
I have tried to explain the code for AndLogical and for the OrLogical it is almost same. I think this will be helpfull.

package com.rite.framework.condition;

import com.rite.framework.exception.SystemException;

public class AndLogical
    implements Logical
{

    public AndLogical(Logical lhsLogical, Logical rhsLogical)
    {
        this(lhsLogical, rhsLogical, false);
      //calls AndLogical(Logical lhsLogical, Logical rhsLogical, boolean withBrackets)
    }

    public AndLogical(Logical lhsLogical, Logical rhsLogical, boolean withBrackets)
    {
        logicals = (new Logical[] {
            lhsLogical, rhsLogical
        });
      //creates a 2 element array and assigns the lhsLogical to logicals[0]
      //and rhsLogical to logicals[1]
        this.withBrackets = withBrackets;
    }

    public AndLogical(Logical logicals[])
    {
        this(logicals, false);
      //calls AndLogical(Logical logicals[], boolean withBrackets)
    }

    public AndLogical(Logical logicals[], boolean withBrackets)
    {
        this.logicals = logicals;
        this.withBrackets = withBrackets;
    }

    public String evaluate()
        throws SystemException
    {
        String str = "";
        if(logicals != null)//checks if the logical array is null
        {
            for(int i = 0; i < logicals.length; i++)//iterates till the length of the array
            {
                Logical logical = logicals[i];
                if(logical != null)
                    str = str + logical.evaluate();//uses a recursive call here
                                       //calls the corrsponding evaluate method of the Logical
                                       //till the logical does not contain any child logical element.
                if(i < logicals.length - 1)
                    str = str + " and ";
            }

        }
        if(withBrackets)
            str = "(" + str + ")";
        return str;
    }

    private Logical logicals[];
    private boolean withBrackets;
}



OrLogical Class .......................

package com.rite.framework.condition;

import com.rite.framework.exception.SystemException;

public class OrLogical
    implements Logical
{

    public OrLogical(Logical lhsLogical, Logical rhsLogical)
    {
        this(lhsLogical, rhsLogical, false);
    }

    public OrLogical(Logical lhsLogical, Logical rhsLogical, boolean withBrackets)
    {
        logicals = (new Logical[] {
            lhsLogical, rhsLogical
        });
        this.withBrackets = withBrackets;
    }

    public OrLogical(Logical logicals[])
    {
        this(logicals, false);
    }

    public OrLogical(Logical logicals[], boolean withBrackets)
    {
        this.logicals = logicals;
        this.withBrackets = withBrackets;
    }

    public String evaluate()
        throws SystemException
    {
        String str = "";
        if(logicals != null)
        {
            for(int i = 0; i < logicals.length; i++)
            {
                Logical logical = logicals[i];
                if(logical != null)
                    str = str + logical.evaluate();
                if(i < logicals.length - 1)
                    str = str + " or ";
            }

        }
        if(withBrackets)
            str = "(" + str + ")";
        return str;
    }

    private Logical logicals[];
    private boolean withBrackets;
}
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 500 total points
ID: 16484774
Depends upon what is defined in the logical.evaluate () method.

>> str = str + logical.evaluate();

Looks like some code to evaluate expressions. BTW, this is the way it is done:

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21347495.html#13524450
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question