Pradyut's profileObject Oriented Programm...PhotosBlogListsMore Tools Help

Blog


    December 30

    java code to evaluate a arithmetic expression....

    Java code to evaluate in arithmetic expression......

    following order...
    Division
    Multiplication
    Subtraction
    Addition

    The code: -

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    public class Main {

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            String a = "4-6-1+3*9/2"; //1*3+2*7-1-1
            String[] b = null;
            String c = null;
            //double[] d =null;
            Main obj = new Main();
            //double d1 = 0;
            System.out.println(obj.check(a));
            /*b = obj.mul (a);
            //a = b[0].substring(0, b[1].length()-1) + func2(func3(b[0].trim()), b[1]).toString() + b[]
            System.out.println(b[0].substring(obj.last(b[0])+1, b[0].length()));
            System.out.println(b[1].substring(0, obj.first(b[1])));
            c = obj.mult(b[0].substring(obj.last(b[0])+1, b[0].length()), b[1].substring(0, obj.first(b[1])));
            System.out.println(d1);
           
            a = b[0].substring(0, obj.last(b[0])+1) +d1 + b[1].substring( obj.first(b[1]), b[1].length());
            System.out.println("The string is: " + a);
           
           
            //System.out.print(func2(b[0].substring(0, func3(b[0].trim())), b[1].substring(func4(b[1].trim()), ));
            for(int i=0; i<b.length; i++)
                System.out.println(b[i]);
            
            //System.out.println(b[1].substring(0,b[1].indexOf('+')));
           
            //String f = b[1].substring(0,b[1].indexOf('+'));
           
            //d1 = obj.func2(b[0], f);
            System.out.println("The addition: " + d1);*/
        }
        double func(String a) {
            double d = 0;
           
            return d;
        }
        String mult(String a, String b) {
            double c = func1(a) * func1(b);       
            return (Double.toString(c));
        }
        String minus(String a, String b) {
           
            double c = func1(a) - func1(b);
            System.out.println(c);
            return (Double.toString(c));
        }
        String add(String a, String b) {
            double c = func1(a) + func1(b);
            return (Double.toString(c));
        }
       
        String divide(String a, String b) {
            double c = func1(a) / func1(b);
            return (Double.toString(c));
        }
       
        double func1(String a) {
            double d =0;
            try {
                d = Double.parseDouble(a);
            } catch (NumberFormatException n1) {
                System.out.println(n1);
            }
            return d;
        }
        /*String[] mul(String a) {
            String[] b = null;
            b = a.split("\\*");
            return b;
        }
        double mul1(String[] a) {
            double c = 0;
            try{
               
            } catch()
            return c;
        }*/
        /*int last(String a) {
            int c=0;
            char d;
            for(int i =a.length()-1; i>0; i=i-1) {
                if(a.charAt(i)=='+' || a.charAt(i)=='-' || a.charAt(i)=='*' || a.charAt(i)=='/')
                return i;
            }
            return c;
        }
        int first(String a) {
            int i=0;
            for(i =0; i<a.length(); ++i) {
                if(a.charAt(i)=='+' || a.charAt(i)=='-' || a.charAt(i)=='*' || a.charAt(i)=='/')
                return i;
            }
            return i;
        }*/
        String check(String a) {
            String[] b=null;
            if(a.contains("+")) {
                //System.out.println("This is inside minus");
                b = a.split("\\+",2);
               
                a=add(check1(b[0]),check(b[1])) ;
                return a;
            }
            else
                return check1(a);
        }

        private String check1(String a) {
           
            String[] b=null;
            //System.out.println(a);
            if(a.contains("-")) {
                //System.out.println("This is inside minus");
                /*if(a.indexOf('-')!=0) {
                    b = a.split("\\-",2);
                b[1] = "-" + b[1];
                a = add(check2(b[0]), check1(b[1])) ;
                }
                else {
                    a = add(check2(b[0]), b[1]) ;
                }*/
                if(a.indexOf('-')==0)
                {
                    a =a.substring(1);
                    if( a.contains("-")) {
                        b = a.split("-", 2);
                        b[0] = "-" + b[0];
                        b[1] = "-" + b[1];
                        a = add(check2(b[0]), check1(b[1]));
                    }
                    else
                        a = "-"+a;
                } else {
                    b = a.split("-", 2);
                    //b[0] = "-" + b[0];

                    b[1] = "-" + b[1];
                    a = add(check2(b[0]), check1(b[1]));
                } 
                return a;
            }else
                return check2(a);
            //throw new UnsupportedOperationException("Not yet implemented");
           
        }

        private String check2(String a) {
            String[] b=null;
            if(a.contains("*")) {
                b = a.split("\\*",2);
                a = mult(check3(b[0]), check2(b[1])) ;
                return a;
            }
               
            else
                return check3(a);
            //throw new UnsupportedOperationException("Not yet implemented");
        }

        private String check3(String a) {
            String[] b=null;
            if(a.contains("/")) {
                b = a.split("\\/");
                a = divide(b[0], check3(b[1])) ;
                return a;
            }
               
            else
                return a;
            //throw new UnsupportedOperationException("Not yet implemented");
        }
    }

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    Reply any comments or suggestions.......

    Thanks....

    --
    Pradyut
    http://pradyut.tk
    http://oop-edge.blogspot.com/
    http://pradyutb.blogspot.com/
    http://praddy-photos.blogspot.com/
    http://oop-edge.spaces.live.com/
    http://www.flickr.com/photos/praddy
    http://groups.google.com/group/oop_programming
    India
    December 14

    creating sql server JDBC pool

    The blog at:-

    http://oop-edge.blogspot.com/2007/12/creating-sql-server-jdbc-pool.html


    Creating Sql Server 2005 JDBC connection pool in Glassfish v2


    First we copy sqljdbc.jar file from the sql server jdbc driver distributed from Microsoft available here to the path \Program Files\glassfish-v2-b58g\lib.

    Then we open the admin console. The snapshot: -


    In resources->JDBC->Connection Pool

    The main thing here: -

    The name should not contain underscore or other special characters or it may not work

    DataSource className: - com.microsoft.sqlserver.jdbc.SQLServerXADataSource

    Resource Type: - javax.sql.XADataSource

    In the Additional Properties the parameters should be correrct: -


    check out the sql server 2005 port number from
    Sql server Configuration Manager->Sql Server 2005 network configuration->Protocols for SqlExpress. If the TCP/IP is disabled, enable it.

    Right click on TCP/IP -> properties

    In the IP addresses tab, in IPAll see the parameter for TCP Dynamic Ports.

    same should be in the portNumber in Glassfish server

    Save the settings and test the connection by Ping button on the page.

    create a JDBC resource using the JDBC connection pool: -


    creating a connection to the JDBC resource (from a web page) using the code: -

    try {
    Context ctx = new InitialContext();
    DataSource ds = (DataSource) ctx.lookup("test");
    Connection c = ds.getConnection();
    Statement st = c.createStatement();
    ResultSet rs = st.executeQuery("....");
    .............


    ...............

    c.close();
    } catch(NamingException e1) {
    label1.setText("Naming exception: " + e1);
    } catch (SQLException e2) {
    label1.setText("Sql exception: " + e2);
    }

    the imports should be: -

    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.sql.DataSource;

    reply your comments.... thanks


    --
    Pradyut
    http://pradyut.tk
    http://oop-edge.blogspot.com/
    http://pradyutb.blogspot.com/
    http://praddy-photos.blogspot.com/
    http://oop-edge.spaces.live.com/
    http://www.flickr.com/photos/praddy
    http://groups.google.com/group/oop_programming
    India