Advertisement
Luninariel

Recursive Manager - WIP

Mar 11th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.27 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. /**
  7.  * @Author Carlos
  8.  * This program does a number of functions explained below.
  9.  * The factorial of a pre-defined number is calculated using recursion.
  10.  * These numbers are 12, 25, and -5.
  11.  * This function is defined as index-1 * index. Since negative numbers have no factorial a value of 0 is returned.
  12.  * Once that is complete the Fibonacci of pre-defined numbers are calculated using recursion.
  13.  * These numbers are 5, 12, 1, and 8.
  14.  * This function is defined as Fibonacci of index-1+Fibonacci of index-2 for any index >=2.
  15.  * Once both of these are completed a Binary search is run using recursion.
  16.  * This function operates in the following way:
  17.  * A file of ints, ints.txt and doubles, doubles.txt are scanned and added to an array.
  18.  * Once they are within the array, the array is sorted generically.
  19.  * Once sorted the array is printed.
  20.  * From here, we scan in a document of values to search the sorted array.
  21.  * These documents are intsearch.txt and doublesearch.txt
  22.  * The function will search for a number from these documents against its respective array.
  23.  * From here once found the location of the number will be printed.
  24.  */
  25.  
  26. public class RecursionManagement {
  27.  
  28.     public static void main(String[] args) {
  29.         //List of number's we will be using to find their Factorial or their Fibonacci
  30.         int a = 12;
  31.         int b = 25;
  32.         int c = -5;
  33.         int d = 4;
  34.         int e = 5;
  35.         int f = 12;
  36.         int g = 1;
  37.         int h = 8;
  38.  
  39.  
  40.         //Printing the results of the specific function
  41.         System.out.println("\nFactorial of " + a + " is: " + Factorial(a));
  42.         System.out.println("\nFactorial of " + b + " is: " + Factorial(b));
  43.         System.out.println("\nFactorial of " + c + " is: " + Factorial(c));
  44.         System.out.println("\nFactorial of " + d + " is: " + Factorial(d));
  45.         System.out.println("\nFibonacci of " + e + " is: " + Fibonacci(e));
  46.         System.out.println("\nFibonacci of " + f + " is: " + Fibonacci(f));
  47.         System.out.println("\nFibonacci of " + g + " is: " + Fibonacci(g));
  48.         System.out.println("\nFibonacci of " + h + " is: " + Fibonacci(h));
  49.  
  50.         //Array of Ints to hold ints.txt
  51.         Integer[] myintarray = new Integer[11];
  52.  
  53.  
  54.         //Array of doubles to hold doubles.txt
  55.         Double[] mydoublearray = new Double[9];
  56.  
  57.         //Array of ints to search with
  58.         Integer[] myintsearch = new Integer[5];
  59.  
  60.         //Array of doubles to search with
  61.         Double[] mydoublesearch = new Double[4];
  62.  
  63.  
  64.         //New Instance of MyRecursiveManager for Ints
  65.         MyRecursiveManager<Integer> MyGenericInts = new MyRecursiveManager<Integer>(myintarray);
  66.  
  67.         //New Instance of MyRecursiveManager for doubles
  68.         MyRecursiveManager<Double> MyGenericDoubles = new MyRecursiveManager<Double>(mydoublearray);
  69.  
  70.         try {
  71.             //A row of ints to use with the 4 array's
  72.             int n = 0;
  73.             int x = 0;
  74.             int y = 0;
  75.             int z = 0;
  76.  
  77.             //Scanners for each file
  78.             Scanner intinput = new Scanner(new File("src/main/ints.txt"));
  79.             Scanner intsearch = new Scanner(new File("src/main/intsearch.txt"));
  80.             Scanner doubleinput = new Scanner(new File("src/main/doubles.txt"));
  81.             Scanner doublesearch = new Scanner(new File("src/main/doublesearch.txt"));
  82.  
  83.             //Adding each scanners readings to its specific arrays.
  84.             while (intinput.hasNextLine()) {
  85.                 myintarray[n] = intinput.nextInt();
  86.                 n++;
  87.             }
  88.             while (intsearch.hasNextLine()) {
  89.                 myintsearch[y] = intsearch.nextInt();
  90.                 y++;
  91.             }
  92.             while (doubleinput.hasNextLine()) {
  93.                 mydoublearray[x] = doubleinput.nextDouble();
  94.                 x++;
  95.             }
  96.             while (doublesearch.hasNextLine()) {
  97.                 mydoublesearch[z] = doublesearch.nextDouble();
  98.                 z++;
  99.             }
  100.  
  101.  
  102.         } catch (FileNotFoundException x) {
  103.             System.err.println("File  was not found");
  104.         }
  105.         //Sorting the Array's of Integers and Doubles
  106.         MyGenericInts.SelectSort(10);
  107.         MyGenericDoubles.SelectSort(8);
  108.  
  109.         System.out.println("\nPrinting The Sorted Array of Integers: ");
  110.         for (int i = 0; i <= 10; i++) {
  111.             System.out.println("In Location: " + i + " Is: " + myintarray[i]);
  112.         }
  113.         System.out.println("\nPrinting The Sorted Array of Doubles");
  114.         for (int i = 0; i <= 8; i++) {
  115.             System.out.println("In Location: " + i + " Is: " + mydoublearray[i]);
  116.         }
  117.  
  118.         System.out.println("\nSearching The Array of Ints for Values, and returning their location: ");
  119.         for (int i = 0; i < myintsearch.length; i++) {
  120.             int result = MyGenericInts.binarySearchRecursive(myintsearch[i], 0, myintarray.length);
  121.             System.out.println("Searching for: " + myintsearch[i] + " Location: " + result);
  122.         }
  123.         System.out.println("\nSearching The Array of Doubles for Values, and returning their location: ");
  124.         for (int i = 0; i < mydoublesearch.length; i++) {
  125.             int result = MyGenericDoubles.binarySearchRecursive(mydoublesearch[i], 0, mydoublearray.length);
  126.             System.out.println("Searching for: " + mydoublesearch[i] + " Location: " + result);
  127.         }
  128.     }
  129.  
  130.  
  131.     //Recursively calculates the Fibonacci of a pre-defined number. Defined as Fib(n-1)+Fib(n-2)
  132.     public static int Fibonacci(int index) {
  133.         if (index == 0)
  134.             return 0;
  135.         else if (index == 1)
  136.             return 1;
  137.         else
  138.             return Fibonacci(index - 1) + Fibonacci(index - 2);
  139.     }
  140.  
  141.     //Recursively calculates the Factorial of a Pre-defined number. Defined as N-1 * N
  142.     public static int Factorial(int index) {
  143.  
  144.         if (index < 0) {
  145.             System.out.println("Factorial For: "+index+" Does Not Exist. Returning -1.");
  146.             return -1;
  147.         }
  148.  
  149.         if (index <= 1) {
  150.             return 1;
  151.         } else
  152.             return Factorial(index - 1) * index;
  153.  
  154.     }
  155.  
  156.     //MyRecusriveManager class.
  157.     static class MyRecursiveManager<T extends Comparable<T>> {
  158.         protected T values[];
  159.         protected int mcount;
  160.         protected T max;
  161.  
  162.         //Constructor for passing an Array
  163.         public MyRecursiveManager(T[] myGenericArray) {
  164.             values = myGenericArray;
  165.             mcount = 0;
  166.         }
  167.  
  168.         //Gets the values from Values Array
  169.         public T getvalue(int i) {
  170.             if (i <= mcount) return values[i];
  171.             else return values[0];
  172.         }
  173.  
  174.         //Recursive Binary Search
  175.         public int binarySearchRecursive(T search, int start, int end) {
  176.  
  177.             int middle = (start + end) / 2;
  178.  
  179.             if (end < start) {
  180.                 return -1;
  181.             }
  182.  
  183.  
  184.             if (search.compareTo(values[middle]) == -1) {
  185.                 return binarySearchRecursive(search, start, middle - 1);
  186.             }
  187.  
  188.             if (search.compareTo(values[middle]) == 1) {
  189.                 return binarySearchRecursive(search, middle + 1, end);
  190.             }
  191.  
  192.             if (search.compareTo(values[middle]) == 0) {
  193.                 return middle;
  194.             }
  195.  
  196.             return -1;
  197.         }
  198.  
  199.         //Recursive SelectSort
  200.         public void SelectSort(int high) {
  201.             //Stopping condition for the recursive Function.
  202.             if (high > 0) {
  203.                 //Find the Max element to high subscript
  204.                 int indexOfMax = 0;
  205.                 max = values[0];
  206.                 for (int i = 1; i <= high; i++) {
  207.                     if ((values[i]).compareTo(max) == 1) {
  208.                         max = values[i];
  209.                         indexOfMax = i;
  210.                     }
  211.                 }
  212.                 //Swap the largest with the last number in the list
  213.                 values[(indexOfMax)] = values[high];
  214.                 values[high] = max;
  215.                 //Now recursively call sort on the list that is one element shorter.
  216.                 SelectSort(high - 1);
  217.             } else return;
  218.  
  219.             return;
  220.         }
  221.  
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement