Wednesday, June 10, 2009

Puzzle 32 – Double Trouble

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

This brings me to the last puzzle in this series. If you are wondering what I mean – it's just the way I write twisters - A series of 16 puzzles – next week we start with a new set of questions! I like to keep the best for the last and I believe this is the toughest puzzle on twister. Maybe, you don’t – lets get cracking shall we.

package com.twisters;
class DoubleTrouble{
public static void main(String args[])
{System.out.println(
"This is the wrong one!");}

public static void main(String args[])
{System.out.println(
"This is the right one!");}
}

Get this to print 'This is the right one!' when the program is run. Oh yes you need to get the code to compile first.

Let’s get to the challenging part shall we.

You cannot change anything about the main() which has the 'This is the wrong one!' print statement. The means no changes allowed to public static void main(String args[]){System.out.println("This is the wrong one!");}
It remains as it is - no changes, no additions, no deletions, no commenting it out - nothing!

Need a hint? One of the puzzles in April holds the solution to this puzzle (Be warned, it’s a complete spoiler). However if you are really stuck, you might want to have a look at the puzzles asked in April!!

Site of the day - http://www.facebook.com/careers/puzzles.php – puzzles on Facebook – isn’t that something we have been waiting for ;)!

Got an answer? Do leave it here.

14 comments:

  1. Here is my solution:

    class String {}


    class DoubleTrouble{
    public static void main(String args[])
    {System.out.println("This is the wrong one!");}

    public static void main(java.lang.String args[])
    {System.out.println("This is the right one!");}
    }

    By creating a String class, both the String args[] are referring to my String class. To get it to print the proper answer I used a full class name so the proper main() would be called.

    ReplyDelete
  2. class DoubleTrouble {

    public static class InnerClass {

    public static void main(String args[]) {
    System.out.println("This is the wrong one!");
    }
    }

    public static void main(String args[]) {
    System.out.println("This is the right one!");
    }
    }

    ReplyDelete
  3. package com.twisters;

    class String{}

    class DoubleTrouble{
    public static void main(String args[])
    {System.out.println("This is the wrong one!");}
    public static void main(java.lang.String args[])
    {System.out.println("This is the right one!");}
    }

    ReplyDelete
  4. class DoubleTrouble{
    static class DoubleTrouble1{
    public static void main(String args[])
    {System.out.println("This is the wrong one!");}
    }
    public static void main(String args[])
    {System.out.println("This is the right one!");}


    }

    ReplyDelete
  5. class DoubleTrouble {
    public static void main(String args[]) {
    System.out.println("This is the wrong one!");
    }
    public static void main(String args[][]) {
    System.out.println("This is the right one!");
    }
    }

    ReplyDelete
  6. package com.twisters;

    class DoubleTrouble {

    static class Inner {
    public static void main(String args[]) {
    System.out.println("This is the wrong one!");
    }
    }


    public static void main(String args[]) {
    System.out.println("This is the right one!");
    }
    }

    ReplyDelete
  7. public class DoubleTrouble {
    static class Test {
    public static void main(String args[]) {
    System.out.println("This is the wrong one!");
    }
    }

    public static void main(String args[]) {
    System.out.println("This is the right one!");
    }

    }

    ReplyDelete
  8. package com.twisters;
    class DoubleTrouble{
    public static void main(String args[])
    {System.out.println("This is the wrong one!");}

    public static void main(java.lang.String args[])
    {System.out.println("This is the right one!");}
    }

    class String{}

    The main principle here is to fool the program in order for it to believe the "wrong" main is not truly the original main method.

    ReplyDelete
  9. Hi,

    This looks pretty cool. Put the main() having "Wrong one" in an inner class like below

    package com.twisters;
    class DoubleTrouble
    {
    static class ragha{
    public static void main(String args[])
    {System.out.println("This is the wrong one!");}
    }
    public static void main(String args[])
    {System.out.println("This is the right one!");}
    }

    ReplyDelete
  10. package com.twisters;
    class DoubleTrouble{

    public static void main(String args[])
    {System.out.println("This is the wrong one!");}

    static
    {
    System.out.println("This is the right one!");
    System.exit(0);}
    }

    ReplyDelete
  11. package com.twisters;

    class DoubleTrouble{

    public static void main(String args[])
    {System.out.println("This is the wrong one!");}


    public static void main(java.lang.String args[])
    {System.out.println("This is the right one!");}

    class String{}
    }

    ReplyDelete
  12. package com.twisters;

    public class DoubleTrouble{

    static class Inner {

    public static void main(String args[])
    {System.out.println("This is the wrong one!");}

    }

    public static void main(String args[])
    {System.out.println("This is the right one!");}

    }

    ReplyDelete
  13. Wrap first main method into innner class.

    ReplyDelete
  14. Was thinking on more solutions but cudnt spare much time... leaving this one before sleeping as after that the time will b over ;D

    class DoubleTrouble {
    public static void main(String args[])
    {System.out.println("This is the wrong one!");}
    }

    public class Trouble {
    public static void main(String args[])
    {System.out.println("This is the right one!");}
    }

    ReplyDelete

Solution for this question?