Labels

Tuesday, January 3, 2012

Creating random expression JAVA

Hi friends,
I created a  random arbitrary expression using java for a small project. Its pretty simple to create absolutely random expressions using this java code.

except for the decimal format part all the other parts are simple and easily understandable. Decimal Format part is just used to round the double value only to 3 decimal digits.
 DecimalFormat df= new DecimalFormat("##.###"); 

GetEx() is the method that has being called from the main method and a String (arbitrary expression )will be passed back to the caller.

here is the Creating random arbitrary expression  java implementation


class RandomExp {

    private double  num1;
    private double num2;
    private char chara;
    
    public RandomExp() {
      num1=0.0;
      num2=0.0;
      chara=' ';
    }
    private String GetOper(){
        Random ran= new Random();
        int oper= ran.nextInt(4);
        
        switch(oper){
            case 0:
                chara='+';
                break;
            case 1:
                 chara='-';
                break;
            case 2:
                 chara='*';
                break;
            case 3:
                 chara='/';
                break;
                
        }
        return Character.toString(chara);
    }
    private  String GetInside(){
        
        String expression=null;
        Random ran=new Random();
        DecimalFormat df= new DecimalFormat("##.###");                
       
        num1= ((ran.nextDouble()*50)+10);
        num1=Double.parseDouble(df.format(num1));
       
        num2=((ran.nextDouble()*50)+10);
        num2=Double.parseDouble(df.format(num2));
        
        chara=GetOper().charAt(0);
        
        expression="("+Double.toString(num1)+chara+Double.toString(num2)+")";              
             
        return expression;
    }
    
    public String GetEx(){
        String expres="";
        String sNo1="";
        String sNo2="";
        Random ran=new Random();
        
        
        DecimalFormat df= new DecimalFormat("##.###");                
              
        int no= ran.nextInt(3);
        switch(no){
            case 0:
                num1=((ran.nextDouble()*50)+10);
                sNo1=(df.format(num1));
                break;
            case 1:
               sNo1=GetInside(); 
                break;
            case 2:
                sNo1=GetEx();
                break;
        }
        int no1= ran.nextInt(3);
        switch(no1){
            case 0:
                num2=((ran.nextDouble()*50)+10);
                sNo2=df.format(num2);
                break;
            case 1:
                sNo2=GetInside(); 
                break;
            case 2:
                sNo2=GetEx();
                break;
            
                    
        }
       chara=GetOper().charAt(0);
        expres="("+sNo1+chara+sNo2+")";
        
        return expres;
    }
    
}

4 comments: