Sunday, October 4, 2009

Puzzle 54 – No More If’s

Language – Java | Type – Concept | Last date 11-Oct-2009 12:00 p.m. IST | Points 3

Today’s unearthly puzzle finds its roots in code written by a Jalvanian master. The question in today’s puzzle should be self explanatory.

package com.twister;
public class NoIf {

public static void main(String[] args) {
if(true){}
System.out.println(
"Print This");
if(true){}
System.out.println(
"Print This - Not!");
if(true){}
System.out.println(
"Print This");
}
}


It’s kind of pretty obvious what needs to be done – the code says it all.

You must of course follow the Three Jalvanian Laws.

1. You may only add but not delete nor comment out any of the code.

2. A real Jalvanian would never use an else so neither must you.

3. You may not use any more boolean variables (there are three in the code – which must remain unaltered) or conditions that evaluate to a boolean value. (So no true==false – since false is a boolean value etc).

That’s all folks!!

Got an answer? Do leave it here.

10 comments:

  1. public static void main(String[] args) {
    try{
    if(true){}
    System.out.println("Print This");
    }catch(Exception e){
    if(true){}
    System.out.println("Print This - Not!");
    }finally{
    if(true){}
    System.out.println("Print This");
    }
    }

    ReplyDelete
  2. if(true){}
    System.out.println("Print This");
    not:
    {
    if(true){break not;}
    System.out.println("Print This - Not!");
    }
    if(true){}
    System.out.println("Print This");

    ReplyDelete
  3. System.out.println("Print This - Not!".substring(0,10));

    ReplyDelete
  4. package com.twister;
    public class NoIf {

    public static void main(String[] args) {
    if(true){}
    System.out.println("Print This");

    try
    {
    if(true){throw new Exception();}
    System.out.println("Print This - Not!");
    }
    catch (Exception e) {
    }
    if(true){}
    System.out.println("Print This");
    }
    }

    ReplyDelete
  5. package com.twister;
    public class NoIf {

    public static void main(String[] args) {
    if(true){}
    System.out.println("Print This");

    try
    {
    if(true){throw new Exception();}
    System.out.println("Print This - Not!");
    }
    catch (Exception e) {
    }
    if(true){}
    System.out.println("Print This");
    }
    }

    ReplyDelete
  6. package com.twister;
    public class NoIf {

    public static void main(String[] args) {
    if(true){{}
    System.out.println("Print This");}
    if(!true){{}
    System.out.println("Print This - Not!");}
    if(true){{}
    System.out.println("Print This");}
    }
    }

    ReplyDelete
  7. package com.twister;
    public class NoIf {

    public static void main(String[] args) {
    if(true){}
    System.out.println("Print This");

    for(;;){
    if(true){break;}
    System.out.println("Print This - Not!");
    }
    if(true){}
    System.out.println("Print This");
    }
    }

    ReplyDelete
  8. package com.twister;
    public class NoIf {

    public static void main(String[] args) {
    if(true){}
    System.out.println("Print This");
    try {
    if(true){throw new RuntimeException()}
    System.out.println("Print This - Not!");
    } catch (RuntimeException e) {}
    if(true){}
    System.out.println("Print This");
    }
    }

    ReplyDelete
  9. package com.twister;
    public class NoIf {

    public static void main(String[] args) {
    if(true){}
    System.out.println("Print This");
    if(true){}
    try {}
    catch(Exception e) {
    System.out.println("Print This - Not!");
    }
    if(true){}
    System.out.println("Print This");
    }
    }

    ReplyDelete
  10. redirect the System.out before the second println with
    System.setOut() and undo it before the third

    ReplyDelete

Solution for this question?