// CSE 373, Winter 2013, Marty Stepp // This program is just a basic review of Java and Eclipse. // We wrote methods to compute a factorial and to replicate a character. // One new concept shown here is the use of the BigInteger class // to represent arbitrarily large integer values. import java.math.*; // for BigInteger public class Lecture01 { public static void main(String[] args) { System.out.println("Hello, world!"); // String s = "12345"; // int n = Integer.parseInt(s); // System.out.println(s + 1); // System.out.println(n + 1); System.out.println(factorial(40)); System.out.println(nCopies(1000000, 'x')); } // Returns a string containing n copies of the character c. // For example, nCopies(5, 'x') returns "xxxxx". public static String nCopies(int n, char c) { StringBuilder s = new StringBuilder(); for (int i = 0; i < n; i++) { s.append(c); } return s.toString(); } // Returns n! = 1 * 2 * 3 * ... * n-1 * n. // Assumes n >= 0. public static BigInteger factorial(int n) { BigInteger x = BigInteger.valueOf(1); for (int i = 1; i <= n; i++) { x = x.multiply(BigInteger.valueOf(i)); } return x; } }