File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Help me to understand this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help me to understand this code" Watch "Help me to understand this code" New topic
Author

Help me to understand this code

ryan almadin
Greenhorn

Joined: Jul 19, 2012
Posts: 9
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.
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
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

Joined: Jul 19, 2012
Posts: 9
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

Joined: Jul 19, 2012
Posts: 9
Thanks for welcome me here at the ranch
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
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

Joined: Jul 19, 2012
Posts: 9
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

Joined: Jul 19, 2012
Posts: 9
Wowww thanks sir for your help me to understand i got it now...
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
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

Joined: Jul 19, 2012
Posts: 9
I finish now my Exercises thanks again sir soni.
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
You're welcome ryan
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
@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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7637
    
  19

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
ryan almadin
Greenhorn

Joined: Jul 19, 2012
Posts: 9
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

Joined: Feb 03, 2011
Posts: 247
@ryan-----No problem!!!

You are new that's why it does happen.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help me to understand this code