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 return statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "return statement" Watch "return statement" New topic
Author

return statement

krishna kitti
Greenhorn

Joined: Jan 01, 2007
Posts: 4
i came across this program while reading headfirst java.output of this program is lised as 14 1 .i could understand upto m4[x].counter=m4a[x].counter+1;can someone explain how itz working and producing 14 1 ouput.



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();
m4[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 static maybeNew(int index){
if(index<5) {
Mix4 m4=new Mix4();
m4.counter=m4.counter+1;
return1;
}
return 0;
}
}
krishna kitti
Greenhorn

Joined: Jan 01, 2007
Posts: 4
i came across this program while reading headfirst java.output of this program is lised as 14 1 .i could understand upto m4[x].counter=m4a[x].counter+1;can someone explain how itz working and producing 14 1 ouput.



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();
m4[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;
return1;
}
return 0;
}
}
[ February 11, 2007: Message edited by: krishna kitti ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Welcome to the Ranch.

I presume both posts are identical? Please use code tags before and after any code quotes; they preserve the identation etc.

It has three spelling errors in:-
  • return1; instead of return 1;
  • m4[x].counter = instead of m4a[x].counter =
  • and, m4a[x] = new mix4(); instead of m4a[x] = new Mix4();
  • When you correct them and compile the program, it does infact print 14 1.

    It looks a very complicated piece of programming to fit into 32 lines.
    Put some "print" statements into it, as below. You can tell what I have inserted because it ends with //test.You can see the numbers going up to 14 and 1, but I fail to see what the point of the program is in the first place.

    CR
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39393
        
      28
    You appear to be setting the counter to 0, adding 1 to it, then adding that 1 to the count. For the first 5 instances (no 0 to no 4) your "maybeNew" method returns 1, after that it adds 0.

    9 + 5 = 14.

    Then you print out the 2nd Mix4's counter number, which we have already seen is increased from 0 to 1.

    It still seems pointless to me.
    krishna kitti
    Greenhorn

    Joined: Jan 01, 2007
    Posts: 4
    thanks Campbell Ritchie for clearing my doubt.i understood it now.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39393
        
      28
    Only too pleased to be able to help
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: return statement