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 11. 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}
Program:
public class MyClass {
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("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("Number of below average elements in whole
array: "+numberOfBelowAverageElements(myArray));
System.out.println("Number of below average elements in 12-18:
"+numberOfBelowAverageElements(myArray,12,18));
reverseArray(myArray);
}
public static int sum(int[] arr)
{
int sum = 0;
for(int i=0;i<arr.length;i++)
sum = sum + arr[i];
return sum;
}
public static int sum(int[] arr, int firstIndex, int
lastIndex)
{
int sum = 0;
for(int i=firstIndex;i<=lastIndex;i++)
sum = sum + arr[i];
return sum;
}
public static double average(int[] arr)
{
double avg = 0;
int i=0;
for(i=0;i<arr.length;i++)
avg = avg + arr[i];
return avg/i;
}
public static double average(int[] arr, int firstIndex, int
lastIndex)
{
double avg = 0;
int i=0,j=0;
for(i=firstIndex;i<=lastIndex;i++)
{
avg = avg + arr[i];
j++;
}
return avg/j;
}
public static int maxValue(int[] arr)
{
int max = 0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]>max)
max = arr[i];
}
return max;
}
public static int maxValue(int[] arr, int firstIndex, int
lastIndex)
{
int max = 0;
for(int i=firstIndex;i<=lastIndex;i++)
{
if(arr[i]>max)
max = arr[i];
}
return max;
}
public static int indexOfFirstMaxValue(int[] arr)
{
int max = 0,j=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]>max)
{
j=i;
max = arr[i];
}
}
return j;
}
public static int indexOfFirstMaxValue(int[] arr, int firstIndex,
int lastIndex)
{
int max = 0,j=0;
for(int i=firstIndex;i<=lastIndex;i++)
{
if(arr[i]>max)
{
j=i;
max = arr[i];
}
}
return j;
}
public static int numberOfBelowAverageElements(int[] arr)
{
double avg = average(arr);
int i=0,j=0;
for(i=0;i<arr.length;i++)
{
if(arr[i]<avg)
j++;
}
return j;
}
public static int numberOfBelowAverageElements(int[] arr, int
firstIndex, int lastIndex)
{
double avg = average(arr, firstIndex, lastIndex);
int i=0,j=0;
for(i=firstIndex;i<=lastIndex;i++)
{
if(arr[i]<avg)
j++;
}
return j;
}
public static void reverseArray(int[] arr)
{
int[] b = new int[arr.length];
int j = arr.length;
for (int i = 0; i < arr.length; i++) {
b[j - 1] = arr[i];
j = j - 1;
}
/*printing the reversed array*/
System.out.println("Reversed array is: \n");
for (int k = 0; k < arr.length; k++) {
System.out.print(b[k] + " ");
}
}
}
Output: