Question

In: Computer Science

Java Programming II Homework 2-1 In this assignment you are being asked to write some methods...

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}

Solutions

Expert Solution

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]


Related Solutions

Java Programming II Homework 2-1 In this assignment you are being asked to write some methods...
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...
Programming II: C++ - Programming Assignment Vector Overloads Overview In this assignment, the student will write...
Programming II: C++ - Programming Assignment Vector Overloads Overview In this assignment, the student will write a C++ program that overloads the arithmetic operators for a pre-defined Vector object. When completing this assignment, the student should demonstrate mastery of the following concepts: · Object-oriented Paradigm · Operator Overloading - Internal · Operator Overloading - External · Mathematical Modeling Assignment In this assignment, the student will implement the overloaded operators on a pre-defined object that represents a Vector. Use the following...
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered...
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered this week. come up with a “problem scenario” for which you can create a “solution”. Utilize any/all of the examples from the book and class that we discussed. Your program should be interactive and you should give a detailed statement about what the “description of the program – purpose and how to use it”. You should also use a “good bye” message. Remember to...
Java Programming Assignment 6-1 We have covered the “Methods” this week. Apply the concepts that we...
Java Programming Assignment 6-1 We have covered the “Methods” this week. Apply the concepts that we have learnt in class and come up with a “problem scenario” for which you can create a “solution”. Utilize any/all of the examples from the book and class that we discussed. You program should be interactive and you should give a detailed statement about what the “description of the program – purpose and how to use it”. You should also use a “good bye”...
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants...
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants Create a class Die representing a die to roll randomly. ☑ Give Die a public final static int FACES, representing how many faces all dice will have for this run of the program. ☑ In a static block, choose a value for FACES that is a randomly chosen from the options 4, 6, 8, 10, 12, and 20. ☑ Give Die an instance variable...
Programming II: C++ - Programming Assignment Fraction Object with Operator Overloads Overview In this assignment, the...
Programming II: C++ - Programming Assignment Fraction Object with Operator Overloads Overview In this assignment, the student will write a C++ program that implements a “fraction” object. When writing the object, the student will demonstrate mastery of implementing overloaded operators in a meaningful way for the object. When completing this assignment, the student should demonstrate mastery of the following concepts: · Mathematical Modeling - Fractions · Operator Overloading – Binary Operators (Internal Overload) · Operator Overloading – Binary Operator (External...
This programming assignment involves learning about some of the common exceptions that occur in Java programs.
This programming assignment involves learning about some of the common exceptions that occur in Java programs. Consider the following exception types: NullPointerException ArrayIndexOutOfBoundsException ClassCastException IllegalArgumentException Research what each exception type means and the conditions under which each occurs (i.e., is thrown). Write programs that demonstrate each type of exception being thrown (one program per exception) and provide a screen capture of the output. You should write your code so that each exception type is forced to occur. Name your programs...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display: -   The given info of each process -   CPU utilization - The average wait time - Turnaround time for each process...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT