In: Computer Science
Java Programming II Homework 2-1 In this assignment you are being asked to write some methods that operate on an array of int values. You will code all the methods and use your main method to test your methods. Your class should be named Array Your class will have the following methods (click on the method signatures for the Javadoc description of the methods): [ https://bit.ly/2GZXGWK ]
public static int sum(int[] arr) public static int sum(int[] arr, int firstIndex, int lastIndex)
public static double average(int[] arr) public static double average(int[] arr, int firstIndex, int lastIndex)
public static int maxValue(int[] arr) public static int maxValue(int[] arr, int firstIndex, int lastIndex)
public static int indexOfFirstMaxValue(int[] arr)
public static int indexOfFirstMaxValue(int[] arr, int firstIndex, int lastIndex)
public static int numberOfBelowAverageElements(int[] arr)
public static int numberOfBelowAverageElements(int[] arr, int firstIndex, int lastIndex)
public static void rotateElements(int[] arr) public static void rotateElements(int[] arr, int rotationCount)
public static void reverseArray(int[] arr)
For example, given the following array: myArray = {45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44, 32, 21, 14, 7, 16, 49, 58, 72}
Your methods will return the following values:
1. Sum of whole array = 1253
2. Sum of elements 12-18 = 343
3. Average of whole array = 50.12
4. Average of elements 12-18 = 49.0
5. Max of whole array = 100
6. Max of elements 12-18 = 98
7. Index of first Max of whole array = 8
8. Index of first Max of elements 12-18 = 13
9. Count of elements below average of whole array = 13
10. Count of elements below average of elements 12-18 = 4 1
1. Rotating once myArray = {72, 45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44, 32, 21, 14, 7, 16, 49, 58}
12. Rotating 5 more times myArray = {14, 7, 16, 49, 58, 72, 45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44, 32, 21}
13. Reversing the array myArray = {21, 32, 44, 75, 57, 98, 16, 72, 48, 55, 100, 69, 15, 79, 82, 89, 18, 22, 45, 72, 58, 49, 16, 7, 14}
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Array.java
import java.util.Arrays;
public class Array {
// returns the sum of elements in the array
public static int sum(int[] arr) {
// calling the second method passing first and last indices of array
return sum(arr, 0, arr.length - 1);
}
// returns the sum of elements in the array between the indices, -666 if
// indices are invalid
public static int sum(int[] arr, int firstIndex, int lastIndex) {
// validating index
if (firstIndex < 0 || firstIndex >= arr.length || lastIndex < 0
|| lastIndex >= arr.length || firstIndex > lastIndex) {
// invalid
return -666;
}
// if first index == last index, returning element at firstIndex
// (base condition)
if (firstIndex == lastIndex) {
return arr[firstIndex];
}
// recursively adding current element to the sum of elements from next
// index and returning it
return arr[firstIndex] + sum(arr, firstIndex + 1, lastIndex);
}
// returns the average of elements in the array
public static double average(int[] arr) {
// using the second function to calculate and return the average
return average(arr, 0, arr.length - 1);
}
// returns the average of elements in the array in the specified range
public static double average(int[] arr, int firstIndex, int lastIndex) {
if (firstIndex < 0 || firstIndex >= arr.length || lastIndex < 0
|| lastIndex >= arr.length || firstIndex > lastIndex) {
return -666;
}
// finding sum using the sum method
double sumTotal = sum(arr, firstIndex, lastIndex);
// finding the count of values
int count = lastIndex - firstIndex + 1;
// finding and returning average
double avg = (double) sumTotal / count;
return avg;
}
// returns the maximum of elements in the array
public static int maxValue(int[] arr) {
return maxValue(arr, 0, arr.length - 1);
}
// returns the maximum of elements in the array in the specified range
public static int maxValue(int[] arr, int firstIndex, int lastIndex) {
if (firstIndex < 0 || firstIndex >= arr.length || lastIndex < 0
|| lastIndex >= arr.length || firstIndex > lastIndex) {
return -666;
}
// returning this element if index first=last
if (firstIndex == lastIndex) {
return arr[firstIndex];
}
// recursively finding the maximum of remaining values
int max = maxValue(arr, firstIndex + 1, lastIndex);
// returning the maximum value among arr[firstIndex] and max
if (arr[firstIndex] > max) {
return arr[firstIndex];
} else {
return max;
}
}
// returns the index of maximum of elements in the array
public static int indexOfFirstMaxValue(int[] arr) {
return indexOfFirstMaxValue(arr, 0, arr.length - 1);
}
// returns the index of maximum of elements in the array in the specified
// range
public static int indexOfFirstMaxValue(int[] arr, int firstIndex,
int lastIndex) {
if (firstIndex < 0 || firstIndex >= arr.length || lastIndex < 0
|| lastIndex >= arr.length || firstIndex > lastIndex) {
return -1;
}
// returning firstIndex if first=last
if (firstIndex == lastIndex) {
return firstIndex;
}
// finding index of max value in remaining elements recursively
int indexMax = indexOfFirstMaxValue(arr, firstIndex + 1, lastIndex);
// returning the index which points to the highest value
if (arr[firstIndex] > arr[indexMax]) {
return firstIndex;
} else {
return indexMax;
}
}
// returns the number of values below the average of elements in the array
public static int numberOfBelowAverageElements(int[] arr) {
return numberOfBelowAverageElements(arr, 0, arr.length - 1);
}
// returns the number of values below the average of elements in the array
// in the specified range
public static int numberOfBelowAverageElements(int[] arr, int firstIndex,
int lastIndex) {
if (firstIndex < 0 || firstIndex >= arr.length || lastIndex < 0
|| lastIndex >= arr.length || firstIndex > lastIndex) {
return -666;
}
// finding average
double avg = average(arr, firstIndex, lastIndex);
int count = 0;
// looping and counting the values under avg
for (int i = firstIndex; i <= lastIndex; i++) {
if (arr[i] < avg) {
count++;
}
}
return count;
}
// rotate the array right once
public static void rotateElements(int[] arr) {
// calling the second method, passing 1 as rotation count
rotateElements(arr, 1);
}
// rotate the array right for rotationCount times
public static void rotateElements(int[] arr, int rotationCount) {
// doing nothing if array is empty
if (arr.length == 0) {
return;
}
// looping for rotationCount number of times
for (int i = 0; i < rotationCount; i++) {
// storing last element
int last = arr[arr.length - 1];
// shifting all elements one place to right
for (int j = arr.length - 1; j > 0; j--) {
arr[j] = arr[j - 1];
}
//storing previous last element in first position
arr[0] = last;
}
}
// reverses the array
public static void reverseArray(int[] arr) {
for (int i = 0; i <= arr.length / 2; i++) {
// swapping elements at i position from front and rear
int temp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}
}
// main method for testing
public static void main(String[] args) {
int myArray[] = { 45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16,
98, 57, 75, 44, 32, 21, 14, 7, 16, 49, 58, 72 };
System.out.println("myArray : " + Arrays.toString(myArray));
System.out.println("Sum of whole array = " + sum(myArray));
System.out.println("Sum of elements 12-18 = " + sum(myArray, 12, 18));
System.out.println("Average of whole array = " + average(myArray));
System.out.println("Average of elements 12-18 = "
+ average(myArray, 12, 18));
System.out.println("Max of whole array = " + maxValue(myArray));
System.out.println("Max of elements 12-18 = "
+ maxValue(myArray, 12, 18));
System.out.println("Index of first Max of whole array = "
+ indexOfFirstMaxValue(myArray));
System.out.println("Index of first Max of elements 12-18 = "
+ indexOfFirstMaxValue(myArray, 12, 18));
System.out.println("Count of elements below average of whole array = "
+ numberOfBelowAverageElements(myArray));
System.out
.println("Count of elements below average of elements 12-18 = "
+ numberOfBelowAverageElements(myArray, 12, 18));
rotateElements(myArray);
System.out.println("Rotating once myArray = "
+ Arrays.toString(myArray));
rotateElements(myArray, 5);
System.out.println("Rotating 5 more times myArray = "
+ Arrays.toString(myArray));
reverseArray(myArray);
System.out.println("Reversing array myArray = "
+ Arrays.toString(myArray));
}
}
/*OUTPUT*/
myArray : [45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44, 32, 21, 14, 7, 16, 49, 58, 72]
Sum of whole array = 1253
Sum of elements 12-18 = 343
Average of whole array = 50.12
Average of elements 12-18 = 49.0
Max of whole array = 100
Max of elements 12-18 = 98
Index of first Max of whole array = 8
Index of first Max of elements 12-18 = 13
Count of elements below average of whole array = 13
Count of elements below average of elements 12-18 = 4
Rotating once myArray = [72, 45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44, 32, 21, 14, 7, 16, 49, 58]
Rotating 5 more times myArray = [14, 7, 16, 49, 58, 72, 45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44, 32, 21]
Reversing array myArray = [21, 32, 44, 75, 57, 98, 16, 72, 48, 55, 100, 69, 15, 79, 82, 89, 18, 22, 45, 72, 58, 49, 16, 7, 14]