/** * Programming Assignment #1 * *
 * P1s1.java, a program designed to calculate a variety 
 * of mathematical functions.  These functions include
 * multiplication, division, sequences, and factorial.
 * 
* * @author s1 * * Christoph Csallner 2004-07-21 renamed the file from P1 to P1s1. */ public class P1s1 { /*=================================================================*/ /* I T E R A T I V E M E T H O D S */ /*=================================================================*/ /** * Retrieves the factorial of the specified number. *

* @param iNumber the number of which the factorial is desired * @return the factorial of the specified number */ public static long getIterativeFactorial(int iNumber) { int ans = 1; int counter = 1; do { ans = ans * counter; counter = counter + 1; } while (counter <= iNumber); return ans; } // end of static method, getIterativeFactorial(int) /** * Retrieves the summation of the specified number. You may assume * that the passed-in number is positive (or zero). *

* @param iNumber the number of which the summation is desired * @return the summation of the specified number */ public static long getIterativeSummation(int iNumber) { int counter = 0; int answer = 0; do { answer = answer + counter; counter = counter + 1; } while (counter <= iNumber); return answer; } // end of static method, getIterativeSummation(int) /** * Returns whether or not the passed-in number is prime. If it is * prime true is returned; if it is not prime, false is returned. *

* @param iNumber the number to determine whether it is prime * @return true if the number is prime, otherwise false */ public static boolean isPrime(int iNumber) { int counter; boolean answer = true; for (counter=2;((counter < iNumber) && (answer = false));counter++) { if (iNumber%counter != 0) //this means it's not a divisor { return answer; } else answer = false; } if (iNumber < 1) answer = false; return answer; } // end of static method, isPrime(int) /** * Returns the Nth Fibonacci number where N is the passed-in value. * Any non-positive value results in 0 being returned *

* @param iPosition how far into the Fibonacci Sequence to go * @return the Fibonacci number at the specified position or 0 if * appropriate */ public static long getIterativeFibonacciNumber(int iPosition) { int sum; int newsum; sum = 0; newsum = 1; int i; i = iPosition; if (iPosition < 1) return sum; else { while (0 < iPosition) { iPosition--; newsum = sum + newsum; sum = newsum - sum; } // end while } // end else return sum; } // end of static method, getIterativeFibonacciNumber(int) /** * Retrieves the result of the specified base raised to the * specified exponent. *

* @param iBase the base of the expression * @param iExponent the exponent of the expression * @return the value of the base raised to the power of the exponent */ public static long getIterativePower(int iBase, int iExponent) { int counter; int result = 1; for (counter=0;counter
* @param iNumber the number of which the factorial is desired * @return the factorial of the specified number */ public static long getRecursiveFactorial(int iNumber) { if (iNumber == 0) { return 1; } else { return (iNumber * (getRecursiveFactorial ((iNumber - 1)))); } } // end of static method, getRecursiveFactorial(int) /** * Retrieves the summation of the specified number. You may assume * that the passed-in number is positive (or zero). *

* @param iNumber the number of which the summation is desired * @return the summation of the specified number */ public static long getRecursiveSummation(int iNumber) { if (iNumber == 0) { return 0; } else { return (iNumber + (getRecursiveSummation((iNumber - 1)))); } } // end of static method, getRecursiveSummation(int) /** * Returns the Nth Fibonacci number where N is the passed-in value. * Any non-positive value results in 0 being returned *

* @param iPosition how far into the Fibonacci Sequence to go * @return the Fibonacci number at the specified position or 0 if * appropriate */ public static long getRecursiveFibonacciNumber(int iPosition) { if (iPosition < 1) return 0; if (iPosition == 1) { return 1; } if (iPosition == 2) { return 1; } else { return ((getRecursiveFibonacciNumber((iPosition - 1))) + (getRecursiveFibonacciNumber((iPosition - 2)))); } } // end of static method, getRecursiveFibonacciNumber(int) /** * Retrieves the result of the specified base raised to the * specified exponent. * * @param iBase the base of the expression * @param iExponent the exponent of the expression * @return the value of the base raised to the power of the exponent */ public static long getRecursivePower(int iBase, int iExponent) { if (iExponent == 0) return iBase; if (iExponent == 1) return iBase; else { return (iBase * (getRecursivePower(iBase, (iExponent - 1)))); } } // end of static method, getRecursivePower(int, int) /** the following is a method that could have been used * to assist in the coding for the previous method, however * I did not need to use it. */ /** * May be used as a helper method to getPower(int,int). * * @param iBase Base of the expression * @param iExponent the Exponent of the expression * @param iValue a holder variable to help with the recursion. * @return the value of the base raised to the power of the exponent */ public static long getRecursivePower(int iBase, int iExponent, int iValue) { /* * This method may be called from getPower(int,int). * This method is held to the same restrictions as getPower(int,int) */ return -1; /* This line of code is here to allow your program to * compile before you complete the entire assignment. * Replace the line of code and these comments with your * own code when you get to this method. */ } // end of static method, getRecursivePower(int, int, int) /*=================================================================*/ /* A R R A Y M E T H O D S */ /*=================================================================*/ /** * Returns an array of the first N squares, where N is the passed-in * value. If length is not a positive number, return an empty array. *

* @param length the number of squares the array should contain * @return an array containing the first N non-negative integers * squared */ public static int [] getSquaresArray(int length) { int[] myArray; if (length < 0) return new int [0]; myArray = new int[length]; if (length == 0) return new int [0]; for (int i=0;i
* @param firstArray An array of doubles * @param secondArray Another array of doubles */ public static void swapArrays(double [] firstArray, double [] secondArray) { double[] helperArray = new double [firstArray.length]; for(int m=0;m
* @param data an array of integers * @return the number of prime numbers in the array */ public static int primeCount(int [] data) { int count = 0; int p; for (p=0;p
* @param list an array of integers that might contain the pattern * @param pattern an array of integers containing the pattern to find * @return the position where the pattern starts */ public static int findPattern(int [] list, int [] pattern) { int place = -1; int iPattern = 0; int iList; for (iList=0;iList
* @param argv the command-line parameters */ public static void main(String[] argv) { //#1 System.out.println(); System.out.println("getIterativeFactorial(0)=" + getIterativeFactorial(0)); System.out.println("getIterativeFactorial(1)=" + getIterativeFactorial(1)); System.out.println("getIterativeFactorial(2)=" + getIterativeFactorial(2)); System.out.println("getIterativeFactorial(3)=" + getIterativeFactorial(3)); System.out.println("getIterativeFactorial(4)=" + getIterativeFactorial(4)); System.out.println("getIterativeFactorial(5)=" + getIterativeFactorial(5)); //#2 System.out.println(); System.out.println("getIterativeSummation(0)=" + getIterativeSummation(0)); System.out.println("getIterativeSummation(1)=" + getIterativeSummation(1)); System.out.println("getIterativeSummation(2)=" + getIterativeSummation(2)); System.out.println("getIterativeSummation(3)=" + getIterativeSummation(3)); System.out.println("getIterativeSummation(4)=" + getIterativeSummation(4)); System.out.println("getIterativeSummation(5)=" + getIterativeSummation(5)); //#3 System.out.println(); System.out.println("isPrime(1)=" + isPrime(1)); System.out.println("isPrime(2)=" + isPrime(2)); System.out.println("isPrime(3)=" + isPrime(3)); System.out.println("isPrime(4)=" + isPrime(4)); System.out.println("isPrime(5)=" + isPrime(5)); System.out.println("isPrime(6)=" + isPrime(6)); System.out.println("isPrime(7)=" + isPrime(7)); System.out.println("isPrime(9)=" + isPrime(9)); System.out.println("isPrime(13)=" + isPrime(13)); System.out.println("isPrime(15)=" + isPrime(15)); //#4 System.out.println(); System.out.println("getIterativeFibonacciNumber(-1)=" +getIterativeFibonacciNumber(-1)); System.out.println("getIterativeFibonacciNumber(0)=" +getIterativeFibonacciNumber(0)); System.out.println("getIterativeFibonacciNumber(1)=" +getIterativeFibonacciNumber(1)); System.out.println("getIterativeFibonacciNumber(2)=" +getIterativeFibonacciNumber(2)); System.out.println("getIterativeFibonacciNumber(3)=" +getIterativeFibonacciNumber(3)); System.out.println("getIterativeFibonacciNumber(4)=" +getIterativeFibonacciNumber(4)); System.out.println("getIterativeFibonacciNumber(5)=" +getIterativeFibonacciNumber(5)); //#5 System.out.println(); System.out.println("getIterativePower(1,0)=" + getIterativePower(1,0)); System.out.println("getIterativePower(0,1)=" + getIterativePower(0,1)); System.out.println("getIterativePower(2,2)=" + getIterativePower(2,2)); System.out.println("getIterativePower(3,1)=" + getIterativePower(3,1)); System.out.println("getIterativePower(2,3)=" + getIterativePower(2,3)); //#6 System.out.println(); System.out.println("getRecursiveFactorial(0)=" + getRecursiveFactorial(0)); System.out.println("getRecursiveFactorial(1)=" + getRecursiveFactorial(1)); System.out.println("getRecursiveFactorial(2)=" + getRecursiveFactorial(2)); System.out.println("getRecursiveFactorial(3)=" + getRecursiveFactorial(3)); System.out.println("getRecursiveFactorial(4)=" + getRecursiveFactorial(4)); //#7 System.out.println(); System.out.println("getRecursiveSummation(0)=" + getRecursiveSummation(0)); System.out.println("getRecursiveSummation(1)=" + getRecursiveSummation(1)); System.out.println("getRecursiveSummation(2)=" + getRecursiveSummation(2)); System.out.println("getRecursiveSummation(3)=" + getRecursiveSummation(3)); System.out.println("getRecursiveSummation(4)=" + getRecursiveSummation(4)); System.out.println("getRecursiveSummation(5)=" + getRecursiveSummation(5)); //#8 System.out.println(); System.out.println("getRecursiveFibonacciNumber(-1)=" + getRecursiveFibonacciNumber(-1)); System.out.println("getRecursiveFibonacciNumber(0)=" + getRecursiveFibonacciNumber(0)); System.out.println("getRecursiveFibonacciNumber(1)=" + getRecursiveFibonacciNumber(1)); System.out.println("getRecursiveFibonacciNumber(2)=" + getRecursiveFibonacciNumber(2)); System.out.println("getRecursiveFibonacciNumber(3)=" + getRecursiveFibonacciNumber(3)); System.out.println("getRecursiveFibonacciNumber(4)=" + getRecursiveFibonacciNumber(4)); //#9 System.out.println(); System.out.println("getRecursivePower(0,1)=" + getRecursivePower(0,1)); System.out.println("getRecursivePower(1,3)=" + getRecursivePower(1,3)); System.out.println("getRecursivePower(2,2)=" + getRecursivePower(2,2)); System.out.println("getRecursivePower(1,0)=" + getRecursivePower(1,0)); System.out.println("getRecursivePower(2,4)=" + getRecursivePower(2,4)); //#10 System.out.println(); int[] tempArray = getSquaresArray(5); int j; for (j=0;j