Monday, October 12, 2009

Puzzle 55 – The No constructor dilemma.

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

This week’s puzzle is pretty simple and the question self explanatory (I hope).

package com.twisters;
public class NoConstructor {

boolean isConstructor = false; //No changes permitted to this line

/* No Code may be added or changed in main*/
public static void main(String[] args) {
NoConstructor noConstructor
= new NoConstructor();
System.out.println(noConstructor.isConstructor);
//Prints true
}
}

The NoConstructor class must not have any explicit constructor (that is - don’t use the word NoConstructor any more times in your solution). That's all - everything else about the puzzle should be self explanatory from the comments!

Got an answer? Leave one here

8 comments:

  1. package com.twisters;
    public class NoConstructor {

    boolean isConstructor = false; //No changes permitted to this line

    boolean isTrue = isConstructor = true;

    /* No Code may be added or changed in main*/
    public static void main(String[] args) {
    NoConstructor noConstructor = new NoConstructor();
    System.out.println(noConstructor.isConstructor); //Prints true
    }
    }

    ReplyDelete
  2. package com.twisters;

    public class NoConstructor {


    boolean isConstructor = false; // No changes permitted to this line

    {
    isConstructor = true;
    }


    /* No Code may be added or changed in main */
    public static void main(String[] args) {
    NoConstructor noConstructor = new NoConstructor();
    System.out.println(noConstructor.isConstructor); // Prints true
    }
    }

    ReplyDelete
  3. public class NoConstructor {
    boolean isConstructor = false; //No changes permitted to this line

    {isConstructor = true;}

    /* No Code may be added or changed in main*/
    public static void main(String[] args) {
    NoConstructor noConstructor = new NoConstructor();
    System.out.println(noConstructor.isConstructor); //Prints true
    }
    }

    Don't think I can get much shorter than one line...

    $ javac NoConstructor.java
    $ java NoConstructor
    true

    ReplyDelete
  4. package com.twisters;
    public class NoConstructor {

    boolean isConstructor = false; //No changes permitted to this line
    {isConstructor = true;} // Instance initializer.

    /* No Code may be added or changed in main*/
    public static void main(String[] args) {
    NoConstructor noConstructor = new NoConstructor();
    System.out.println(noConstructor.isConstructor); //Prints true
    }
    }

    ReplyDelete
  5. The "take the comments very literally" solution:

    package com.twisters;
    public class NoConstructor {
    /*
    boolean isConstructor = false; //No changes permitted to this line
    */
    boolean isConstructor = true;

    /* No Code may be added or changed in main*/
    public static void main(String[] args) {
    NoConstructor noConstructor = new NoConstructor();
    System.out.println(noConstructor.isConstructor); //Prints true
    }
    }

    Just because the line can't be changed doesn't mean that you're not allowed to change the line before and after it ;)

    ReplyDelete
  6. public class NoConstructor {
    boolean isConstructor = false;
    {isConstructor = true;}
    // or : {isConstructor ^= !isConstructor;}
    public static void main(String[] args){
    NoConstructor noConstructor = new NoConstructor();
    System.out.println(noConstructor.isConstructor);
    }
    }

    ReplyDelete
  7. boolean isConstructor = false; //No changes permitted to this line

    {
    isConstructor = true;
    }

    /* No Code may be added or changed in main*/
    public static void main(String[] args) {
    Twister noConstructor = new Twister();
    System.out.println(noConstructor.isConstructor); //Prints true
    }

    ReplyDelete

Solution for this question?