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.
b -> !b
ReplyDeleteMinimal change is one symbol '!':
ReplyDeleteEither
boolean b = !Boolean.getBoolean("false");
or
System.out.println(!b);
1 package com.twister;
ReplyDelete2 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 }
System.out.println(b^!b);
ReplyDeletepublic class MyTruth {
ReplyDeletepublic static void main(String[] args) {
boolean b = !Boolean.getBoolean("false");
System.out.println(b);
}
}
public class MyTruth {
ReplyDeletepublic static void main(String[] args) {
boolean b = !Boolean.getBoolean("false");
System.out.println(!b);
}
}
public class MyTruth {
ReplyDeletepublic static void main(String[] args) {
boolean b = Boolean.getBoolean("false");
System.out.println(!b);
}
}
Gerhard Balthasar: simply add ! before Boolean:
ReplyDeletepublic static void main(String[] args) {
boolean b = !Boolean.getBoolean("false");
System.out.println(b); // This should print true
}
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":
ReplyDeletejava -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.
We have two solutions (with the assumption that it's required to always print 'true'):
ReplyDelete1- (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
}
}
Change:
ReplyDeleteSystem.out.println(b);
To:
System.out.println(!b);
P/S: How about my solution for Puzzle 51?
package com.twister;
ReplyDeletepublic 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
System.out.println(!b);
ReplyDeletepackage com.twister;
ReplyDeletepublic class MyTruth {
public static void main(String[] args) {
boolean b = Boolean.getBoolean("false");
System.out.println(!b); //This should print true
}
}
1 package com.twister;
ReplyDelete2 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 }
boolean b = !Boolean.getBoolean("false");
ReplyDeleteI can think of two solutions
ReplyDelete1) boolean b = !Boolean.getBoolean("false");
Only one character '!' is being used
2) System.out.println(b=true);
Cheers