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
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.
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 staticArrayList reverse(ArrayList xs){ // Your definition here throw new UnsupportedOperationException("unimplemented!"); } /** * 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 ArrayList rotate(ArrayList xs, int shift){ // Your definition here throw new UnsupportedOperationException("unimplemented!"); } /** * 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 ArrayList interlace(ArrayList xs, ArrayList ys){ // Your definition here throw new UnsupportedOperationException("unimplemented!"); } }
--------------------------------------------------------
Reversal
In ALUtils, write a method reverse(aL) that takes an ArrayList
public static void demo_reverse(){ System.out.println("TESTING REVERSE"); ArrayListsa = new ArrayList (); String [] strings = {"A","B","C","D","E"}; // Use the a for-each to add all strings for(String s : strings){ sa.add(s); } // sa.toString() === "[A, B, C, D, E]" ArrayList sb = ALUtils.reverse(sa); System.out.println(sb); // [E, D, C, B, A] System.out.println(sa); // [A, B, C, D, E] - sa is not changed ArrayList ia = new ArrayList (); Integer[] ints = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; // Use the addAll() method add all ints from a list ia.addAll(Arrays.asList(ints)); // ia.toString() === "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]" ArrayList ib = ALUtils.reverse(ia); System.out.println(ib); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] System.out.println(ia); // [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] - sa not changed }
Rotation
In ALUtils.java, write a static method rotate(aL, shift) which accepts an ArrayList
aL = [ A, B, C, D, E, F] bL = [ A, B, C, D, E, F, G] cL = rotate(aL, 2) cL = rotate(bL, 7) cL== [ E, F, A, B, C, D] cL== [ A, B, C, D, E, F, G] cL = rotate(aL, 7) cL = rotate(bL, 4) cL== [ F, A, B, C, D, E] cL== [ D, E, F, G, A, B, C]
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.
// Demonstrate rotat(a,shift) method public static void demo_rotate(){ ArrayListintsL; intsL = new ArrayList (); int [] ints = { 10, 20, 30, 40, 50, 60}; for(Integer i : ints){ intsL.add(i); } // intsL = [10, 20, 30, 40, 50, 60] ArrayList result1; result1 = ALUtils.rotate(intsL, 2); // result1== [ 50, 60, 10, 20, 30, 40] System.out.println(result1); result1 = ALUtils.rotate(intsL, 7); // result1== [ 60, 10, 20, 30, 40, 50] System.out.println(result1); }
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.
ArrayListevens = new ArrayList (Arrays.asList(new Integer[]{0,2,4})); ArrayList odds = new ArrayList (Arrays.asList(new Integer[]{1,3,5,7,9})); 1System.out.println(ALUtils.interlace(evens,odds)); // [0, 1, 2, 3, 4, 5, 7, 9] System.out.println(ALUtils.interlace(odds,evens)); // [1, 0, 3, 2, 5, 4, 7, 9] ArrayList empty = new ArrayList (); ArrayList some = new ArrayList (Arrays.asList(new Integer[]{6,7,8)); ArrayList combined = ALUtils.interlace(empty,some); System.out.println(ALUtils.interlace(empty,some)); // [6, 7, 8] System.out.println( ALUtils.rotate( ALUtils.reverse( ALUtils.interlace(evens,some)) // 0 6 2 7 4 8 ,2 )); //6 0 8 4 7 2
------------------------
Tester cases: https://paste.ee/p/d2Wl5
Please open the link for the tester. Thank you so much.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started