Forums Register Login

return statement

+Pie Number of slices to send: Send
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;
}
}
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
    +Pie Number of slices to send: Send
    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.
    +Pie Number of slices to send: Send
    thanks Campbell Ritchie for clearing my doubt.i understood it now.
    +Pie Number of slices to send: Send
    Only too pleased to be able to help
    He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 1310 times.
    Similar Threads
    How to read this?
    how exactly does this code compile?
    Java Problem = List of errors, Clueless. HELP
    return - problem
    code
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 28, 2024 13:07:14.