This puzzle gave me a lot of trouble. I had a real hard time deciding which one to put here. Finally I decided to give this one a go. This one is tough – real tough, at least it was the first time I saw it. I never did get a solution – one of my friends pointed it out to me. So much for the story – I'll leave you with the hint 'Read carefully.'
package com.twisters;
public class Loaded {
public Loaded (){}
public static void main(String[] args) {
System.out.println(new Loaded().equals(new Loaded())) //Line 1
}
}
Get the System.out.prinltn() to print true at line 1.
You may add (but not delete) any additional amount of code - by the way did I mention that you are not allowed to Override the equals method :)!
Site for the day – http://www.topcoder.com/ - Programming contest site that gives cash awards.
Got an answer? Do leave it here.
public class Loaded {
ReplyDeletepublic Loaded() {}
public static void main(String[] args) {
System.out.println(!new Loaded().equals(new Loaded()));
}
}
1 package com.twisters;
ReplyDelete2 public class Loaded {
3 public Loaded() {}
4 public static void main(String[] args) {
5 System.out.println(true || new Loaded().equals(new Loaded()); // Line 1
6 }
7 }
This solution short circuits the boolean expression as you did not specify that Loaded.equals() returned true only that System.out.println() prints true.
P.S. Your restricted comment input (no pasting, arrow keys, etc) is very annoying. Especially when trying to copy code.
If I can't override the equals method, it will always print false. But !(false) = true, so if I just change the code like this:
ReplyDeletepackage com.twisters;
public class Loaded {
public Loaded (){}
public static void main(String[] args) {
System.out.println(!(new Loaded().equals(new Loaded()))); //Line 1
}
}
it will print false :D
I don't know if this was the solution you were looking for, but it serves the purpose of what is asked.
Cheers, Bernardo
I admit this is splitting symantecs but this could be done by overloading rather than overriding the equals method:
ReplyDeletepublic boolean equals(Loaded in) { return true; }
i don't know if this is the answer that you had in mind, but if we add a call to the getClass() method to each of the instanciations of the Loaded objects, we get a "true" printed.
ReplyDeleteThis was just from the top of my head. i'll keep thinking about this one to see if i can get a "true" to print, without messing with the System.out.println line :P
1 package com.twisters;
2 public class Loaded {
3 public Loaded (){}
4 public static void main(String[] args) {
5 System.out.println(new Loaded().getClass().equals(new Loaded().getClass())) //Line 1
6 }
7 }
GOT IT!!!!!
ReplyDeleteyou were serious when you said to read carefully.
we can not override but we can overload ;P
so i just overload the method "equals" with a new one, with signature public String equals(Loaded o) and it works.
Even the class name "Loaded" was a hint. This one was a thrill to work with :P
here's my second submission :P please ignore my first one, as i believe this second one is the one you are looking for:
package com.twisters;
public class Loaded{
public Loaded (){}
public static void main(String[] args) {
System.out.println( new Loaded().equals(new Loaded()) ); //Line 1
}
private String equals(Loaded o){
return "true";
}
}
This seemed to be the simplest :
ReplyDeleteReplace -
System.out.println(new Loaded().equals(new Loaded()));
with;
System.out.println(!new Loaded().equals(new Loaded()));
Not sure if this would satisfy all the criteria but I believe it does:
ReplyDeletepublic static void main(String[] args) {
System.out.println(new Loaded().getOne().equals(new Loaded().getOne()));
}
public String getOne() {
return "1";
}
1 package com.twisters;
ReplyDelete2 public class Loaded {
3 public Loaded (){}
4 public static void main(true || String[] args) {
5 System.out.println(new Loaded().equals(new Loaded())) //Line 1
6 }
7 }
or also (if we want to create the object)
1 package com.twisters;
2 public class Loaded {
3 public Loaded (){}
4 public static void main(String[] args) {
5 System.out.println(true | new Loaded().equals(new Loaded())) //Line 1
6 }
7 }
System.out.println(!(new Loaded().equals(new Loaded())));
ReplyDeleteSystem.out.println(new Loaded().equals(new Loaded()) || true);
ReplyDeleteWill always print true..
Add an overloaded equals method:
ReplyDeleteboolean equals(Loaded obj) {
return true;
}
Well the first thing you need to add is a semicolon to the end of line 5 :)
ReplyDeleteThe bold was a bit of a clue, you can solve this by overloading (not overriding) the equals() method to take a more specific class than Object:
package com.twisters;
public class Loaded {
public Loaded (){}
public static void main(String[] args) {
System.out.println(new Loaded().equals(new Loaded())); //Line 1
}
private boolean equals(Loaded other) {
return true;
}
}
public static void main(String[] args) {
ReplyDeleteSystem.out.println(!new Loaded().equals(new Loaded())); //Line 1
}
With a : !new Loaded().equals(new Loaded())
ReplyDeleteor a : extends ArrayList [or any other class with an overridden equals and a default constructor] (with an appropriate import)
System.out.println(!new Loaded().equals(new Loaded()));
ReplyDeleteSystem.out.println(new Loaded().equals(new Loaded()) | true);
ReplyDeletepublic class Loaded {
ReplyDeletepublic Loaded() {
}
{
System.out.println(true);
System.exit(1);
}
public static void main(String[] args) {
System.out.println(new Loaded().equals(new Loaded())); // Line 1
}
}