Wednesday, June 3, 2009

Puzzle 30 - The Namesake

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

It's time to get back to some real basics. The next question is pretty easy – have fun folks solving this quickee.

package com.twisters;
public class Namesake {
public static void main(String[] args) {
new Namesake().Namesake();
}
}

Get this code to compile and run. Add any amount of code you like but no deletes. Can you tell me the main principle involved in your solution?

Wanna challenge yourself on this one - come up with conceptually two different solutions for this problem?

Site of the day - http://www.xploremagic.com – Magic is always fun!

Edit - Updated this post - Looks like I has some problem with BlogTrog - the service I use to display formatted code. If you have left a solution already the principle remains the same - so old solution are still good!

8 comments:

  1. This is simplest I can think of ;D
    I've modified the lines of course but will try to find another solution as well


    public class Namesake {
    public static void main(String[] args) {
    new Q4().equals(new Q4());
    }
    }

    class Q4 {

    Q4() {
    System.out.println("Working?");
    }

    }

    ReplyDelete
  2. Solution 1:
    package com.twisters;
    public class Namesake {
    public static void main(String[] args) {
    new Namesake().equals(new Namesake());
    }
    }

    Solution 2:
    package com.twisters;
    public class Namesake {
    public static void main(String[] args) {
    new Namesake().Namesake();
    }
    private void Namesake() {}
    }

    I guess they are conceptually different though pretty basic!

    ReplyDelete
  3. solution 1 : declare a method (with return type) as Namesake.

    public class Namesake {
    public static void main(String[] args) {
    new Namesake().Namesake();
    }
    public void Namesake(){}
    }

    constructor doesnt have return type.

    solution 2 : - calling super class method

    public class Namesake extends superNamesake {
    public static void main(String[] args) {
    new Namesake().Namesake();
    }
    }
    class superNamesake {
    public void Namesake(){}
    }

    ReplyDelete
  4. package com.twisters;
    public class Namesake {
    public static void main(String[] args) {
    new Namesake().Namesake();
    }

    public Namesake() {}
    public void Namesake() {}
    }

    Class constructor and void method. Plain and simple =)

    ReplyDelete
  5. solution 1:
    public class Namesake {

    public static void main(String[] args) {
    new Namesake().aNamesake();
    }

    private void aNamesake(){};
    }

    solution 2:
    public class Namesake {

    public static void main(String[] args) {
    new Namesake().anyMethod(new Namesake());
    }

    private void anyMethod(Namesake ns){};
    }

    ReplyDelete
  6. ANSWER ONE:
    package com.twisters;

    public class Namesake {

    public void Namesake() {
    System.out.println("Ran a method with a constructor-like name.");
    }

    public static void main(String[] args) {
    new Namesake().Namesake();
    }
    }

    ANSWER TWO:
    package com.twisters;

    public class Namesake {

    public class NamesakeInner {

    public NamesakeInner() {
    System.out.println("Ran an inner constructor!");
    }
    }

    public Namesake() {
    System.out.println("Ran an outer constructor!");
    }

    public static void main(String[] args) {
    new Namesake().new NamesakeInner();
    }
    }

    ReplyDelete
  7. 1. public class Namesake {
    2.
    3. public Namesake(){}
    4. public void Namesake (){}
    5.
    6. public static void main (String args[]){
    7. new Namesake().Namesake();
    8. }
    9.
    10. }

    and also....

    1. public class Namesake {
    2.
    3. private static class namesake {
    4. public void Namesake()
    5. {}
    6. }
    7.
    8. public namesake Namesake(){
    9. return new namesake();
    10. }
    11.
    12. public static void main (String args[]){
    13. new Namesake().Namesake();
    14. }
    15.
    16. }

    lcf.

    ReplyDelete
  8. Greetings, what about this?

    1. public class Namesake {
    2.
    3. public Namesake(){}
    4. public void Namesake (){}
    5.
    6. public static void main (String args[]){
    7. new Namesake().Namesake();
    8. }
    9.
    10. }

    and also this?

    1. public class Namesake {
    2.
    3. private static class namesake {
    4. public void Namesake()
    5. {}
    6. }
    7.
    8. public namesake Namesake(){
    9. return new namesake();
    10. }
    11.
    12. public static void main (String args[]){
    13. new Namesake().Namesake();
    14. }
    15.
    16. }

    lcf.

    ReplyDelete

Solution for this question?