Designing Java Methods with 1-Dimensional Arrays

We can represent a collection of data items using an array. Java arrays have the following characteristics:

  1. Once created, the size of an array is fixed. We can access the size of the array using the array’s length attribute.
  2. The data items represented by the array are indexed consecutively from 0 to length-1.
  3. We can access each data item or more frequently array element using its index.
  4. The time to access each data item is always equal or almost equal.

We can either pass arrays to Java methods or have a Java method to return an array. The following are several exercises about designing and using Java methods with 1-dimensional arrays.

Exercise 1. Finding the Smallest Value in an Array

Write the definition of the method named getSmallestValue. The method takes an array of double, and returns the smallest value in the array. The header of the method should be as follows,

public static double getSmallestValue(double[] data)

Exercise 2. Finding the Greatest Element in an Array

Write the definition of the method named getLargestElement. The method receives a double array argument returns the index of the largest element. The header of the method is as follows,

public static int getLargestElement(double[] data)

Exercise 3. Counting Letters in a Character Array

Write the definition of the method named countLetters. The method takes a char array of lower case English letters, i.e., 'a''z', and returns the number of occurrences of each letter in an array. In the returned array, the element at index 0 represents the number of occurrences of letter 'a', that at index 1 the number of occurrences of letter 'b', and so on. The header of the method is as follows:

public static int[] countLetters(char[] letters)

Exercise 4. Exchange Values of Two Array Elements

Write the definition of the method named swapElements. The method takes a String array, two int parameters serving as array indices, and exchanges the values of the two array elements indexed by the two indices. The header of the method is as follows:

public static String[] swapElements(String[] arr, int i, int j)

Exercise 5. Computing the Average of Array Elements

Write the definition of the method named getAverage. The method takes a double array, computes and returns the average of the array. The header of the method is as follows,

public static double getAverage(double[] arr)

Exercise 6. Shifting Array Elements

Write the definition of the method named circularShift. The header of the method is as follows:

public static void circularShift(int[] arr, int position, char direction)

where parameter position indicates how many positions to shift and direction the direction to shift. The direction can be either left shift or right shift. If direction is L, the method does left-shift, and if direction is R, it does right-shift.

For instance, given that arr is {1, 2, 3, 4, 1, 3, 2, 1, 4, 5, 2}, position 2, and direction R, i.e., after our invoking

circularShift(arr, position, direction);

The arr should become:

{5, 2, 1, 2, 3, 4, 1, 3, 2, 1, 4}

Also for instance, for the same array, if position is 3 and direction is L, after invoking the method, we would have,

{4, 1, 3, 2, 1, 4, 5, 2, 1, 2, 3}

Exercise 7. Reverse an Array

Write the definition of the method named reverseArray with header:

public static void reverseArray(int[] arr)

that reverse the array in arr. For instance, given arr as {1, 2, 3, 4, 5}, by invoking the method reverseArray(arr), we expect that the array should become {5, 4, 3, 2, 1}. This method should work for any int array.

Exercise 8. Finding the Longest Run in Array

Given an array arr, we define a run as a consecutive array elements whose values are identical. For instance, given an int array {1, 2, 2, 2, 1, 3, 2, 1, 4, 5, 1}, we list the runs in the array as follows:

1
2 2 2
1 
3
2
1
4
5
1

For this exercise, you are to write the definition of the method findLongestRun with the following method header:

int[] findLongestRun(int[] arr)

which is to find the longest run in the array, and returns an array of length 3. In the returned array, the element at index 0 is the starting index of the run in array arr, the element at index 1 is the length of the run, and the element at index 2 is the value of the run. For instance, if the array has the values of the example given above, the method should return an array whose values are {1, 3, 2} where 1 is the starting index, 3 is the length, and 2 is the value of the longest run.

Additionally, the method should find the longest run “naturally”, i.e., if there are two runs of equal length and the two runs are the longest, the method always returns the first run.