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: Help with the return concept

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with the return concept

    I have a few questions below about this code.


    /*
    * File: FactorialTable.java
    * -------------------------
    * This file generates a table of factorials.
    */

    import acm.program.*;

    public class FactorialTable extends ConsoleProgram {

    public void run() {
    for (int i = LOWER_LIMIT; i <= UPPER_LIMIT; i++) {
    println(i + "! = " + factorial(i));
    }
    }

    private int factorial(int n) {
    int result = 1;
    for (int i = 1; i <= n; i++) {
    result *= i;
    }
    return result;
    }

    /* Private constants */
    private static final int LOWER_LIMIT = 0;
    private static final int UPPER_LIMIT = 10;

    }


    Could someone be kind enough to explain a couple things here. Why is (int n) needed in the private method. I am lost as to how the for loop knows what is less than or equals to n as well. I am lost on some pretty basic concepts and my textbook is very vague. Some explanation would be very great!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with the return concept

    Why is (int n) needed
    Take a look at the tutorial:
    Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    how the for loop knows what is less than or equals to n
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. JADE---Sending an Concept including an concept
    By HansDampf in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 25th, 2014, 12:22 AM
  2. HELP with Inheritance concept here
    By dreamincode in forum Java Theory & Questions
    Replies: 6
    Last Post: January 5th, 2013, 07:45 AM
  3. HELP with Inheritance concept here
    By dreamincode in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 3rd, 2013, 12:03 PM
  4. Return Object does not return the expected output
    By Nour Damer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 13th, 2011, 07:24 AM
  5. Need help with a interface concept
    By drakenlord in forum Object Oriented Programming
    Replies: 2
    Last Post: September 13th, 2011, 02:17 PM