This week's book giveaway is in the Testing forum.We're giving away four copies of The Way of the Web Tester: A Beginner's Guide to Automating Tests and have Jonathan Rasmusson on-line!See this thread for details.
Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# return statement

krishna kitti
Greenhorn
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
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
Posts: 50702
83
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
Posts: 50702
83
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
Posts: 4
thanks Campbell Ritchie for clearing my doubt.i understood it now.

Campbell Ritchie
Sheriff
Posts: 50702
83
Only too pleased to be able to help