Sunday, August 30, 2009

Puzzle 50 – Before & After

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

Do you remember the first time you wrote some java code and were stuck on a really silly thing for hours? Well this puzzle is right up that alley,

package com.twister; public class Area { int length = 10; int area = length*breath; int breath = 20; public static void main(String[] args) { Area a = new Area(); System.out.println(a.area); } }

Apart for being a really silly piece of code with all values ‘hard coded’ – as you might have already guessed this code fails to compile.
The puzzle - what is the minimum code addition (no deletes, no moving the code – plain addition only) that is needed to make this code compile and run to give the expected output – 200?

Got an answer? Do leave it here.

55 comments:

  1. "int area = length * breath;static" - 6 new characters

    ReplyDelete
  2. "int area = length * 20;//breath;" - 5 new characters

    ReplyDelete
  3. public class Area {
    static final int length = 10;
    static final int area = length*Area.breath;
    static final int breath = 20;

    public static void main(String[] args) {
    Area a = new Area();
    System.out.println(a.area);
    }
    }

    ReplyDelete
  4. Line 6:

    int area = 200;//length*breath;

    "200;//" - 6 char addition

    ReplyDelete
  5. package com.twister;

    public class Area
    {
    int length = 10;
    int area = length*breath;
    static int breath = 20;

    public static void main(String[] args) {
    Area a = new Area();
    System.out.println(a.area);
    }
    }

    ReplyDelete
  6. Make a "static" declaration for variable "breath".

    package com.twister;

    public class Area {
    int length = 10;
    int area = length*breath;
    static int breath = 20;
    public static void main(String[] args) {
    Area a = new Area();
    System.out.println(a.area);
    }
    }

    ReplyDelete
  7. Two solutions for me:

    First, make breath, so it becomes visible earlier:

    package com.twister;

    public class Area {

    int length = 10;
    int area = length*breath;static
    int breath = 20;

    public static void main(String[] args) {
    Area a = new Area();
    System.out.println(a.area);
    }
    }

    6 characters added: 'static'.


    Another one: don't use the breath variable. More values hard coded, but prints 200 :-)

    public class Area {

    int length = 10;
    int area = length*20;//breath;
    int breath = 20;

    public static void main(String[] args) {
    Area a = new Area();
    System.out.println(a.area);
    }
    }

    5 characters added: '20;//'.

    ReplyDelete
  8. Gerhard Balthasar: adding the breath variable to a static inner class allows access with addition of 25 chars (may not be best solution, buts works).

    public class Area {

    int length = 10;
    int area = length*_.breath;

    static class _ {
    static int breath = 20;
    }

    public static void main(String[] args) {
    Area a = new Area();
    System.out.println(a.area);
    }
    }

    ReplyDelete
  9. Make breath static! Adding the static modifier is enough because static initializers run before instance initializers and thus eliminate the forward reference.

    ReplyDelete
  10. change int breath = 20; -> static int breath = 20;

    ReplyDelete
  11. package com.twister;

    public class Area {

    int length = 10;
    int breath = 20;
    int area = length*breath;
    int breath1 = 20;

    public static void main(String[] args) {
    Area a = new Area();
    System.out.println(a.area);
    }
    }

    ReplyDelete
  12. public class Area {

    int length = 10;
    int area(){
    int area = length*breath;
    return area;
    }
    int breath = 20;

    public static void main(String[] args) {
    Area a = new Area();
    System.out.println(a.area());
    }
    }


    26 characters added.
    Will try for better solution.

    ReplyDelete
  13. Pretty cool teaser ;)

    I just solved it in 5 additional lines of code, but I don't want to ruin it for others by posting my answer here.

    Can anyone do it in less than 5 lines of code?

    ReplyDelete
  14. Ok... Here is the solution - add a new line with one word 'static' after line 6. I doubt anyone will be able to do it in less than 1 line :)

    ReplyDelete
  15. static int breath = 20;

    ReplyDelete
  16. Gerhard Balthasar: En even easier fix would be to make the field 'int breath = 20;' static. Addition of just 6 characters.

    ReplyDelete
  17. package com.twister;

    public class Area {

    int length=10, breath1=20;
    int area=length*breath1;
    int breath=20;

    public static void main(String[] args) {
    Area a = new Area();
    System.out.println(a.area);
    }
    }

    ReplyDelete
  18. package com.twister;

    public class Area {

    int length = 10;
    int area = length*breath;
    static int breath = 20;

    public static void main(String[] args) {
    Area a = new Area();
    System.out.println(a.area);
    }
    }

    ReplyDelete
  19. package com.twister;
    public class Area {
    int length = 10;
    int area = length*breath;
    static int breath = 20;
    public static void main(String[] args) {
    Area a = new Area(); System.out.println(a.area);
    }
    }

    ReplyDelete
  20. Change to static int breadth = 20;

    Static variables are initialised first and so the program will run properly.

    ReplyDelete
  21. public class Test {

    int length = 10;
    int area = length*20;//*breather;
    int breath = 20;

    public static void main(String[] args) {
    Test a = new Test();
    System.out.println(a.area);
    }
    }

    ReplyDelete
  22. line 7, add "static" before int breath = 20;

    ReplyDelete
  23. Ugly, unreadable and I even did not understood why içt was working :-)

    package com.twister;

    public class Area {
    int length = 10;
    int area = length*(breath=20);
    int breath = 20;
    public static void main(String[] args) {
    Area a = new Area();
    System.out.println(a.area);
    }
    }

    ReplyDelete
  24. Ugly, unreadable !!!
    I did not even understood why it was working :-)

    package com.twister;

    public class Area {
    int length = 10;
    int area = length*(breath=20);
    int breath = 20;
    public static void main(String[] args) {
    Area a = new Area();
    System.out.println(a.area);
    }
    }

    ReplyDelete
  25. package com.twister;

    public class Area {
    int length = 10;
    int area = length * breath;
    static int breath = 20;

    public static void main(String[] args) {
    Area a = new Area();
    System.out.println(a.area);
    }
    }

    ReplyDelete
  26. Breath needs a value before you use it.

    ReplyDelete
  27. Just stick make breath static (or even better breadth ;-) )

    ReplyDelete
  28. Make breath a static field

    ReplyDelete
  29. add "static" @ begin of line 7

    ReplyDelete
  30. line 11
    System.out.println(a.area*20);

    3chars :P

    ReplyDelete
  31. int breatha = 20;
    int area = length * breatha;

    ReplyDelete
  32. int area = length*breath + 200;

    ReplyDelete
  33. Make 'breath' static

    public class Area {

    int length = 10;
    int area = length * breath;
    static int breath = 20;

    public static void main(final String[] args) {
    Area a = new Area();
    System.out.println(a.area);
    }
    }

    ReplyDelete
  34. Make the breadth instance variable static.
    public class Area {

    int length = 10;
    int area = length*breath;
    static int breath = 20;

    public static void main(String[] args) {
    Area a = new Area();
    System.out.println(a.area);
    }
    }

    ReplyDelete
  35. use static for breadth

    ReplyDelete
  36. Just move the area variable declaration below the breadth variable declaration. Btw, "breath" is spelled "breadth".

    ReplyDelete
  37. replace line 6 with:

    int area = 200;//length*breath;

    ReplyDelete
  38. Add 'static' before int breath = 20;

    ReplyDelete
  39. Add "static" before "int breath":

    static int breath = 20;

    Complete Code:

    package com.twister;

    public class Area {

    int length = 10;
    int area = length*breath;
    static int breath = 20;

    public static void main(String[] args) {
    Area a = new Area();
    System.out.println(a.area);
    }
    }

    ReplyDelete
  40. 1 package com.twister;
    2
    3 public class Area {
    4
    5 int length = 10;
    6 int breadth = 20;
    7 int area = length*breadth;
    8 int breath = 20;
    9
    10 public static void main(String[] args) {
    11 Area a = new Area();
    12 System.out.println(a.area);
    13 }
    14 }

    ReplyDelete
  41. Make breath static.

    ReplyDelete
  42. make least breath static.

    static int breath = 20;

    ReplyDelete
  43. public class Area {

    int length = 10;
    int area = length*breath;
    static int breath = 20;

    public static void main(String[] args) {
    Area a = new Area();
    System.out.println(a.area);
    }
    }

    ReplyDelete
  44. public class Area {

    int length = 10;
    int area = length*breath;
    static int breath = 20;

    public static void main(String[] args) {
    Area a = new Area();
    System.out.println(a.area);
    }
    }

    ReplyDelete
  45. change:
    int breath = 20;
    to:
    static int breath = 20;

    ReplyDelete
  46. static int breath = 20;

    ReplyDelete
  47. 1 package com.twister;
    2
    3 public class Area {
    4
    5 int length = 10;
    7 int breath = 20;
    6 int area = length*breath;
    8
    9 public static void main(String[] args) {
    10 Area a = new Area();
    11 System.out.println(a.area);
    12 }
    13 }

    ReplyDelete
  48. package com.twister;
    /**
    * Twister 50
    *
    * @author beders
    *
    */
    public class Area {

    int length = 10;
    int area = length * breath;
    static int breath = 20;

    public static void main(String[] args) {
    Area a = new Area();
    System.out.println(a.area);
    }
    }

    ReplyDelete
  49. make 'length' and 'breath' to be static.

    ReplyDelete
  50. package com.twister;

    public class Area {
    int length = 10;
    int area = length * breath;
    static int breath = 20;

    public static void main(String[] args) {
    Area a = new Area();
    System.out.println(a.area);
    }
    }

    ReplyDelete
  51. int area = length * /*breath;
    int breath = */20;

    I just added 4 characters
    is it wrong?

    ReplyDelete
  52. I quite like:

    int area = length*(breath=20);

    For a suitable value of "like".

    Shorter, but lacking, is:

    int area = length*20+breath;

    ReplyDelete
  53. The "static breath" solution was obvious, but I like "(breath=20)" which takes advantage of assignment returning the value of the assignment which is often seen in cases like "while ((nextline = reader.readLine()) != null)". The "+ 200" solutions are doubly clever for satisfying the "plain addition only" requirement of the twister.

    ReplyDelete

Solution for this question?