Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public class Recursion { public static void print(int[] array) { for(int i = 0;

ID: 3778966 • Letter: P

Question

public class Recursion
{

public static void print(int[] array)
{

for(int i = 0; i < array.length-1; i++)
{
System.out.print(array[i]+ "," );
}
System.out.print(array[array.length-1]);
System.out.println();
}

public static int smallest(int[] array)
{
return smallestFrom(array, 0);
}

private static int smallestFrom(int[] array, int start)
{
  

if (start == array.length - 1)
{
return array[start];
}

int val = smallestFrom(array, start + 1);
if (array[start] < val)
{
return array[start];
}
else
{
return val;
}
}

public static int smallestIndex(int[] array)
{
return smallestIndexFrom(array, 0);
}

private static int smallestIndexFrom(int[] array, int start)
{
// Your code goes here.
if (start == array.length - 1)
{
return start;
}

int index = smallestIndexFrom(array, start + 1);

if (array[start] < array[index])
{
return start;
}
else
{
return index;
}
}
}

form the code above answer the following questions :

Why are smallestFrom() and smallestIndexFrom() private? Should they be?

Why are all the methods in Recursion static? Could it be changed so they are not? Which one is better for this application?

Explanation / Answer

smallestFrom and smallest Index Frm sould be private to protect its data from outside users.To perform encapsulation so that other funtions which are unauthorised to access this data,cannot even touch them.

we control recursive methods or lgorithms just by its parameters or arguments.we dont use instance variables here.As we all know that ,when a method does not rely on instance variables,it should be made static.Hence recursion is static.

theoretically these can be changed to dynamic