Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java method question

  1. #1
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Java method question

    This was a question on a quiz we had in my programming course. The answer is 70 as the output, but I'm having trouble understanding why. Could someone please tell me, step by step, why that comes out to be the output?


    1.      public class MysteriousClass {
    2.          public static void main(String[] args) {
    3.              int i = 20;
    4.              int b = m2(i);
    5.              System.out.println(b + i);
    6.          }
     
    7.          public static int m1(int i){
    8.              int n = 0;
    9.              while (n * n <= i){ 
    10.                 n++; 
    11.             }
    12.             return n - 1;
    13.         }
     
    14.         public static int m2(int a){ 
    15.             int b = 0;
    16.             for (int n = 0; n < a; n++){
    17.                 int i = m1(n);
    18.                 b = b + i;
    19.             }
    20.             return b;
    21.         }
    22.   }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,667
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: Java method question

    To see what the code is doing, try debugging the code by adding lots of println statements to print out the values of the variables as they are used and changed.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. calling method question.
    By jasonrest in forum Object Oriented Programming
    Replies: 4
    Last Post: March 26th, 2013, 06:42 PM
  2. Question on a method?
    By cam25 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 17th, 2012, 02:53 PM
  3. [SOLVED] Question about using the toString method.
    By iDizzle in forum Object Oriented Programming
    Replies: 12
    Last Post: April 9th, 2012, 09:24 AM
  4. Substring Retrieval Method Question
    By Nuggets in forum Java Theory & Questions
    Replies: 12
    Last Post: January 30th, 2012, 11:16 PM
  5. I need help on the Newton Raphson Method in java, here is the question
    By Peter Mande in forum Member Introductions
    Replies: 1
    Last Post: March 18th, 2011, 02:30 PM

Tags for this Thread