/* @(#)Methods.java 15 September 2005 */ /* Useful imports */ import edu.neu.ccs.*; import edu.neu.ccs.gui.*; import edu.neu.ccs.codec.*; import edu.neu.ccs.console.*; import edu.neu.ccs.filter.*; import edu.neu.ccs.jpf.*; import edu.neu.ccs.parser.*; import edu.neu.ccs.pedagogy.*; import edu.neu.ccs.quick.*; import edu.neu.ccs.util.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.font.*; import java.awt.image.*; import javax.swing.*; import javax.swing.border.*; import java.io.*; import java.util.*; import java.math.*; import java.beans.*; import java.lang.reflect.*; import java.net.URL; import java.util.regex.*; import java.text.ParseException; /** The sample starter class for Java Power Framework. */ public class Methods extends JPF { public static void main(String[] args) { // LookAndFeelTools.showSelectLookAndFeelDialog(); LookAndFeelTools.adjustAllDefaultFontSizes(4); new Methods(); } /** * Table of factorial of n using the Java class BigInteger. * Mathematicians denote factorial of n as n! * * n! = 1 * 2 * 3 * ... * (n-1) * n. * * Inductively: n! = n * (n-1)!. * * BigInteger is constructed using a String representation * of a big integer. * * BigInteger is immutable so one must replace the local * variable f each time an arithmetic operation is done. * * Output for n = 10: * * 0! = 1 * 1! = 1 * 2! = 2 * 3! = 6 * 4! = 24 * 5! = 120 * 6! = 720 * 7! = 5040 * 8! = 40320 * 9! = 362880 * 10! = 3628800 */ public void FactorialTable1(int n) { int k = 0; BigInteger f = new BigInteger("1"); while (k <= n) { console.out.println(k + "! = " + f); k++; f = f.multiply(new BigInteger(k + "")); } console.out.println(); } /** * Table of factorial of n using the JPT class XBigInteger. * Mathematicians denote factorial of n as n! * * n! = 1 * 2 * 3 * ... * (n-1) * n. * * Inductively: n! = n * (n-1)!. * * XBigInteger is constructed using a String representation * of a big integer, or a long, or a BigInteger, or an * XBigInteger. This is more flexible. * * XBigInteger is mutable so an arithmetic operation is done * directly on the data object f it acts upon so replacement * by assignment is not needed. * * Output for n = 10: * * 0! = 1 * 1! = 1 * 2! = 2 * 3! = 6 * 4! = 24 * 5! = 120 * 6! = 720 * 7! = 5040 * 8! = 40320 * 9! = 362880 * 10! = 3628800 */ public void FactorialTable2(int n) { int k = 0; XBigInteger f = new XBigInteger(1); while (k <= n) { console.out.println(k + "! = " + f); k++; f.multiply(new XBigInteger(k)); } console.out.println(); } }