/**
* Demonstrate methods using for
*
* @author (Michael Branicky), 09/25/2006, 09/26/2006
*/
public class ForMethods
{
   public static int rollDie() {
     return (int) Math.floor(Math.random()*6)+1;
   }

   public static void rollDie10Times() {
     for (int rolls=0; rolls<10; rolls++) {
       System.out.print(rollDie()+" ");
     }
     System.out.println();
   }

   public static void countDownFrom10 () {
     for (int i = 10; i>0; i--) {
       System.out.print(i+" ... ");
     } 
     System.out.println("Liftoff!");
   }

   public static void countDownFrom (int n) {
     for (int i = n; i>0; i--) {
       System.out.print(i+" ... ");
     } 
     System.out.println("Liftoff!");
   }


   //////////////////// Sums ////////////////

   // compute the sum of the first N integers
   //   sum_{i = 1 to N} i
   public static int sum1toN (int N) {
     int sum = 0;
     for (int i = 1; i<=N; i++) {
       sum += i;
     } 
     return sum;
   }

   // compute the sum of the first N+1 terms of a geometric series
   //   sum_{i = 0 to N} r^i
   public static double geometricSum (double r, int N) {
     double sum = 0;
     double term = 1.0;
     for (int i = 0; i<=N; i++) {
       sum += term;
       term *= r;
     } 
     return sum;
   }

   //////////////////// Functions involving repetition  ////////////////

   // computes n! for n >= 0
   public static int factorial (int n) {
     int answer = 1;   // 0! == 1
     for (int i = 1; i<=n; i++) {
       answer *= i;
     } 
     return answer;
   }

   // computes 2^n for n >= 0
   public static int powerOf2 (int n) {
     int answer = 1;   // 2^0 == 1
     for (int i = 1; i<=n; i++) {
       answer *= 2;
     } 
     return answer;
   }

   // computes m^n for n >= 0
   public static int power (int m, int n) {
     int answer = 1;   // m^0 == 1
     for (int i = 1; i<=n; i++) {
       answer *= m;
     } 
     return answer;
   }


   public static void main(String args[])
   {
     // Test/use the rollDie10Times method
     System.out.print("10 die rolls: "); 
     rollDie10Times();
     System.out.print("10 die rolls: ");
     rollDie10Times();

     // Test/use the countDownFrom10 method
     countDownFrom10();

     // Test/use the countDownFrom method
     countDownFrom(15);
     countDownFrom(3);
     countDownFrom(0);
     // countDownFrom(-10); // <-- this would run a long time!

     // Test/use the sums methods
     System.out.println("sum 1 to 3 = " + sum1toN(3));
     System.out.println("sum 1 to 100 = " + sum1toN(100));
     System.out.println("3 terms of (1/2)^i = " + geometricSum(0.5, 2));
     System.out.println("10 terms of (1/2)^i = " + geometricSum(0.5, 9));

     // Test/use the functions involving repetition
     System.out.println("3! = " + factorial(3));
     System.out.println("5! = " + factorial(5));
     System.out.println("2^3 = " + powerOf2(3));
     System.out.println("2^5 = " + powerOf2(5));
     System.out.println("2^10 = " + powerOf2(10));
     System.out.println("3^2 = " + power(3, 2));
     System.out.println("5^3 = " + power(5, 3));
     System.out.println("10^6 = " + power(10, 6));
  }
}
