Purpose: Learn how to do basic mathematics in java. Learn about if statements and loops. Learn some more about classes and functions.
public static void main(String[] args) {...}We will describe the content of the main function in steps below, and then summarize at the end. Everything in statements B-E will go in the main method.
int n=Integer.parseInt(args[0]);Comments:
if (n < 0) { System.out.println("The number (" + n + ")! is undefined because " + n + " is negative."); System.exit(0); }Comments:
int factorial=1; int i=1;The number factorial will eventually store n!. We will iterate i from 1 to n, multiplying factorial by i each time:
while (i <= n) { factorial = factorial * i; i = i+1; }Comments:
System.out.println( "" + n + "! = " + factorial);The construction ""+n takes the empty string "" and concatenates the string formed my converting the integer n to a string. Then we concatenate "! = " and factorial converted to a string. We print something like "5! = 120".
public static void main(String[] args) { int n=Integer.parseInt(args[0]); if ( n < 0) { System.out.println("The number (" + n + ")! is undefined because " + n + " is negative."); System.exit(0); } int factorial=1; int i=1; while (i <=n ) { factorial = factorial * i; i = i+1; } System.out.println( "" + n + "! = " + factorial); }You can also view the whole file with comments here: Factorial.java.
public static int factorial(int n) {...}The function takes as input the integer n. (Input is in parentheses). The int after static indicates that the function will return an int. The complete function is given by:
public static int factorial(int n) { int factorial=1; for (int i=1; i<=n; i++) { factorial=factorial * i; } return factorial; }The only new aspect of this code is that we used a for loop rather than a while loop. In the parentheses after the for loop, there are three expressions: The first, int i=1, is run when the loop begins, and creates an integer i with value 1. The second expression i<=n returns true or false. This has the same purpose as the expression in the while loop; the statements in brackets { ... } will be evaluated so long as this expression is true. The third expression i++ is evaluated after the statements in brackets, whenever they are evaluated. The expression i++ is equivalent to i=i+1. So a for loop is just a compact form of the while loop.
public static double power(double x, int n) {...}This function takes two inputs, a double named x and an int named n, which we assume is non-negative. A double stores an approximation of a real number. The function will also return a real number. The number returned will be xn. The code for this follows:
public static double power(double x, int n) { double ret=1.0; for (int i=1; i<=n; i++) { ret = ret * x; } return ret; }You should be able to figure out how the function returns xn.
Our method will do the following. It will read a real number x from input, then it will output exp(x). We use the Taylor series for exp(x):
The source code for our main function is given below:
public static void main(String[] args) { double x=Double.parseDouble(args[0]); double sum=0; for (int i=0; i<15; i++) { sum=sum+power(x,i)/factorial(i); } System.out.println("exp("+x+") = "+sum); System.out.println("A second opinion: exp("+x+") = "+Math.exp(x)); }Comments:
import java.math.BigInteger;
public static BigInteger factorial(int n) { BigInteger factorial=BigInteger.valueOf(1); for (int i=1; i<=n; i++) { factorial=factorial.multiply(BigInteger.valueOf(i)); } return factorial; }
public static BigInteger valueOf(long val)Comments on the method declaration:
public BigInteger multiply(BigInteger val)The absence of the modifier static means that to call the function you need to use an object to call it. In our code, we use:
factorial.multiply(BigInteger.valueOf(i))Here, factorial is an object of class BigInteger. We use it to call multiply with parameter BigInteger.valueOf(i), which is a newly constructed BigInteger with the same value as i. The function will return the product of the two BigInteger objects involved.
public static void main(String[] args) { int n=Integer.parseInt(args[0]); System.out.println(""+n+"! = "+factorial(n)); }This code simply converts the first argument to an integer, then prints the result returned from the factorial method.
import java.math.BigInteger; public class BigFactorial { public static BigInteger factorial(int n) { BigInteger factorial=BigInteger.valueOf(1); for (int i=1; i<=n; i++) { factorial=factorial.multiply(BigInteger.valueOf(i)); } return factorial; } public static void main(String[] args) { int n=Integer.parseInt(args[0]); System.out.println(""+n+"! = "+factorial(n)); } }You can see the commented source code here: BigFactorial.java.