Question:
Can you please explain this java program line by line, please?...?
anonymous
2014-03-05 21:23:53 UTC
Hello all, I have been following several tutorials and reading several books on java. I am reading this book that i think requires a bit of programming knowledge beforehand but i am doing pretty well so far.

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.
Four answers:
anonymous
2014-03-05 21:42:12 UTC
One of the things you have to get good at to be a good programmer, is 'executing' code line by line. Get a dry erase white board, write down the current value of each variable, and then slowly, step through the code just like the program would do if the computer were executing it.



If you get stuck, then try this with a debugger, like the one's built into eclipse or net beans. It will walk you through the code line by line so you can see exactly why you get the output you do.



I'd also suggest you ask specific questions here in the forums. A general question will usually get a general answer (if any), where as a specific question will get a specific answer. Ask specifically about the parts that you don't understand, not about the whole program.
tumbleweed_biff
2014-03-06 05:42:21 UTC
public class EchoTestDrive { //beginning of class definition

public static void main(String [] args) { //main

Echo e1 = new Echo(); // create an object of type echo named e1

Echo e2 = e1; // create another object of type echo as a copyt of e1 named e2

int x = 0; // if you don't know what this does, then you should probably quit now, a career in programming isn't for you.



while (x < 4) //check and see if the counter is less than 4. if it is, do the following



//here you have a problem. The way you have this written, the while clause isn't going to repeat anything, it will go straight to the e1.hello() because you haven't set the contents off with curly braces {} to show what is part of the the while clause. Once you add the braces around the contents of the where clause, I think it will perform as you desire. All the rest of the code is pretty straight forward so I will forgo covering that.
?
2014-03-06 05:42:39 UTC
Hey, I am not entirely sure on how to do this but I wanted to say LOL to you for getting back someone who flamed you. This satrya character, whom cant even structure a proper English sentence flamed you. I understand people from other places do not speak native English. If you are trying to learn it good for you, but do not try to flame someone if you cant properly speak there language. I hope his lunch goes well too haha! BTW, I know who you are.. you are Marc Ewing's cousin. Ive seen you on several forums with him and I saw a lecture he gave with you in the video. Why don't you ask him?
?
2014-03-06 05:28:40 UTC
no


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...