Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javam;
- /**
- * @author Samuel Finocchio
- * Recursive and iterative methods
- */
- public class Javam {
- static boolean findLetter(int[] arr, int n, int toFind) {
- // if the whole array does not contain TO_FIND
- if(n == arr.length) return false;
- // if the TO_FIND constant is inside the array
- if(arr[n] == toFind) return true;
- // If the array is not finished yet and nothing was already found
- return findLetter(arr, n + 1, toFind);
- }
- public static int min (int vet[], int min, int position) {
- // If the whole array was parsed
- if(position == vet.length)
- return min;
- // if the current vet value is less then the current min
- if (vet[position] < min)
- min = vet[position];
- // if not the next element is returned with the current position incremented
- return min(vet, min, position + 1);
- }
- public static int max (int vet[], int max, int position) {
- // If the whole array was parsed
- if(position == vet.length)
- return max;
- // if the current vet value is greater then the current min
- if (vet[position] > max)
- max = vet[position];
- // if not the next element is returned with the current position incremented
- return max(vet, max, position + 1);
- }
- public static int sum (int vet[], int position, int sum) {
- // If the whole array was parsed
- if(position == vet.length)
- return sum;
- // Sum gets incremented by the current element of the array
- sum += vet[position];
- // Sum is called with the next position
- return sum(vet, position + 1, sum);
- }
- public static int sum (int vet[][], int firstIndex, int secondIndex, int sum) {
- // vet[firstIndex][secondIndex]
- // If the whole array was parsed
- if(firstIndex == vet.length - 1 && secondIndex == vet[firstIndex].length)
- return sum;
- // If a row in the array is parsed but there is anotherone
- if(secondIndex == vet[firstIndex].length)
- return sum(vet, firstIndex + 1, 0, sum);
- // Sum gets incremented by the current element of the array
- sum += vet[firstIndex][secondIndex];
- // Sum is called with the next position
- return sum(vet, firstIndex, secondIndex + 1, sum);
- }
- public static char[] select(String s, int array[], char selectedArray[], int position) {
- // if the whole array is parsed
- if(position == array.length)
- return selectedArray;
- // assigns the relative position found in array of index specified by position in selected array of index position
- selectedArray[position] = s.charAt(array[position]);
- // returns select with the next position
- return select(s, array, selectedArray, position + 1);
- }
- public static String separate(String s, char separator)
- {
- String newS = "";
- // Foreach character in s
- for(int i = 0; i < s.length() - 1; i++){
- // Attaches the char at position i and the separator
- newS += s.charAt(i);
- newS += separator;
- }
- newS += s.charAt(s.length() - 1);
- return newS;
- }
- public static String separateR (String s, String newS, char separator, int position)
- {
- if(position == s.length() - 1)
- return newS + s.charAt(s.length() - 1);
- newS += s.charAt(position) + Character.toString(separator);
- // newS += separator;
- return separateR(s, newS, separator, position + 1);
- }
- public static void main(String[] args) {
- int vet[] = {3, 1, 2, 3, 1};
- int vetBi[][] = {
- {0,3, 1},
- {-1, 3, 2},
- {0, 1, 1},
- {1, 2 ,2},
- {0, 1, 1}
- };
- // findLetter(array, counter to 0, number to find)
- System.out.println("Find letter: " + findLetter(vet, 0, 5));
- // min (array, first element used to compare, counter to 0)
- System.out.println("Min: " + min (vet, vet[0], 0));
- // max (array, first element used to compare, counter to 0)
- System.out.println("Max: " + max (vet, vet[0], 0));
- // sum (array, position, starting sum from 0)
- System.out.println("Sum: " + sum (vet, 0, 0));
- // sum overloaded (array, position1, position2, starting sum)
- System.out.println("Sum 2: " + sum(vetBi, 0, 0, 0));
- // For each char generated by the method select
- for (char
- // select ("string in witch characters are picked", vector of positions, array to create )
- c:select("wkci2", vet, new char["wkci2".length()], 0))
- System.out.print(c);
- System.out.println();
- // separateR(initial string, empty starting, separator, starting from 0)
- System.out.println(separateR("FreeSoftware", "", '_', 0));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement