Sunday, September 13, 2009

Puzzle 52 – "Satyameva Jayate" : Truth shall always prevail.

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

What is the minimum change (additions only – no deleting or commenting out code) that you need to make to the code so that the program prints true?

This should be easy – but it does highlight another peculiarity to look out for.

package com.twister; public class MyTruth { public static void main(String[] args) { boolean b = Boolean.getBoolean("false"); System.out.println(b); //This should print true } }

Got an answer? Do leave it
here.

17 comments:

  1. Minimal change is one symbol '!':
    Either
    boolean b = !Boolean.getBoolean("false");
    or
    System.out.println(!b);

    ReplyDelete
  2. 1 package com.twister;
    2 public class MyTruth {
    3 public static void main(String[] args) {
    4 boolean b = Boolean.getBoolean("false");
    5 System.out.println(b||true); //This should print true
    6 }
    7 }

    ReplyDelete
  3. public class MyTruth {

    public static void main(String[] args) {
    boolean b = !Boolean.getBoolean("false");
    System.out.println(b);
    }
    }

    ReplyDelete
  4. public class MyTruth {

    public static void main(String[] args) {
    boolean b = !Boolean.getBoolean("false");
    System.out.println(!b);
    }
    }

    ReplyDelete
  5. public class MyTruth {

    public static void main(String[] args) {
    boolean b = Boolean.getBoolean("false");
    System.out.println(!b);
    }
    }

    ReplyDelete
  6. Gerhard Balthasar: simply add ! before Boolean:

    public static void main(String[] args) {
    boolean b = !Boolean.getBoolean("false");
    System.out.println(b); // This should print true
    }

    ReplyDelete
  7. Actually no change is required to the program since the program asks for a system property named "false" and you can set that system property to "true" and run the program. It will then output "true":
    java -Dfalse=true com.twister.MyTruth

    However if by Boolean.getBoolean you mean Boolean.valueOf, adding a "!" can be used to negate the outcome, which is a one char addition that actually changes the logic.

    ReplyDelete
  8. We have two solutions (with the assumption that it's required to always print 'true'):
    1- (With no modifications at all to the code) Using command line when running the application:
    java -Dfalse=true com.twister.MyTruth

    2- (with one line addition - 36 characters) Changing the class to be:
    package com.twister;
    public class MyTruth {
    public static void main(String[] args) {
    System.setProperty("false", "true");
    boolean b = Boolean.getBoolean("false");
    System.out.println(b); //This should print true
    }
    }

    ReplyDelete
  9. Change:
    System.out.println(b);
    To:
    System.out.println(!b);

    P/S: How about my solution for Puzzle 51?

    ReplyDelete
  10. package com.twister;
    public class MyTruth {
    public static void main(String[] args) {
    boolean b = Boolean.getBoolean("false");
    System.out.println(!b);
    }
    }

    // Adding '! ' before b in the println statement solves this puzzle

    ReplyDelete
  11. package com.twister;
    public class MyTruth {
    public static void main(String[] args) {
    boolean b = Boolean.getBoolean("false");
    System.out.println(!b); //This should print true
    }
    }

    ReplyDelete
  12. 1 package com.twister;
    2 public class MyTruth {
    3 public static void main(String[] args) {
    4 boolean b = Boolean.getBoolean("false");
    5 System.out.println(b=true); //This should print true
    6 }
    7 }

    ReplyDelete
  13. boolean b = !Boolean.getBoolean("false");

    ReplyDelete
  14. I can think of two solutions

    1) boolean b = !Boolean.getBoolean("false");
    Only one character '!' is being used

    2) System.out.println(b=true);

    Cheers

    ReplyDelete

Solution for this question?