Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Please explain this output

 
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"
 
Marshal
Posts: 6643
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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?
 
author
Posts: 23855
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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.
 
Bartender
Posts: 1845
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: 1845
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
 
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
 
Marshal
Posts: 67477
257
  • 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.
 
I will suppress my every urge. But not this shameless plug:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!