Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

Write RECURIVE programs.

Problem II. Write the method


public static double getLargest(double [] a, int low, int high)
that returns the largest number in the array slice a[low:high].
If low > high, it throws an IllegalArgumentException. Otherwise, it checks if
the slice has 1 item. If so, it returns that value.
If the slice has 2 or more items, it divides the slice into 2 equal subslices,
computes the largestvalues of the 2 subslices and returns the largest of the 2 v
alues.
--------------------------------------------------------------------------------------------Driver for the programs
public static void main(String[] args)
{
// check getlargest
System.out.println("\nWe test getLargest");
System.out.println("The array is arr = { 6.9, -10.3, 6.7, 2.5, 16.4, -3.
1}.");
double[] arr = { 6.9, -10.3, 6.7, 2.5, 16.4, -3.1};
try
{
System.out.println("The largest of arr[0:6] is ");
System.out.println(getLargest(arr, 0, 6));
}
catch(IllegalArgumentException e)
{
System.out.println("We got an illegal argument exception.");
}
try
{
System.out.println("The largest of arr[2:1] is ");
System.out.println(getLargest(arr, 2, 1));
}
catch(IllegalArgumentException e)
{
System.out.println("We got an illegal argument exception.");
}
System.out.println("The largest of arr[2:2] is " +
getLargest(arr, 2, 2));
System.out.println("The largest of arr[0:4] is " +
getLargest(arr, 0, 4));
}

-------------------------------------------------------------------------------------expected output:
We test getLargest
The array is arr = { 6.9, -10.3, 6.7, 2.5, 16.4, -3.1}.
The largest of arr[0:6] is
We got an illegal argument exception.
The largest of arr[2:1] is
We got an illegal argument exception.
The largest of arr[2:2] is 6.7
The largest of arr[0:4] is 16.4

You might also like