Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.Scanner;
- /**
- * @Author Carlos
- * This program does a number of functions explained below.
- * The factorial of a pre-defined number is calculated using recursion.
- * These numbers are 12, 25, and -5.
- * This function is defined as index-1 * index. Since negative numbers have no factorial a value of 0 is returned.
- * Once that is complete the Fibonacci of pre-defined numbers are calculated using recursion.
- * These numbers are 5, 12, 1, and 8.
- * This function is defined as Fibonacci of index-1+Fibonacci of index-2 for any index >=2.
- * Once both of these are completed a Binary search is run using recursion.
- * This function operates in the following way:
- * A file of ints, ints.txt and doubles, doubles.txt are scanned and added to an array.
- * Once they are within the array, the array is sorted generically.
- * Once sorted the array is printed.
- * From here, we scan in a document of values to search the sorted array.
- * These documents are intsearch.txt and doublesearch.txt
- * The function will search for a number from these documents against its respective array.
- * From here once found the location of the number will be printed.
- */
- public class RecursionManagement {
- public static void main(String[] args) {
- //List of number's we will be using to find their Factorial or their Fibonacci
- int a = 12;
- int b = 25;
- int c = -5;
- int d = 4;
- int e = 5;
- int f = 12;
- int g = 1;
- int h = 8;
- //Printing the results of the specific function
- System.out.println("\nFactorial of " + a + " is: " + Factorial(a));
- System.out.println("\nFactorial of " + b + " is: " + Factorial(b));
- System.out.println("\nFactorial of " + c + " is: " + Factorial(c));
- System.out.println("\nFactorial of " + d + " is: " + Factorial(d));
- System.out.println("\nFibonacci of " + e + " is: " + Fibonacci(e));
- System.out.println("\nFibonacci of " + f + " is: " + Fibonacci(f));
- System.out.println("\nFibonacci of " + g + " is: " + Fibonacci(g));
- System.out.println("\nFibonacci of " + h + " is: " + Fibonacci(h));
- //Array of Ints to hold ints.txt
- Integer[] myintarray = new Integer[11];
- //Array of doubles to hold doubles.txt
- Double[] mydoublearray = new Double[9];
- //Array of ints to search with
- Integer[] myintsearch = new Integer[5];
- //Array of doubles to search with
- Double[] mydoublesearch = new Double[4];
- //New Instance of MyRecursiveManager for Ints
- MyRecursiveManager<Integer> MyGenericInts = new MyRecursiveManager<Integer>(myintarray);
- //New Instance of MyRecursiveManager for doubles
- MyRecursiveManager<Double> MyGenericDoubles = new MyRecursiveManager<Double>(mydoublearray);
- try {
- //A row of ints to use with the 4 array's
- int n = 0;
- int x = 0;
- int y = 0;
- int z = 0;
- //Scanners for each file
- Scanner intinput = new Scanner(new File("src/main/ints.txt"));
- Scanner intsearch = new Scanner(new File("src/main/intsearch.txt"));
- Scanner doubleinput = new Scanner(new File("src/main/doubles.txt"));
- Scanner doublesearch = new Scanner(new File("src/main/doublesearch.txt"));
- //Adding each scanners readings to its specific arrays.
- while (intinput.hasNextLine()) {
- myintarray[n] = intinput.nextInt();
- n++;
- }
- while (intsearch.hasNextLine()) {
- myintsearch[y] = intsearch.nextInt();
- y++;
- }
- while (doubleinput.hasNextLine()) {
- mydoublearray[x] = doubleinput.nextDouble();
- x++;
- }
- while (doublesearch.hasNextLine()) {
- mydoublesearch[z] = doublesearch.nextDouble();
- z++;
- }
- } catch (FileNotFoundException x) {
- System.err.println("File was not found");
- }
- //Sorting the Array's of Integers and Doubles
- MyGenericInts.SelectSort(10);
- MyGenericDoubles.SelectSort(8);
- System.out.println("\nPrinting The Sorted Array of Integers: ");
- for (int i = 0; i <= 10; i++) {
- System.out.println("In Location: " + i + " Is: " + myintarray[i]);
- }
- System.out.println("\nPrinting The Sorted Array of Doubles");
- for (int i = 0; i <= 8; i++) {
- System.out.println("In Location: " + i + " Is: " + mydoublearray[i]);
- }
- System.out.println("\nSearching The Array of Ints for Values, and returning their location: ");
- for (int i = 0; i < myintsearch.length; i++) {
- int result = MyGenericInts.binarySearchRecursive(myintsearch[i], 0, myintarray.length);
- System.out.println("Searching for: " + myintsearch[i] + " Location: " + result);
- }
- System.out.println("\nSearching The Array of Doubles for Values, and returning their location: ");
- for (int i = 0; i < mydoublesearch.length; i++) {
- int result = MyGenericDoubles.binarySearchRecursive(mydoublesearch[i], 0, mydoublearray.length);
- System.out.println("Searching for: " + mydoublesearch[i] + " Location: " + result);
- }
- }
- //Recursively calculates the Fibonacci of a pre-defined number. Defined as Fib(n-1)+Fib(n-2)
- public static int Fibonacci(int index) {
- if (index == 0)
- return 0;
- else if (index == 1)
- return 1;
- else
- return Fibonacci(index - 1) + Fibonacci(index - 2);
- }
- //Recursively calculates the Factorial of a Pre-defined number. Defined as N-1 * N
- public static int Factorial(int index) {
- if (index < 0) {
- System.out.println("Factorial For: "+index+" Does Not Exist. Returning -1.");
- return -1;
- }
- if (index <= 1) {
- return 1;
- } else
- return Factorial(index - 1) * index;
- }
- //MyRecusriveManager class.
- static class MyRecursiveManager<T extends Comparable<T>> {
- protected T values[];
- protected int mcount;
- protected T max;
- //Constructor for passing an Array
- public MyRecursiveManager(T[] myGenericArray) {
- values = myGenericArray;
- mcount = 0;
- }
- //Gets the values from Values Array
- public T getvalue(int i) {
- if (i <= mcount) return values[i];
- else return values[0];
- }
- //Recursive Binary Search
- public int binarySearchRecursive(T search, int start, int end) {
- int middle = (start + end) / 2;
- if (end < start) {
- return -1;
- }
- if (search.compareTo(values[middle]) == -1) {
- return binarySearchRecursive(search, start, middle - 1);
- }
- if (search.compareTo(values[middle]) == 1) {
- return binarySearchRecursive(search, middle + 1, end);
- }
- if (search.compareTo(values[middle]) == 0) {
- return middle;
- }
- return -1;
- }
- //Recursive SelectSort
- public void SelectSort(int high) {
- //Stopping condition for the recursive Function.
- if (high > 0) {
- //Find the Max element to high subscript
- int indexOfMax = 0;
- max = values[0];
- for (int i = 1; i <= high; i++) {
- if ((values[i]).compareTo(max) == 1) {
- max = values[i];
- indexOfMax = i;
- }
- }
- //Swap the largest with the last number in the list
- values[(indexOfMax)] = values[high];
- values[high] = max;
- //Now recursively call sort on the list that is one element shorter.
- SelectSort(high - 1);
- } else return;
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement