anonymous
2014-03-05 21:23:53 UTC
I have come across a piece of code in the book, despite me trying to wrap my head around it i Do not understand it. Pretty much it was an optional puzzle, and it was a fill in the blank. I could not figure it out, and this is the complete puzzle.
public class EchoTestDrive {
public static void main(String [] args) {
Echo e1 = new Echo();
Echo e2 = e1;
int x = 0;
while (x < 4)
e1.hello()
e1.count = e1.count +1;
if ( x == 3) {
e2.count = e2.count + 1;
}
if (x > 0 ) {
e2.count = e2.count + e1.count;
}
x = x + 1;
}
System.out.println(e2.count);
}
}
___new class____
class Echo{
int echo = 0;
void hello() {
System.out.println("helloooo...");
}
}
It should out put helloooo... four times and at the end print out 10. Can anyone break this code down for me and help me understand it? Dont get me wrong, I do understand some stuff about it but I dont know what isnt clicking in my head for me to see the whole picture. It just is really discouraging that if a book puts a puzzle, and i cant figure it out it makes me feel like im not retaining anything and/or programming isnt for me. And i do not want to give up on programming as i have many times before and I really want to learn it. I just honestly do have ADD and i think it really affects me so anyone that can help please, I will give you best answer right away.
Thanks, Gene Jr.