Advertisement
informaticage

Recursive and iterative methods java

Nov 28th, 2016
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.24 KB | None | 0 0
  1. package javam;
  2. /**
  3.  * @author Samuel Finocchio
  4.  * Recursive and iterative methods
  5.  */
  6.  
  7. public class Javam {
  8.     static boolean findLetter(int[] arr, int n, int toFind) {
  9.         // if the whole array does not contain TO_FIND
  10.         if(n == arr.length) return false;
  11.        
  12.         // if the TO_FIND constant is inside the array
  13.         if(arr[n] == toFind) return true;
  14.        
  15.         // If the array is not finished yet and nothing was already found
  16.         return findLetter(arr, n + 1, toFind);
  17.     }
  18.    
  19.     public static int min (int vet[], int min, int position) {
  20.         // If the whole array was parsed
  21.         if(position == vet.length)
  22.             return min;
  23.        
  24.         // if the current vet value is less then the current min
  25.         if (vet[position] < min)
  26.             min = vet[position];
  27.        
  28.         // if not the next element is returned with the current position incremented
  29.         return min(vet, min, position + 1);
  30.     }
  31.    
  32.     public static int max (int vet[], int max, int position) {
  33.         // If the whole array was parsed
  34.         if(position == vet.length)
  35.             return max;
  36.        
  37.         // if the current vet value is greater then the current min
  38.         if (vet[position] > max)
  39.             max = vet[position];
  40.        
  41.         // if not the next element is returned with the current position incremented
  42.         return max(vet, max, position + 1);
  43.     }
  44.    
  45.     public static int sum (int vet[], int position, int sum) {
  46.         // If the whole array was parsed
  47.         if(position == vet.length)
  48.             return sum;
  49.        
  50.         // Sum gets incremented by the current element of the array
  51.         sum += vet[position];
  52.        
  53.         // Sum is called with the next position
  54.         return sum(vet, position + 1, sum);
  55.     }
  56.    
  57.     public static int sum (int vet[][], int firstIndex, int secondIndex, int sum) {
  58.         // vet[firstIndex][secondIndex]
  59.        
  60.         // If the whole array was parsed
  61.         if(firstIndex == vet.length - 1 && secondIndex == vet[firstIndex].length)
  62.             return sum;
  63.        
  64.         // If a row in the array is parsed but there is anotherone
  65.         if(secondIndex == vet[firstIndex].length)
  66.             return sum(vet, firstIndex + 1, 0, sum);
  67.        
  68.         // Sum gets incremented by the current element of the array
  69.         sum += vet[firstIndex][secondIndex];
  70.        
  71.         // Sum is called with the next position
  72.         return sum(vet, firstIndex, secondIndex + 1, sum);
  73.     }
  74.    
  75.     public static char[] select(String s, int array[], char selectedArray[], int position) {
  76.         // if the whole array is parsed
  77.         if(position == array.length)
  78.             return selectedArray;
  79.        
  80.         // assigns the relative position found in array of index specified by position in selected array of index position
  81.         selectedArray[position] = s.charAt(array[position]);
  82.                
  83.         // returns select with the next position
  84.         return select(s, array, selectedArray, position + 1);
  85.     }
  86.      
  87.     public static String separate(String s, char separator)
  88.     {
  89.         String newS = "";
  90.         // Foreach character in s
  91.         for(int i = 0; i < s.length() - 1; i++){
  92.             // Attaches the char at position i and the separator
  93.             newS += s.charAt(i);
  94.             newS += separator;
  95.         }
  96.         newS += s.charAt(s.length() - 1);
  97.         return newS;
  98.     }
  99.    
  100.     public static String separateR (String s, String newS, char separator, int position)
  101.     {
  102.         if(position == s.length() - 1)
  103.             return newS + s.charAt(s.length() - 1);
  104.        
  105.         newS += s.charAt(position) + Character.toString(separator);
  106.         // newS += separator;
  107.        
  108.         return separateR(s, newS, separator, position + 1);
  109.     }
  110.    
  111.     public static void main(String[] args) {
  112.         int vet[] = {3, 1, 2, 3, 1};
  113.         int vetBi[][] = {
  114.             {0,3, 1},
  115.             {-1, 3, 2},
  116.             {0, 1, 1},
  117.             {1, 2 ,2},
  118.             {0, 1, 1}
  119.         };
  120.        
  121.         // findLetter(array, counter to 0, number to find)
  122.         System.out.println("Find letter: " + findLetter(vet, 0, 5));
  123.        
  124.         // min (array, first element used to compare, counter to 0)
  125.         System.out.println("Min: " + min (vet, vet[0], 0));
  126.      
  127.         // max (array, first element used to compare, counter to 0)
  128.         System.out.println("Max: " + max (vet, vet[0], 0));
  129.        
  130.         // sum (array, position, starting sum from 0)
  131.         System.out.println("Sum: " + sum (vet, 0, 0));
  132.        
  133.         // sum overloaded (array, position1, position2, starting sum)
  134.         System.out.println("Sum 2: " + sum(vetBi, 0, 0, 0));
  135.        
  136.         // For each char generated by the method select
  137.         for (char
  138.                 // select ("string in witch characters are picked", vector of positions, array to create )
  139.                 c:select("wkci2", vet, new char["wkci2".length()], 0))
  140.             System.out.print(c);
  141.        
  142.         System.out.println();
  143.        
  144.         // separateR(initial string, empty starting, separator, starting from 0)
  145.         System.out.println(separateR("FreeSoftware", "", '_', 0));    
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement