Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 5: Array-processing methods 35 points total; individual-only In a class named ArrayMethods, implement each of the methods described below. As usual, we highly recommend

Problem 5: Array-processing methods

35 points total; individual-only

In a class named ArrayMethods, implement each of the methods described below.

As usual, we highly recommend adding a main method to your class that makes test calls to your methods. Dont forget to import java.util so that you can use the Arrays.toString method in your tests.

Important guidelines:

  • Your methods must have the exact headers that we have specified, or we wont be able to test them. Note in particular that the case of the letters matters.

  • If you are asked to write a method that returns a value, make sure that it returns the specified value, rather than printing it.

  • You may not use any Java classes that we have not covered in the lecture notes.

  • While you are encouraged to use Arrays.toString() when testing your methods, you must not use it inside the methods themselves. In addition, you must not use any other methods from the Arrays class.

  • Each of your methods should be preceded by a comment that explains what your methods does and what its inputs are. You should also include a comment at the top of the file, similar to the one that we included in Methods.java from Problem Set 1.

  • More generally, use good programming style. Use appropriate indentation, select descriptive variable names, insert blank lines between logical parts of your program, and add comments as necessary to explain what your code does. See the coding conventions for more detail.

  1. Include the following declaration at the beginning of your class, immediately after the class header:

    public static final String[] DAYS = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; 

    It declares a variable called DAYS that refers to an array of strings that you will use in one of your methods.

    The syntax that we are using to declare this variable makes it a class constant. Declaring a variable to be static means it has class-level scope and can be accessed by any method written in the class. Declaring a variable to be final means that it is a constant that cannot be modified by code elsewhere in the program.

    By convention, when declaring a class constant, we capitalize the entire variable name. Doing so makes it easier for readers of our code to recognize that the variable is a constantand to know that they can find its value at the top of the class.

  2. Write a method with the header

    public static int getDayIndex(String day) 

    that takes a reference to a string and returns the index of that string in the array referred to by the class constant DAYS that you declared above. For example:

    • getDayIndex("Sunday") should return 0, because "Sunday" has an index of 0 in the DAYS array.
    • getDayIndex("Wednesday") should return 3, because "Wednesday" has an index of 3 in the DAYS array.

    Important notes:

    • If the parameter is null or the string is not found in the array, the method should return -1.

    • The method should work correctly regardless of the case of the letters in the string that is passed in. For example, the inputs "Monday", "monday", and "MoNDaY" should all produce the same result. Hint: There is more than one way to do this, but any simple approach will take advantage of one or more methods in the String class.

    • Your method should work even if we change the contents of the DAYS array. In other words, you should write code that processes an arbitrary array of String objects called DAYS, and you should not make any assumptions about the strings found in that array.

  3. Write a method with the header

    public static void swapAdjacent(int[] values) 

    that takes a reference to an array of integers values and swaps adjacent pairs of elements: values[0] with values[1], values[2] with values[3], etc.

    For example, if you add the following test code to your main method:

    int[] a1 = {0, 2, 4, 6, 8, 10}; swapAdjacent(a1); System.out.println(Arrays.toString(a1)); 

    you should see the following output:

    [2, 0, 6, 4, 10, 8] 

    In an odd-length array, the last element should not be moved. For example, if you run these statements:

    int[] a2 = {1, 2, 3, 4, 5, 6, 7}; swapAdjacent(a2); System.out.println(Arrays.toString(a2)); 

    you should see:

    [2, 1, 4, 3, 6, 5, 7] 

    Special case: If the parameter is null, the method should throw an IllegalArgumentException.

  4. Write a method with the header

    public static int[] copyCapped(int[] values, int cap) 

    that takes a reference to an array of integers values and an integer cap, and that creates and returns a new array based on values in which all elements greater than cap are replaced by the value cap.

    Important:

    • You may not use any of the built-in methods for copying an array.

    • You must not modify the original array.

    For example:

    int[] a3 = {2, 5, 6, 3, 7, 4, 1}; int[] a4 = ArrayMethods.copyCapped(a3, 4); System.out.println(Arrays.toString(a4)); 

    should display:

    [2, 4, 4, 3, 4, 4, 1] 

    Special case: If the first parameter is null, the method should throw an IllegalArgumentException.

  5. Write a method with the header

    public static int mostFrequentValue(int[] arr) 

    that takes a reference to a sorted array of integers and returns the value that occurs most frequently in the array. For example, consider this array:

    int[] arr = {1, 2, 3, 3, 8, 8, 8, 8, 11, 11, 11, 14, 19, 19}; 

    The method call mostFrequentValue(arr) should return 8, because 8 is the most frequently occurring value in the array, appearing four times.

    The array is guaranteed to be in sorted order. For full credit, you should not use another array as part of your solution, and you should not perform more than one scan through the array from left to right.

    Special cases:

    • If two or more values tie for the most occurrences, return the one that comes first. For example, if we added an extra 11 to the array shown above giving that value four occurrences as well the method should still return 8, because 8 comes before 11 in the array.

    • If all of the values occur exactly once, or if there is only one element, the first element should be returned. You should not need separate code to check for these cases.

    • If the parameter is null, or if the array has a length of 0, the method should throw an IllegalArgumentException.

    Important: You must not modify the original array.

  6. Write a method with the header

    public static int indexOf(int[] arr1, int[] arr2) 

    that takes two arrays containing sequences of integers and that returns the index of the first occurrence of the first sequence in the second sequence, or -1 if the first sequence does not appear in the second sequence. For example, suppose that you have these arrays:

    int[] list1 = {1, 3, 6}; int[] list2 = {1, 3, 5, 8, 12, 1, 3, 17, 1, 3, 6, 9, 1, 3, 6}; 

    then the call indexOf(list1, list2) should return 8 because the sequence of values stored in list1 appears in list2 starting at index 8. Notice that list1 actually appears twice in list2, starting at position 8 and starting at position 12. Your method should return the first such position.

    If the first sequence is not contained in the second sequence, then the method should return -1. For example, if list2 had the same values as before but list1 stored {12, 1, 3, 6}, then the call indexOf(list1, list2) should return -1 because list1 is not contained in list2.

    Special case: If either parameter is null or has a length of 0, the method should throw an IllegalArgumentException.

    Important: You must not modify the original arrays.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions