Sunday, September 6, 2009

Puzzle 51 – This and That!

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

Improving upon last week’s code, I came up with this:

package com.twister; public class Area { //should initialize to 0 - formula mentioned for documentation int area = length*width; //Instance variables get initialized to 0 int length; int width; public static void main(String[] args) { Area a = new Area(); //Do whatever needs to be done in main } }


Now while this is much better than the previous code (no hard coded values, self documenting), I seem to be having a problem with the code. As you might have already guessed this code does not compile!!!

Your challenge is pretty simple – get this code to compile by adding minimum number of characters to this code. Also a few additional conditions that you need to meet are -

a. You may only add code to get this to work – no deletes, no moving about the code, no commenting out any code! Additions only!

b. Obviously as before, declaring length and width as static would do the trick – but then that is so obvious that it can’t be the right solution, can it?
(So I am looking for something that solves the problem in less than 12 characters).

Got an answer? Do leave it here.

28 comments:

  1. declare inner class
    class A{
    int area = length * width;
    }

    ReplyDelete
  2. Change:
    int area = length*width;
    To:
    int area;// =length*width; -> 3 new characters

    ReplyDelete
  3. int area;String a= "length*width";

    ReplyDelete
  4. Area(){
    int area = length*width;
    }

    ReplyDelete
  5. package com.twister;
    public class Area {

    //should initialize to 0 - formula mentioned for documentation
    Area(){
    int area = length*width;
    }
    //Instance variables get initialized to 0
    int length;
    int width;

    public static void main(String[] args) {
    Area a = new Area();
    //Do whatever needs to be done in main
    }
    }

    8 characters added. My last (worst) solution helped me here

    ReplyDelete
  6. package com.twister;

    public class Area {
    int area = length=0 * (width=0);
    int length=10;
    int width=10;

    public static void main(String[] args) {
    Area a = new Area();
    }
    }

    ReplyDelete
  7. how about this?

    int area = (length=0) * (width=0);

    ReplyDelete
  8. int area = this.length*this.width; ??:)

    ReplyDelete
  9. Area(){int area = length*width;}
    wrapping the code in a default-constructor

    ReplyDelete
  10. public class Area {

    // should initialize to 0 - formula mentioned for documentation
    Area(){
    int area = length * width;
    }

    // Instance variables get initialized to 0

    int length;
    int width;



    public static void main(
    String[] args) {
    Area a = new Area();
    // Do whatever needs to be done in main
    }

    }

    ReplyDelete
  11. 8 characters: put code into constructor

    public class Area {

    //should initialize to 0 - formula mentioned for documentation
    Area(){int area = length*width;}

    //Instance variables get initialized to 0
    int length;
    int width;

    public static void main(String[] args) {
    Area a = new Area();
    //Do whatever needs to be done in main
    }
    }

    ReplyDelete
  12. change area declaration to:


    int area = this.length*this.width;

    ReplyDelete
  13. int area = (length=0)*(width=0);

    ReplyDelete
  14. is it correct to just comment line 6?
    "//" -> 2 chars :)

    ReplyDelete
  15. public class Area {

    //should initialize to 0 - formula mentioned for documentation
    class a{int area = length*width;}

    //Instance variables get initialized to 0
    int length;
    int width;

    public static void main(String[] args) {
    Area a = new Area();
    //Do whatever needs to be done in main
    }
    }

    ReplyDelete
  16. If it is just supposed to compile, following rules a and b (and not comments in code), surround "int area = length*width;" with

    void m(){int area=length*width;}

    10 extra characters.

    ReplyDelete
  17. int area = this.length*this.width;

    10 characters added

    P.S. I'm back from vacations :)

    ReplyDelete
  18. 1 package com.twister;
    2 //With the addition of only 7 characters
    //@Author -Manu Joseph, mailtomanu@gmail.com
    3 public class Area {
    4
    5 //should initialize to 0 - formula mentioned for documentation
    6 int area = length=0*(width=9);
    7
    8 //Instance variables get initialized to 0
    9 int length;
    10 int width;
    11
    12 public static void main(String[] args) {
    13 Area a = new Area();
    14 //Do whatever needs to be done in main
    15 }
    16 }

    ReplyDelete
  19. Adding this reference for the fields that are being used before defined, as follows:

    int area = this.length*this.width;
    // 10 chars added

    ReplyDelete
  20. Do the area calculation in a constructor?

    ReplyDelete
  21. package com.twister;

    public class Area {

    //should initialize to 0 - formula mentioned for documentation
    Area(){int area = length*width;}

    //Instance variables get initialized to 0
    int length;
    int width;

    public static void main(String[] args) {
    Area a = new Area();
    //Do whatever needs to be done in main
    }
    }

    ReplyDelete
  22. package com.twister;
    2
    3 public class Area {
    4
    5 //should initialize to 0 - formula mentioned for documentation
    6 int area = this.length*this.width;
    7
    8 //Instance variables get initialized to 0
    9 int length;
    10 int width;
    11
    12 public static void main(String[] args) {
    13 Area a = new Area();
    14 //Do whatever needs to be done in main
    15 }
    16 }

    ReplyDelete
  23. I believe I have done it in 9. I just wrapped the line in a function.

    void f(){
    int area = length * width;
    }

    The goal was just to get it to compile right?

    ReplyDelete
  24. Also putting "this." in front of both vars works. It's also 10 characters.. depending on how you count. Do you count whitespace?

    ReplyDelete
  25. We can convert the expression to a String. Assuming the ONLY goal is to make the code compile:


    public class Area {

    //should initialize to 0 - formula mentioned for documentation
    int area = "length*width".length();

    //Instance variables get initialized to 0
    int length;
    int width;

    public static void main(String[] args) {
    Area a = new Area();
    //Do whatever needs to be done in main
    }
    }

    ReplyDelete
  26. public class Area {

    //should initialize to 0 - formula mentioned for documentation
    void a(){int area = length*width;}

    //Instance variables get initialized to 0
    int length;
    int width;

    public static void main(String[] args) {
    Area a = new Area();
    //Do whatever needs to be done in main
    }
    }

    ReplyDelete
  27. // 11 characters
    int area = "length*width".length();

    ReplyDelete
  28. int area = (length=0)*(width=0);
    //from previous soln by someone

    ReplyDelete

Solution for this question?