Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Fill in the definitions to the three ArrayList utility methods in the ALUtils class which is outlined below and included in the code pack for

Fill in the definitions to the three ArrayList utility methods in the ALUtils class which is outlined below and included in the code pack for this lab. 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 static  ArrayList 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 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.

public static void demo_reverse(){ System.out.println("TESTING REVERSE"); ArrayList sa = 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 and creates a rotated copy 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.

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(){ ArrayList intsL; 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.

ArrayList evens = 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

Tests:

// Public tests for Lab10 ALUtils

import org.junit.*; import static org.junit.Assert.*; import java.util.*; import java.io.*;

public class Lab10Tests { /*Main method runs all tests in this file*/ public static void main(String args[]) { org.junit.runner.JUnitCore.main("Lab10Tests"); } public static ArrayList prepare (T[] arr){ return new ArrayList<>(Arrays.asList(arr)); } @Test public void test_rev01() { assertEquals(new ArrayList<>(), ALUtils.reverse((ArrayList) null)); } @Test public void test_rev02() { assertEquals(prepare(new Integer[]{}), ALUtils.reverse(prepare(new Integer[]{}))); } @Test public void test_rev03() { assertEquals(prepare(new Integer[]{1}), ALUtils.reverse(prepare(new Integer[]{1}))); } @Test public void test_rev04() { assertEquals(prepare(new Integer[]{3,2,1}), ALUtils.reverse(prepare(new Integer[]{1,2,3}))); } @Test public void test_rev05() { assertEquals(prepare(new Integer[]{4,5,6,3,2,1}), ALUtils.reverse(prepare(new Integer[]{1,2,3,6,5,4}))); } @Test public void test_rev06() { assertEquals(prepare(new String[]{"t","a","c"}), ALUtils.reverse(prepare(new String[]{"c","a","t"}))); } @Test public void test_rev07() { assertEquals(prepare(new Double[]{6.5,4.25,3.125}), ALUtils.reverse(prepare(new Double[]{3.125,4.25,6.5}))); } @Test public void test_rev08() { ArrayList orig = new ArrayList<>(Arrays.asList(new Integer[]{2,4,6,8})); ArrayList origCopy = new ArrayList<>(Arrays.asList(new Integer[]{2,4,6,8})); ArrayList giro = ALUtils.reverse(orig); ArrayList expected = prepare(new Integer[]{8,6,4,2}); assertEquals(expected, giro); // get the right answer, and assertEquals(origCopy, orig); // don't change the original. } @Test public void test_rotate01() { assertEquals( new ArrayList<>(), ALUtils.rotate((ArrayList)null,5));} @Test public void test_rotate02() { assertEquals( prepare(new Integer[]{1,2,3,4,5}), ALUtils.rotate(prepare(new Integer[]{1,2,3,4,5}),0));} @Test public void test_rotate03() { assertEquals( prepare(new Integer[]{5,1,2,3,4}), ALUtils.rotate(prepare(new Integer[]{1,2,3,4,5}),1));} @Test public void test_rotate04() { assertEquals( prepare(new Integer[]{4,6,8,10,2}), ALUtils.rotate(prepare(new Integer[]{2,4,6,8,10}),4));} @Test public void test_rotate05() { assertEquals( prepare(new Integer[]{8,9,5,6,7}), ALUtils.rotate(prepare(new Integer[]{5,6,7,8,9}),-3));} @Test public void test_rotate06() { assertEquals( prepare(new String[]{"X","Y","Z","A","B","C","D"}), ALUtils.rotate(prepare(new String[]{"A","B","C","D","X","Y","Z"}),3));} @Test public void test_rotate07() { assertEquals( prepare(new String[]{"Z","A","B","C","D","X","Y"}), ALUtils.rotate(prepare(new String[]{"A","B","C","D","X","Y","Z"}),29));} @Test public void test_rotate08() { assertEquals( prepare(new Boolean[]{true, false, false, true, false}), ALUtils.rotate(prepare(new Boolean[]{true,false,true,false,false,}),-12));} @Test public void test_rotate09() { ArrayList orig = new ArrayList<>(Arrays.asList(new Integer[]{2,4,6,8})); ArrayList origCopy = new ArrayList<>(Arrays.asList(new Integer[]{2,4,6,8})); ArrayList got = ALUtils.rotate(orig,2); ArrayList expected = prepare(new Integer[]{6,8,2,4}); assertEquals(expected, got ); // get the right answer, and assertEquals(origCopy, orig); // don't change the original. } @Test public void test_interlace01() { assertEquals(prepare(new Integer[]{}), ALUtils.interlace((ArrayList)null,prepare(new Integer[]{}))); } @Test public void test_interlace02() { assertEquals(prepare(new Integer[]{}), ALUtils.interlace(prepare(new Integer[]{}),(ArrayList)null)); } @Test public void test_interlace03() { assertEquals(prepare(new Integer[]{1,2,3,4,5,6}), ALUtils.interlace(prepare(new Integer[]{1,3,5}),prepare(new Integer[]{2,4,6}))); } @Test public void test_interlace04() { assertEquals(prepare(new Integer[]{1,3,5}), ALUtils.interlace(prepare(new Integer[]{1,3,5}),prepare(new Integer[]{}))); } @Test public void test_interlace05() { assertEquals(prepare(new Integer[]{10,20,30}), ALUtils.interlace(prepare(new Integer[]{}),prepare(new Integer[]{10,20,30}))); } @Test public void test_interlace06() { assertEquals(prepare(new String[]{"A","B","C","D","E","F","G","H"}), ALUtils.interlace(prepare(new String[]{"A","C","E"}),prepare(new String[]{"B","D","F","G","H"}))); } @Test public void test_interlace07() { assertEquals(prepare(new Integer[]{5,1,4,2,3,3,2,4,1,5}), ALUtils.interlace(prepare(new Integer[]{5,4,3,2,1}),prepare(new Integer[]{1,2,3,4,5}))); } @Test public void test_interlace08() { ArrayList origA = new ArrayList<>(Arrays.asList(new Integer[]{1,3,5,7})); ArrayList origB = new ArrayList<>(Arrays.asList(new Integer[]{2,4,6,8})); ArrayList origACopy = new ArrayList<>(Arrays.asList(new Integer[]{1,3,5,7})); ArrayList origBCopy = new ArrayList<>(Arrays.asList(new Integer[]{2,4,6,8})); ArrayList got = ALUtils.interlace(origA,origB); ArrayList expected = prepare(new Integer[]{1,2,3,4,5,6,7,8}); assertEquals(expected, got ); // get the right answer, and assertEquals(origACopy, origA); // don't change the originals assertEquals(origBCopy, origB); } }

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

Build It For The Real World A Database Workbook

Authors: Wilson, Susan, Hoferek, Mary J.

1st Edition

0073197599, 9780073197593

More Books

Students also viewed these Databases questions