import java.util.Arrays; class Recursion { public static void main(String[] args) { Recursion r = new Recursion(); System.out.println("2 to the power 5 = "+r.power(2,5)); int [] array = {1,2,2,2,3,4,5,5,5,6}; System.out.println("Mean of array {1,2,2,2,3,4,5,5,5,6} is: "+r.mean(array)); System.out.println("Mode of array {1,2,2,2,3,4,5,5,5,6} is: "+r.mode(array)); } public int power(int a, int b){ if(b == 0) return 1; else return power(a, b, 0, 1); } public int power(int a, int b, int counter, int pow){ if(b == counter) return pow; return power(a, b, counter+1, pow * a); } public int mean(int list[]){ if(list == null) return 0; else return mean(list, 0, 0); } public int mean(int list[], int index, int sum){ if(index == list.length) return (sum / list.length); else return mean(list, index+1, sum+list[index]); } // if more than 1 element has the same number of duplications, the first one is returned public int mode(int list[]){ Arrays.sort(list); return mode(list, list[0], 1, 1, 1); } public int mode(int list[], int maxMode, int maxOccurrence, int currentOccurrence, int index){ if(index == list.length) return maxMode; // the new occurrence is more than the previous maximum occurrence else if (list[index] == list[index-1] && currentOccurrence == maxOccurrence) return mode(list, list[index], currentOccurrence+1, currentOccurrence+1, index+1); // the new occurrence is not more than the previous maximum occurrence else if(list[index] == list[index-1]) return mode(list, maxMode, maxOccurrence, currentOccurrence+1, index+1); // the new element has a different number else return mode(list, maxMode, maxOccurrence, 1, index+1); } }