QUESTION

For this assignment, you will be designing a custom Array class using generics and adding additional features not available to normal arrays. Use the following code as the basis for your class and add the required methods. You MUST use Java Generics to implement the MyArray class.

public class MyArray<E extends Comparable<E>> {
    //No other data fields necessary.
    private E[] data;
    
    public MyArray(int size) {
        this.data = (E[])(new Comparable[size]);
    }

  • get(start, end):
    • This method shall take two integer parameters start and end.
    • This method shall return a new MyArray object with values between indices start and end inclusive.
    • You must make sure that start and end are valid indices.
    • Throw an IndexOutOfBoundsException if either index is not valid.
  • put(index, value):
    • This method shall take an integer index and a value and place the value at the given index.
    • Verify that the index is valid.
    • Throw an IndexOutOfBoundsException if the index is not valid.
  • put(start, end, any number of comma separated values):
    • This method shall take 3 parameters, the start index, end index, and a variable length parameter list of values of any length.
    • This method shall take the values and place them into the array replacing the values between the start index position and the end index position (inclusive).
    • Validate that start and end are valid indices and throw an IndexOutOfBoundsException if they are not valid.
    • Throw a TooManyValuesException if the list of values is larger than the range of positions indicated by start and end.
    • NOTE: You will need to create the TooManyValuesException class. This should be an unchecked exception.
  • max(): This method shall take no parameters and find and return the maximum value in the array.
  • min(): This method shall take no parameters and find and return the minimum value in the array.
  • reverse():
    • This method shall take no parameters and reverse the array.
    • NOTE: It does not return a new array that is the reverse, it simply reverses the existing array.
  • shuffle():
    • This method shall randomly shuffle the array.
    • NOTE: It does not return a new array, it shuffles the existing array.
  • leftShift(shiftDistance):
    • This method shall take an integer value as a parameter and left shift the elements in the array by the given number of positions.
    • This is a circular shift so the beginning should wrap around to the end.
    • You must validate that shiftDistance is a positive integer.
    • If it is not, throw a ShiftDistanceCannotBeNegativeException.
    • NOTE: You will need to create the ShiftDistanceCannotBeNegativeException class. This should be an unchecked exception.
    • NOTE: This method does not return a new MyArray.
  • rightShift(shiftDistance):
    • This method shall take an integer value as a parameter and right shift the elements in the array by the given number of positions.
    • This is a circular shift so the end should wrap around to the beginning.
    • You must validate that shiftDistance is a positive integer. If it is not, throw a ShiftDistanceCannotBeNegativeException.
    • NOTE: You will need to create the ShiftDistanceCannotBeNegativeException class.
    • This should be an unchecked exception.
    • NOTE: This method does not return a new MyArray.
  • toString(): Implement the toString() method so that it will print a String representation of the array.

Public Answer

JI8YK1 The First Answerer