• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please explain this output  RSS feed

 
Jose Aldo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please, help me solve this, I think this just simple for you but I'm really having a hard time of why the output is 14 1 and I don't understand, please explain
asd.JPG
[Thumbnail for asd.JPG]
Please Explain why the output is "14 1"
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

In the future, could you copy the text of the program instead of a screen shot? And UseCodeTags (← that's a link).

What specifically don't you understand? Have you tried working it out with pencil and paper?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
Jose Aldo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got this question from Head First Java 2nd edition in chapter 4 page 90
 
Jose Aldo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Welcome to the Ranch!

In the future, could you copy the text of the program instead of a screen shot? And UseCodeTags (← that's a link).

What specifically don't you understand? Have you tried working it out with pencil and paper?


Actually I don't understand the whole program, and how it really works, I already tried to solve it on paper but my answer didn't match the answers.
 
Jose Aldo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:Please QuoteYourSources.

Henry


Hello sir, I got this from Head First Java 2nd edition Chapter 4, page 90, Mixed message.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"14 1" looks right to me.

What is your answer? Why?

The things to keep in mind is instance vs static variables.
The answer would be different if counter was static.


 
Jose Aldo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:"14 1" looks right to me.

What is your answer? Why?

The things to keep in mind is instance vs static variables.
The answer would be different if counter was static.



Yeah, I know that the output is 14 1, but I don't understand how that happen. Please explain the program to me.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jose,
that's not the way it works around here :-)
Explain to us YOUR answer, and how you got to it.

You know it's wrong but the process of explaining your reasoning will help you, and then let us point out exactly where your misunderstanding might be.
Or even which bit don't you understand how it got? The 14 or the 1 ?

 
Jose Aldo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:Hi Jose,
that's not the way it works around here :-)
Explain to us YOUR answer, and how you got to it.

You know it's wrong but the process of explaining your reasoning will help you, and then let us point out exactly where your misunderstanding might be.
Or even which bit don't you understand how it got? The 14 or the 1 ?



Hello Stefan, I'm so sorry that i couldn't explain my problem properly, but I just want to know of how did this code got the out put of 14 1, I mean I really don't understand how the process works
understand.JPG
[Thumbnail for understand.JPG]
here man, the things that i ddnt understand in this program
 
nikhil kumar sharma
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just do it on paper and pencil. it is because none of the variable is static. so what you are getting through 9 iterations are following:
for x = 1--->counter = 1, count = 2;
for x = 2--->counter = 1, count = 4;
for x = 3--->counter = 1, count = 6;
for x = 4--->counter = 1, count = 8;
for x = 5--->counter = 1, count = 10;
for x = 6--->counter = 1, count = 11;
for x = 7--->counter = 1, count = 12;
for x = 8--->counter = 1, count = 13;
for x = 9--->counter = 1, count = 14;

Thats is why you are getting 14 1 output.
 
nikhil kumar sharma
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry the value of x is from 0 to 8;

for x = 0--->counter = 1, count = 2;
for x = 1--->counter = 1, count = 4;
for x = 2--->counter = 1, count = 6;
for x = 3--->counter = 1, count = 8;
for x = 4--->counter = 1, count = 10;
for x = 5--->counter = 1, count = 11;
for x = 6--->counter = 1, count = 12;
for x = 7--->counter = 1, count = 13;
for x = 8--->counter = 1, count = 14;
 
Jose Aldo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nikhil kumar sharma wrote:sorry the value of x is from 0 to 8;

for x = 0--->counter = 1, count = 2;
for x = 1--->counter = 1, count = 4;
for x = 2--->counter = 1, count = 6;
for x = 3--->counter = 1, count = 8;
for x = 4--->counter = 1, count = 10;
for x = 5--->counter = 1, count = 11;
for x = 6--->counter = 1, count = 12;
for x = 7--->counter = 1, count = 13;
for x = 8--->counter = 1, count = 14;

thanks, now I get it, but I'm still confused with the codes like Mix4 [] m4a = new Mix4[20];
 
nikhil kumar sharma
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that is only an array initialization of type Mix4 with a capacity of 20.
 
Jose Aldo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nikhil kumar sharma wrote:that is only an array initialization of type Mix4 with a capacity of 20.

Thanks man
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to nikhil kumar sharma
 
nikhil kumar sharma
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!