Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

return statement

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Marshal
Posts: 74390
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Marshal
    Posts: 74390
    334
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thanks Campbell Ritchie for clearing my doubt.i understood it now.
     
    Campbell Ritchie
    Marshal
    Posts: 74390
    334
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Only too pleased to be able to help
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic