Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java: Generic ArrayLists Task: Please read. Codes and Requirements are provided

ID: 3702346 • Letter: J

Question

Java: Generic ArrayLists Task: Please read. Codes and Requirements are provided below. Tester cases is also at the bottom. Thank you.

Fill in the definitions to the three ArrayList utility methods in the ALUtils class which is outlined below. The methods are described adequately via Javadoc comments, and there are examples below.

--------------------------------------------------------

Reversal

In ALUtils, write a method reverse(aL) that takes an ArrayList<T> and creates a reversed copy of it which is returned. The nature of this method is shown in the below demo and should produce the results indicated. You may assume that the parameter aL is non-null. While there are some library methods that exactly implement this functionality, take this as an opportunity to write your own loop to reverse the array list.

Rotation

In ALUtils.java, write a static method rotate(aL, shift) which accepts an ArrayList<T> and creates a rotatedcopy of it. Rotation means to shift all elements forward in the list to a new position based on the integer parameter shift. If the elements would shift off the end of the list, they wrap around to the beginning. The concept should be familiar based on projects and is best illustrated by examples given below.

The rotate(aL,shift) method should take the following approach.

Accepts an ArrayList called aL of any type and an integer shift. You may assume shift is a non-negative integer and aL is not null.

Creates a new ArrayList and populates its contents with elements of aL shifted in position by shift. This new ArrayList is returned.

If aL is empty, the return value is an empty ArrayList.

You may assume that the shift parameter isn't negative, and that aL is non-null.

Some additional demonstrations of the expected behavior are below.

Interlacing

In ALUtils.java, write the static method interlace(xs, ys). Both ArrayList arguments must be holding the same type. This method creates and returns a new ArrayList holding the same type of values, and then continually takes an item from xs, then an item from ys, another from xs, another from ys, and so on. If either list is longer, its extra elements still appear at the end. The original lists are unchanged, as before. Examples below.

------------------------

Tester cases: https://paste.ee/p/d2Wl5

Please open the link for the tester. Thank you so much.

Explanation / Answer

// ALUtils.java

import java.util.*;

public class ALUtils {

      

       /**

          * Creates and returns a copy of the parameter xs, with all items in the

          * reversed order. Does not modify the original list. When the parameter

          * is null, the result is an empty list.

          *

          * @param xs the list to create a reversed version

          * @return   the reversed version of the parameter a.

          */

       public static <T> ArrayList<T> reverse(ArrayList<T> xs){

            

             ArrayList<T> reversedList = new ArrayList<T>();

             if(xs != null)

             {

                    for(int i=xs.size()-1;i>=0;i--)

                           reversedList.add(xs.get(i));

             }

             return reversedList;

       }

      

       /**

          * Creates and returns copy of the given ArrayList xs, rotating

          * elements away from the front (and wrapping back in from end to

          * front) as many times as there is positive shift quantity. When

          * xs is null, the answer is an empty list.

          *

          * @param xs the list from which to create a rotated version

          * @param shift # spaces each item shifts away from front (when positive)

          * @return the rotated version of the input list.

          */

       public static <T> ArrayList<T> rotate(ArrayList<T> xs, int shift){

            

             ArrayList<T> rotatedList = new ArrayList<T>();

             if(xs != null)

             {

                    rotatedList.addAll(xs);

                    int i =0;

                    if(shift > 0) {

                           while(i<shift)

                           {

                                 T item = rotatedList.get(rotatedList.size()-1);

                                 rotatedList.remove(rotatedList.size()-1);

                                 rotatedList.add(0, item);

                                 i++;

                           }

                    }

                    else {

                          

                           while(i>shift)

                           {

                                 T item = rotatedList.get(0);

                                 rotatedList.remove(0);

                                 rotatedList.add(rotatedList.size(), item);

                                 i--;

                           }

                    }

             }

            

             return rotatedList;

       }

      

       /**

          * Creates and returns a new ArrayList by interleaving elements from the

          * two parameter ArrayLists. If either list is longer, its extra elements

          * are preserved at the end of the answer list. The answer's size equals

          * the sum of the parameter lists' sizes, always. If either list is null,

          * it is treated like an empty list (contributes no items). The answer is

          * never null.

          *

          * @param xs the first parameter list

          * @param ys the second parameter list

          * @return the interlaced combination of the two lists.

          */

       public static <T> ArrayList<T> interlace(ArrayList<T> xs, ArrayList<T> ys){

             ArrayList<T> interlacedList = new ArrayList<T>();

             int i=0,j=0;

             if(xs !=null && ys != null)

             {

                    for(i=0,j=0;i<xs.size() && j<ys.size();i++,j++)

                    {

                           interlacedList.add(xs.get(i));

                           interlacedList.add(ys.get(j));

                    }

             }

             if(xs!= null)

                    for(;i<xs.size();i++)

                           interlacedList.add(xs.get(i));

             if(ys != null)

                    for(;j<ys.size();j++)

                           interlacedList.add(ys.get(j));

            

             return interlacedList;

       }

}

//end of ALUtils

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote