• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help me to understand this code

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone im currently study java please help me to understand this code.

public class Mix4 {

int counter = 0;

public static void main(String [] args){

int count = 0;

Mix4[] m4a = new Mix4[20];

int x = 0;

while (x<9){

m4a[x] = new Mix4();
m4a[x].counter = m4a[x].counter + 1;
count = count + 1;
count = count + m4a[x].maybeNew(x);
x=x+1;
}
System.out.println(count + " " + m4a[1].counter);
}

public int maybeNew(int index){

if ( index<5 ) {

Mix4 m4 = new Mix4();
m4.counter = m4.counter + 1;
return 1; >>>> where the return go?
}
return 0; >>>>> and also this one?
}
}

I hope you help me guys to understand the code because im newly in java language thanks.
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to Ranch!!!
1) There are 2 methods in your example
main method


maybeNew method


2) You had to write the return statement in maybeNew() method because you had a int written in the signature of method (after public word you can see there's an int), but as far as the main() method block is there
you can't see the return statement because there is void in the signature (i.e. after public word there is void)

3) Now your actual question is "where does the return value goes?" -----The return value goes to the place from where the method is called. So in your example, you have written this in your main() method


So when you wrote the method maybeNew() is called, the method gets executed, and as per the condition written in your method, the value is returned back to the calling statement.

4) So in your case it could be either

OR


Whenever you are posting you can use code tags.
 
ryan almadin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So everytime the maybeNew() method is true the count always execute and add right ? then once the maybeNew() method false it return 0 so the count stop i am right sir?

 
ryan almadin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for welcome me here at the ranch
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ryan almadin wrote:So everytime the maybeNew() method is true the count always execute and add right ? then once the maybeNew() method false it return 0 so the count stop i am right sir?



I haven't really executed your code and its not about start and stop.

When the method maybeNew() was executed either 0 or 1 will be returned to the calling statement i.e.


So suppose at this time count value is 4 and from method the returned value is 1, then the calling statement would have become

That is now count variable has the value 5.

If the return value was 0 then

That is now count variable has value 4.

And after this all the statements in the main() method would execute the way you have written. i.e. the statements below
 
ryan almadin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It means sir the count = count + m4a[x].maybeNew(x); if the return executed the m4a[x].maybeNew(x) is now 1 ? correct me if i am wong thanks.
 
ryan almadin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wowww thanks sir for your help me to understand i got it now...
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ryan almadin wrote:It means sir the count = count + m4a[x].maybeNew(x); if the return executed the m4a[x].maybeNew(x) is now 1 ? correct me if i am wong thanks.



Lets go step by step, it will be easy to understand

1) In your main method you wrote,


2) Now m4a[x].maybeNew(x); is a call to the method mayneNew(x).

3) Therefore maybeNew() method gets executed and this method either returns 1 or 0 value.

4) Now whatever value is returned from maybeNew() method, substitute the value in place of m4a[x].maybeNew(x) in the statement count = count + m4a[x].maybeNew(x);

If you have any more doubts please tell!!!
 
ryan almadin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I finish now my Exercises thanks again sir soni.
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome ryan
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ryan

1) Whenever you have a new question, then start a new thread i.e. a new topic. But if your doubt is regarding the same concept which an earlier topic had then you can continue in that topic.
Since here you are having a new code, i request you to start a new topic.

2) Whenever you are posting any code here, first press the "code" button which you can see when you are posting a question. After pressing the code button paste your code between the code tags. You are pretty new so this happens.

3) As far as your current code is concerned, i can see 2 problems

i)

See you have written x = x 6

ii) After the class name you have put brackets (parenthesis) i.e.


So correct them.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ryan almadin,

Your last post was moved to a new topic called "Puzzle 4".

In future, please keep your threads to a single question at a time.

Thanks.

Winston
 
ryan almadin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh sorry guys im newbie in ranch please forgive me by now i'll do that next time thanks for advice family ranch
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ryan-----No problem!!!

You are new that's why it does happen.
 
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic