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.
declare inner class
ReplyDeleteclass A{
int area = length * width;
}
Change:
ReplyDeleteint area = length*width;
To:
int area;// =length*width; -> 3 new characters
int area;String a= "length*width";
ReplyDeleteArea(){
ReplyDeleteint area = length*width;
}
package com.twister;
ReplyDeletepublic 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
package com.twister;
ReplyDeletepublic class Area {
int area = length=0 * (width=0);
int length=10;
int width=10;
public static void main(String[] args) {
Area a = new Area();
}
}
how about this?
ReplyDeleteint area = (length=0) * (width=0);
int area = this.length*this.width; ??:)
ReplyDeleteArea(){int area = length*width;}
ReplyDeletewrapping the code in a default-constructor
public class Area {
ReplyDelete// 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: put code into constructor
ReplyDeletepublic 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
}
}
change area declaration to:
ReplyDeleteint area = this.length*this.width;
int area = (length=0)*(width=0);
ReplyDeleteis it correct to just comment line 6?
ReplyDelete"//" -> 2 chars :)
public class Area {
ReplyDelete//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
}
}
If it is just supposed to compile, following rules a and b (and not comments in code), surround "int area = length*width;" with
ReplyDeletevoid m(){int area=length*width;}
10 extra characters.
int area = this.length*this.width;
ReplyDelete10 characters added
P.S. I'm back from vacations :)
1 package com.twister;
ReplyDelete2 //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 }
Adding this reference for the fields that are being used before defined, as follows:
ReplyDeleteint area = this.length*this.width;
// 10 chars added
Do the area calculation in a constructor?
ReplyDeletepackage com.twister;
ReplyDeletepublic 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
}
}
package com.twister;
ReplyDelete2
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 }
I believe I have done it in 9. I just wrapped the line in a function.
ReplyDeletevoid f(){
int area = length * width;
}
The goal was just to get it to compile right?
Also putting "this." in front of both vars works. It's also 10 characters.. depending on how you count. Do you count whitespace?
ReplyDeleteWe can convert the expression to a String. Assuming the ONLY goal is to make the code compile:
ReplyDeletepublic 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
}
}
public class Area {
ReplyDelete//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
}
}
// 11 characters
ReplyDeleteint area = "length*width".length();
int area = (length=0)*(width=0);
ReplyDelete//from previous soln by someone