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.
"int area = length * breath;static" - 6 new characters
ReplyDelete"int area = length * 20;//breath;" - 5 new characters
ReplyDeletepublic class Area {
ReplyDeletestatic 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);
}
}
Line 6:
ReplyDeleteint area = 200;//length*breath;
"200;//" - 6 char addition
package com.twister;
ReplyDeletepublic 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);
}
}
Make a "static" declaration for variable "breath".
ReplyDeletepackage 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);
}
}
Two solutions for me:
ReplyDeleteFirst, 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;//'.
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).
ReplyDeletepublic 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);
}
}
Make breath static! Adding the static modifier is enough because static initializers run before instance initializers and thus eliminate the forward reference.
ReplyDeletechange int breath = 20; -> static int breath = 20;
ReplyDeletepackage com.twister;
ReplyDeletepublic 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);
}
}
public class Area {
ReplyDeleteint 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.
Pretty cool teaser ;)
ReplyDeleteI 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?
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 :)
ReplyDeletestatic int breath = 20;
ReplyDeleteGerhard Balthasar: En even easier fix would be to make the field 'int breath = 20;' static. Addition of just 6 characters.
ReplyDeletepackage com.twister;
ReplyDeletepublic 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);
}
}
package com.twister;
ReplyDeletepublic 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);
}
}
package com.twister;
ReplyDeletepublic 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);
}
}
Change to static int breadth = 20;
ReplyDeleteStatic variables are initialised first and so the program will run properly.
public class Test {
ReplyDeleteint 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);
}
}
line 7, add "static" before int breath = 20;
ReplyDeleteUgly, unreadable and I even did not understood why içt was working :-)
ReplyDeletepackage 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);
}
}
Ugly, unreadable !!!
ReplyDeleteI 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);
}
}
package com.twister;
ReplyDeletepublic 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);
}
}
Breath needs a value before you use it.
ReplyDeleteJust stick make breath static (or even better breadth ;-) )
ReplyDeleteMake breath a static field
ReplyDeleteadd "static" @ begin of line 7
ReplyDeleteline 11
ReplyDeleteSystem.out.println(a.area*20);
3chars :P
int breatha = 20;
ReplyDeleteint area = length * breatha;
int area = length*breath + 200;
ReplyDeleteMake 'breath' static
ReplyDeletepublic 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);
}
}
Make the breadth instance variable static.
ReplyDeletepublic 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);
}
}
static int breath = 20;
ReplyDeleteuse static for breadth
ReplyDeleteJust move the area variable declaration below the breadth variable declaration. Btw, "breath" is spelled "breadth".
ReplyDeletereplace line 6 with:
ReplyDeleteint area = 200;//length*breath;
Add 'static' before int breath = 20;
ReplyDeleteAdd "static" before "int breath":
ReplyDeletestatic 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);
}
}
1 package com.twister;
ReplyDelete2
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 }
Make breath static.
ReplyDeleteMake breath static.
ReplyDeletemake least breath static.
ReplyDeletestatic int breath = 20;
public class Area {
ReplyDeleteint 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);
}
}
public class Area {
ReplyDeleteint 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);
}
}
change:
ReplyDeleteint breath = 20;
to:
static int breath = 20;
static int breath = 20;
ReplyDelete1 package com.twister;
ReplyDelete2
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 }
package com.twister;
ReplyDelete/**
* 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);
}
}
make 'length' and 'breath' to be static.
ReplyDeletepackage com.twister;
ReplyDeletepublic 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);
}
}
int area = length * /*breath;
ReplyDeleteint breath = */20;
I just added 4 characters
is it wrong?
I quite like:
ReplyDeleteint area = length*(breath=20);
For a suitable value of "like".
Shorter, but lacking, is:
int area = length*20+breath;
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