Wednesday, May 20, 2009

Puzzle 26 - No Overriding Please!!!

Language – Java | Type – Problem (Code) | Last date 24-May-2009 12:00 p.m. IST | Points 3

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.

18 comments:

  1. public class Loaded {

    public Loaded() {}

    public static void main(String[] args) {
    System.out.println(!new Loaded().equals(new Loaded()));
    }

    }

    ReplyDelete
  2. 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 }

    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.

    ReplyDelete
  3. 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:

    package 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

    ReplyDelete
  4. I admit this is splitting symantecs but this could be done by overloading rather than overriding the equals method:

    public boolean equals(Loaded in) { return true; }

    ReplyDelete
  5. 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.

    This 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 }

    ReplyDelete
  6. GOT IT!!!!!

    you 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";
    }
    }

    ReplyDelete
  7. This seemed to be the simplest :

    Replace -
    System.out.println(new Loaded().equals(new Loaded()));

    with;

    System.out.println(!new Loaded().equals(new Loaded()));

    ReplyDelete
  8. Not sure if this would satisfy all the criteria but I believe it does:


    public static void main(String[] args) {
    System.out.println(new Loaded().getOne().equals(new Loaded().getOne()));
    }

    public String getOne() {
    return "1";
    }

    ReplyDelete
  9. 1 package com.twisters;
    2 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 }

    ReplyDelete
  10. System.out.println(!(new Loaded().equals(new Loaded())));

    ReplyDelete
  11. System.out.println(new Loaded().equals(new Loaded()) || true);

    Will always print true..

    ReplyDelete
  12. Add an overloaded equals method:

    boolean equals(Loaded obj) {
    return true;
    }

    ReplyDelete
  13. Well the first thing you need to add is a semicolon to the end of line 5 :)

    The 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;
    }
    }

    ReplyDelete
  14. public static void main(String[] args) {

    System.out.println(!new Loaded().equals(new Loaded())); //Line 1

    }

    ReplyDelete
  15. With a : !new Loaded().equals(new Loaded())
    or a : extends ArrayList [or any other class with an overridden equals and a default constructor] (with an appropriate import)

    ReplyDelete
  16. System.out.println(!new Loaded().equals(new Loaded()));

    ReplyDelete
  17. System.out.println(new Loaded().equals(new Loaded()) | true);

    ReplyDelete
  18. public class Loaded {
    public Loaded() {
    }

    {
    System.out.println(true);
    System.exit(1);
    }

    public static void main(String[] args) {
    System.out.println(new Loaded().equals(new Loaded())); // Line 1
    }
    }

    ReplyDelete

Solution for this question?