views:

42

answers:

4

Hi,

I started with the following code:

class Vereinfache2_edit {

    public static void main(String[] args) {

        int c1 = Integer.parseInt(args[0]);
        int c2 = Integer.parseInt(args[1]);
        int c3 = Integer.parseInt(args[2]);

        /* 1 */if (c2 - c1 == 0) {
            /* 2 */if (c1 != c3) {
                c3 += c1;
                /* 4 */System.out.println(c3);
                /* 5 */c3 *= c2;
                /* 6 */}
        }

        /* 7 */if (c1 == c3)
            /* 8 */if (c1 - c2 == 0)
            /* 9 */{
                c3 += c1;
                /* 10 */System.out.println(c3);
                /* 11 */c3 *= c1;
                /* 12 */if (c1 < c2)
                    c2 += 7;
                /* 13 */else
                    c2 += 5;
                /* 14 */}

        /* 15 */System.out.println(c1 + c2 + c3);
    }

} // end of class Vereinfache2

...and I ended with:

class Vereinfache2 { 

        public static void main(String [] args) {

           int c1 = Integer.parseInt(args[0]) ;
           int c2 = Integer.parseInt(args[1]) ;
           int c3 = Integer.parseInt(args[2]) ;

/*  1 */       
    /*  2 */        if (c2 == c1 && c1 != c3){  
    /*  4 */              System.out.println(c3 += c2) ; 
    /*  5 */              c3 = c3 * c2 ; 
    /*  6 */        }
/*  7 */      
    /*  8 */        if ( c2 == c1 && c1 == c3){
    /* 10 */            System.out.println(c3 *= 2) ; 
    /* 11 */            c3 = c3 * c2 ; c2 = c2 + 5 ; 
    /* 14 */        }


/* 15 */       System.out.println( c1+c2+c3) ;     
        }          

}  // end of class Vereinfache2

Do you see anything else like dead or switchable code?

Thanks for all answers. I ended up with this working version:

class Vereinfache2 { 

        public static void main(String [] args) {

           int c1 = Integer.parseInt(args[0]) ;
           int c2 = Integer.parseInt(args[1]) ;
           int c3 = Integer.parseInt(args[2]) ;

/*  1 */       if(c2 == c1){
    /*  2 */        if (c1 != c3){  
                        c3 += c2;
    /*  4 */            System.out.println(c3) ;          
    /*  6 */        }else{
                        c3 *= 2;
    /* 10 */            System.out.println(c3) ; 
    /* 14 */        }
                    c3 *= c2; c2 += 5;
               }

/* 15 */       System.out.println(c1+c2+c3) ;      
        }          

}  // end of class Vereinfache2
+1  A: 
/*  4 */              System.out.println(c3 += c2) ; 

should be

/*  4 */              System.out.println(c3 += c1) ; 

I believe, after looking at your original version. And here is my version.

public static void main(String[] args) {

    int c1 = Integer.parseInt(args[0]);
    int c2 = Integer.parseInt(args[1]);
    int c3 = Integer.parseInt(args[2]);

    if (c2 == c1) {
        c3 += c1;
        System.out.println(c3);
        if (c1 != c3) {
            c3 *= c2;
        } else {
            c3 *= c1;
            c2 += 5;
        }
        System.out.println(c1 + c2 + c3);
    }
}

IMO, its not a good idea to assign anything to anyone in sout.

Adeel Ansari
That is right, but I think it does not matter since c1 = c2
ArtWorkAD
+2  A: 

For your first version:

      if (c2 == c1) {
        if (c1 != c3) {
          c3 += c1;
          System.out.println(c3);
          c3 *= c2;
        } else {
          c3 += c1;
          System.out.println(c3);
          c3 *= c1;
          if (c1 < c2)
            c2 += 7;
          else
            c2 += 5;
        }
      } else if (c1 < c2)
          c2 += 7;
        else
          c2 += 5;
    }
    System.out.println(c1 + c2 + c3);
  }
}

and for the second version:

           if (c2 == c1)
              if( c1 != c3){  
                System.out.println(c3 += c2) ; 
                c3 = c3 * c2 ; 
              } else {
                System.out.println(c3 *= 2) ; 
                c3 = c3 * c2 ; c2 = c2 + 5 ; 
              }
            }          

This way you don't do the same test 2 times.

LucaB
@LucaB: Work on the original, improved one is screwed up a little. From where `(c3 *= 2)` thing came in, refer to the orignial.
Adeel Ansari
I see, I thougth when the OP said "...and I ended with:" assumed it was correct.
LucaB
it has the same output..what is incorrect there?
ArtWorkAD
I edited my answer with your first version code.
LucaB
@ArtWorkAD: you're missing some code in the second version. Is this wanted?
LucaB
yes, because the second version is minimized. I deleted some dead code. E.g. the if (c1 < c2) in the original is not necessary, because of if (c1 - c2 == 0) above it
ArtWorkAD
It's not necessary inside that if, of course. But do you want to add different quantities (5 or 7) upon that condition or you don't care?
LucaB
it is ok this way. I found another thing to minimize. c3 *= c2; c2 += 5; after the else {} statement and we can remove it in the if and else statement
ArtWorkAD
@LucaB - The above solution can be definitely optimized further..have a look at mine.
johnbk
You're right, but the OP said it has to use the second version.
LucaB
+2  A: 

What about this? you don't need to check for c1, c2 equality twice and you can avoid checking for c1,c3 equality once..

public static void main(String[] args) {

        int c1 = Integer.parseInt(args[0]);
        int c2 = Integer.parseInt(args[1]);
        int c3 = Integer.parseInt(args[2]);

        if (c2 == c1) {
        int c4 = c3 + c1;
        System.out.println(c4);
        if (c1 == c3) {
            c2 += 5;
        }
        c3 = c4 * c1;

    }

        System.out.println(c1 + c2 + c3);
    }

EDIT: Edited to match with the original version rather than with your ended up version.

johnbk
In what this is different from my answer?
LucaB
@LucaB: You showed it but didn't explain it in the start. :)
Adeel Ansari
@LucaB - ok..i didn't look at your answer while I posted this..and actually looked at it after i posted my answer....if I am not wrong you had 'else if (c1 == c3)' in your answer before... which you edited to simple else now and made your answer look like mine..and as @Adeel mentioned i threw some words there..
johnbk
@LucaB - btw, its not any different from your 'edited' answer now.
johnbk
I posted a "rapid" answer for the OP, and then "cleaned" it.
LucaB
@johnbk: You should have worked on the original, not the modified one. Its having logical error, and would not yield the same output as the original. This bug is not yours, its introduced by the questioner, though. Second thing is, you are not adding c1 to c3, nor printing c3 in your else part.
Adeel Ansari
@Adeel Ansari - Thanks..you are right..I was only looking at the @ArtWorkAD's final solution.
johnbk
+1  A: 

Use shorthands for c3 = c3 * c2;: c3 *= c2;

bancer