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!