Wednesday, July 22, 2009

Puzzle 44 – The One!

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

From where I come from everything is driven by statistic. Its no wonder that I am so much attached to number & tracking. I track the number of visitors on the blog - Google Analytics, Feed Subscribers, Twitter followers even my donate widget is one which shows how much progress I have made - at 0 right now :(!!
I sure would love any help to improve on any of these numbers ;)

The number that's pretty interesting though is the question rating. Its a pointer to the type of questions that you folks find interesting. I try to align questions to what you folks are looking for - in terms of type and difficulty though not always immediately.

Breaking out from my rant and getting to the puzzle at hand -

Have you ever had to code to silly standards. Well here is a typical construct!

package com.twisters;
public class ExtraLoad{
public static void main(String[] args) {
ExtraLoad ex
= new ExtraLoad();
ExtraLoad ex1
= new ExtraLoad("Hello");
ExtraLoad ex2
= new ExtraLoad("Hello", "World");
ExtraLoad ex3
= new ExtraLoad("Hello", "World", "Wide Web");
}
}


Get this code to compile – don't comment or delete any code.

Standard says -
You may not have more than one constructor in the code - Silly isn't it!

Sure you could rename the method to Extraload1(String), ExtraLoad2(String,String) but that would not be an interesting solution, would it? So here another clause - No changes to any part of main() method.

As usual looking forward to your solutions!!

24 comments:

  1. By Putting a Var-Arg Constructor of type String or Object, we can make this program get compiled! As it satisfies the rules of having only one constructor! It can be the solution for this puzzle.

    package com.twisters;
    public class ExtraLoad
    {
    ExtraLoad(String... x) // Just Introduce a Var-Args Constructor
    {


    }
    public static void main(String[] args)
    {
    ExtraLoad ex = new ExtraLoad();
    ExtraLoad ex1 = new ExtraLoad("Hello");
    ExtraLoad ex2 = new ExtraLoad("Hello", "World");
    ExtraLoad ex3 = new ExtraLoad("Hello", "World", "Wide Web");
    }
    }

    ReplyDelete
  2. @ the Moderator

    hi! I did send my solution by name of avinash for the puzzle 43. But i dont know why it was not considered. what makes my solution wrong from others?? It satisfies all the rules which u have given in the puzzle.

    ReplyDelete
  3. ExtraLoad(String...args)
    {
    for(int i=0;i&#60args.length;i++)
    System.out.print(args[i]+" ");
    System.out.println();
    }

    insert this code in class ExtraLoad
    nsureshreddy@live.com

    ReplyDelete
  4. ExtraLoad(String...args)
    {
    for(String a:args)
    System.out.print(a+" ");
    System.out.println();
    }

    //nsureshreddy@live.com

    ReplyDelete
  5. public class ExtraLoad {
    public ExtraLoad(String... string) {

    }

    public static void main(String[] args) {
    ExtraLoad ex = new ExtraLoad();
    ExtraLoad ex1 = new ExtraLoad("Hello");
    ExtraLoad ex2 = new ExtraLoad("Hello", "World");
    ExtraLoad ex3 = new ExtraLoad("Hello", "World", "Wide Web");
    }
    }

    ReplyDelete
  6. Use varargs as constructor paratemer.

    Boring puzzle. Its not even a puzzle, just some kind of test if person ever read any java book.

    Common, add some tricky puzzle!

    ReplyDelete
  7. public ExtraLoad(String... string){}

    ReplyDelete
  8. public ExtraLoad(String... s){};

    ReplyDelete
  9. public class ExtraLoad{
    public static void main(String[] args){
    ExtraLoad ex = new ExtraLoad();
    ExtraLoad ex1 = new ExtraLoad("Hello");
    ExtraLoad ex2 = new ExtraLoad("Hello", "World");
    ExtraLoad ex3 = new ExtraLoad("Hello", "World", "Wide Web");
    }

    public ExtraLoad(String... var){

    }

    }

    ReplyDelete
  10. hi
    We can write constructor having different parameters
    Also if u want we can initialize them using this keyword

    public class ExtraLoad{
    ExtraLoad()
    {

    }

    ExtraLoad(String str)
    {

    }
    ExtraLoad(String str ,String str1)
    {

    }

    ExtraLoad(String str ,String str1,String str2)
    {

    }


    public static void main(String[] args) {
    ExtraLoad ex = new ExtraLoad();
    ExtraLoad ex1 = new ExtraLoad("Hello");
    ExtraLoad ex2 = new ExtraLoad("Hello", "World");
    ExtraLoad ex3 = new ExtraLoad("Hello", "World", "Wide Web");
    }
    }

    ReplyDelete
  11. Add a constructor as:

    public ExtraLoad(String... args) {

    }

    ReplyDelete
  12. ExtraLoad(String...args)
    {
    for(String a:args)
    System.out.print(a+" ");
    System.out.println();
    }

    // insert this constructor in class ExtraLoad

    ReplyDelete
  13. public ExtraLoad(String... args) {}

    ReplyDelete
  14. Now that one was too easy... using varargs solves this "issue"...

    public ExtraLoad(String... args){}

    ReplyDelete
  15. package com.twisters;
    public class ExtraLoad{
    public static void main(String[] args) {
    ExtraLoad ex = new ExtraLoad();
    ExtraLoad ex1 = new ExtraLoad("Hello");
    ExtraLoad ex2 = new ExtraLoad("Hello", "World");
    ExtraLoad ex3 = new ExtraLoad("Hello", "World", "Wide Web");
    }

    public ExtraLoad(String... s){

    }
    }

    ReplyDelete
  16. Just add a constructor with a var-arg parameter :)
    ExtraLoad(String... args) {}

    ReplyDelete
  17. Is this what u really expected ?

    public class ExtraLoad{
    ExtraLoad(String... s)
    {
    }
    public static void main(String[] args) {
    ExtraLoad ex = new ExtraLoad();
    ExtraLoad ex1 = new ExtraLoad("Hello");
    ExtraLoad ex2 = new ExtraLoad("Hello", "World");
    ExtraLoad ex3 = new ExtraLoad("Hello", "World", "Wide Web");
    }
    }

    ReplyDelete
  18. Thanks Java 5 for your cool varargs construct.

    package com.twisters;
    public class ExtraLoad{
    public ExtraLoad(String...strings) {
    //do whatever here
    }
    public static void main(String[] args) {
    ExtraLoad ex = new ExtraLoad();
    ExtraLoad ex1 = new ExtraLoad("Hello");
    ExtraLoad ex2 = new ExtraLoad("Hello", "World");
    ExtraLoad ex3 = new ExtraLoad("Hello", "World", "Wide Web");
    }
    }

    ReplyDelete
  19. Add the following constructor:
    public ExtraLoad(String... args) {}

    ReplyDelete
  20. public ExtraLoad(String... var){
    }

    or

    public ExtraLoad(String[] var){
    }

    ReplyDelete
  21. public ExtraLoad(String... string) {
    }

    ReplyDelete
  22. package com.twisters;

    public class ExtraLoad {

      public ExtraLoad(String... args) {
        // ...
      }

      public static void main(String[] args) {
        ExtraLoad ex = new ExtraLoad();
        ExtraLoad ex1 = new ExtraLoad("Hello");
        ExtraLoad ex2 = new ExtraLoad("Hello", "World");
        ExtraLoad ex3 = new ExtraLoad("Hello", "World", "Wide Web");
      }
    }

    ReplyDelete
  23. Java 1.5 - Variable length arguments !

    package com.twisters;
    public class ExtraLoad {
    public ExtraLoad(String... args) {

    }
    public static void main(String[] args) {
    ExtraLoad ex = new ExtraLoad();
    ExtraLoad ex1 = new ExtraLoad("Hello");
    ExtraLoad ex2 = new ExtraLoad("Hello", "World");
    ExtraLoad ex3 = new ExtraLoad("Hello", "World", "Wide Web");
    }
    }

    ReplyDelete
  24. we can solve this puzzle by using the concept of varargs (variable arguments).


    package com.twisters;
    public class ExtraLoad{
    ExtraLoad(String... a){
    System.out.println("Hello this is the solution");
    }
    public static void main(String[] args) {
    ExtraLoad ex = new ExtraLoad();
    ExtraLoad ex1 = new ExtraLoad("Hello");
    ExtraLoad ex2 = new ExtraLoad("Hello", "World");
    ExtraLoad ex3 = new ExtraLoad("Hello", "World", "Wide Web");
    }
    }

    ReplyDelete

Solution for this question?