Question: this is java problem. Create a package named prob1. Create a class with a main named ListUtilities . Write a static generic method, interleave that

this is java problem.

Create a package named prob1.

Create a class with a main named ListUtilities.

Write a static generic method, interleave that accepts two lists of any type and an integer, n. The method should return a list of of the generic type that interleaves the elements from the two lists, n-at-a-time. For example, if the two input lists are: and , and n=2 then the new list will be: . There are several rules for the interleaving:

null is returned if either the first or the second list do not contain n elements.

The first n elements in the returned list always come from the first list, the second n elements come from the second list, then repeat.

The length of the returned list is always a multiple of n and the number of elements from each list, that are in the returned list, must each be a multiple of n.

The number of elements in the returned list that come from the first list must either equal the number of elements that come from the second list, or the number of elements from the first list can be exactly n more than from the second list (i.e. one final tuple of size n from the first list, if it is available).

For examples, see Appendix 1 for the output for the test cases and Appendices 2 and 3 for test code and another support class that is needed.

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

Appendix 1

Test 1a - list sizes equal

l1=[1, 3, 5, 7, 9, 11]

l2=[2, 4, 6, 8, 10, 12]

interleave(l1,l2,1)=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

Test 1b - list sizes equal

l1=[1, 2, 5, 6, 9, 10]

l2=[3, 4, 7, 8, 11, 12]

interleave(l1,l2,2)=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

Test 1b - list sizes equal

l1=[1, 2, 3, 7, 8, 9]

l2=[4, 5, 6, 10, 11, 12]

interleave(l1,l2,3)=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

Test 2a - list 1 > list 2

l1=[1, 3, 5, 6]

l2=[2, 4]

interleave(l1,l2,1)=[1, 2, 3, 4, 5]

Test 2b - list 1 > list 2

l1=[1, 2, 5, 6]

l2=[3, 4]

interleave(l1,l2,2)=[1, 2, 3, 4, 5, 6]

Test 2c - list 1 > list 2

l1=[1, 2, 3, 6]

l2=[4, 5]

interleave(l1,l2,3)=null

Test 3a - list 1 < list 2

l1=[1, 3]

l2=[2, 4, 5, 6]

interleave(l1,l2,1)=[1, 2, 3, 4]

Test 3b - list 1 < list 2

l1=[1, 2]

l2=[3, 4, 5, 6]

interleave(l1,l2,2)=[1, 2, 3, 4]

Test 4a

l1=[1, 2, 3, 7, 8, 9, 13]

l2=[4, 5, 6, 10, 11, 12]

interleave(l1,l2,3)=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

Test 4b

l1=[1, 2, 3, 7, 8, 9, 13, 14]

l2=[4, 5, 6, 10, 11, 12]

interleave(l1,l2,3)=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

Test 4c

l1=[1, 2, 3, 7, 8, 9, 13, 14, 15]

l2=[4, 5, 6, 10, 11, 12]

interleave(l1,l2,3)=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

Test 4d

l1=[1, 2, 3, 7, 8, 9, 13, 14, 15]

l2=[4, 5, 6, 10, 11, 12, 16]

interleave(l1,l2,3)=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

Test 5-Lists of Dogs

dogs1=[d(1), d(2), d(5), d(6)]

dogs2=[d(3), d(4), d(7)]

interleave(dogs1,dogs2,2)=[d(1), d(2), d(3), d(4), d(5), d(6)]

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

Appendix 2

Copy this code into main for the ListUtilities class for Problem 1 to test.

List l1;

List l2;

System.out.println("Test 1a - list sizes equal");

l1 = new ArrayList(Arrays.asList(1,3,5,7,9,11));

l2 = new ArrayList(Arrays.asList(2,4,6,8,10,12));

System.out.println("l1=" + l1);

System.out.println("l2=" + l2);

System.out.println("interleave(l1,l2,1)=" + interleave(l1,l2,1));

System.out.println(" Test 1b - list sizes equal");

l1 = new ArrayList(Arrays.asList(1,2,5,6,9,10));

l2 = new ArrayList(Arrays.asList(3,4,7,8,11,12));

System.out.println("l1=" + l1);

System.out.println("l2=" + l2);

System.out.println("interleave(l1,l2,2)=" + interleave(l1,l2,2));

System.out.println(" Test 1b - list sizes equal");

l1 = new ArrayList(Arrays.asList(1,2,3,7,8,9));

l2 = new ArrayList(Arrays.asList(4,5,6,10,11,12));

System.out.println("l1=" + l1);

System.out.println("l2=" + l2);

System.out.println("interleave(l1,l2,3)=" + interleave(l1,l2,3));

System.out.println(" Test 2a - list 1 > list 2");

l1 = new ArrayList(Arrays.asList(1,3,5,6));

l2 = new ArrayList(Arrays.asList(2,4));

System.out.println("l1=" + l1);

System.out.println("l2=" + l2);

System.out.println("interleave(l1,l2,1)=" + interleave(l1,l2,1));

System.out.println(" Test 2b - list 1 > list 2");

l1 = new ArrayList(Arrays.asList(1,2,5,6));

l2 = new ArrayList(Arrays.asList(3,4));

System.out.println("l1=" + l1);

System.out.println("l2=" + l2);

System.out.println("interleave(l1,l2,2)=" + interleave(l1,l2,2));

System.out.println(" Test 2c - list 1 > list 2");

l1 = new ArrayList(Arrays.asList(1,2,3,6));

l2 = new ArrayList(Arrays.asList(4,5));

System.out.println("l1=" + l1);

System.out.println("l2=" + l2);

System.out.println("interleave(l1,l2,3)=" + interleave(l1,l2,3));

System.out.println(" Test 3a - list 1 < list 2");

l1 = new ArrayList(Arrays.asList(1,3));

l2 = new ArrayList(Arrays.asList(2,4,5,6));

System.out.println("l1=" + l1);

System.out.println("l2=" + l2);

System.out.println("interleave(l1,l2,1)=" + interleave(l1,l2,1));

System.out.println(" Test 3b - list 1 < list 2");

l1 = new ArrayList(Arrays.asList(1,2));

l2 = new ArrayList(Arrays.asList(3,4,5,6));

System.out.println("l1=" + l1);

System.out.println("l2=" + l2);

System.out.println("interleave(l1,l2,2)=" + interleave(l1,l2,2));

System.out.println(" Test 4a");

l1 = new ArrayList(Arrays.asList(1,2,3,7,8,9,13));

l2 = new ArrayList(Arrays.asList(4,5,6,10,11,12));

System.out.println("l1=" + l1);

System.out.println("l2=" + l2);

System.out.println("interleave(l1,l2,3)=" + interleave(l1,l2,3));

System.out.println(" Test 4b");

l1 = new ArrayList(Arrays.asList(1,2,3,7,8,9,13,14));

l2 = new ArrayList(Arrays.asList(4,5,6,10,11,12));

System.out.println("l1=" + l1);

System.out.println("l2=" + l2);

System.out.println("interleave(l1,l2,3)=" + interleave(l1,l2,3));

System.out.println(" Test 4c");

l1 = new ArrayList(Arrays.asList(1,2,3,7,8,9,13,14,15));

l2 = new ArrayList(Arrays.asList(4,5,6,10,11,12));

System.out.println("l1=" + l1);

System.out.println("l2=" + l2);

System.out.println("interleave(l1,l2,3)=" + interleave(l1,l2,3));

System.out.println(" Test 4d");

l1 = new ArrayList(Arrays.asList(1,2,3,7,8,9,13,14,15));

l2 = new ArrayList(Arrays.asList(4,5,6,10,11,12,16));

System.out.println("l1=" + l1);

System.out.println("l2=" + l2);

System.out.println("interleave(l1,l2,3)=" + interleave(l1,l2,3));

System.out.println(" Test 5-Lists of Dogs");

List dogs1 = new ArrayList<>(Arrays.asList(new Dog(1), new Dog(2), new Dog(5), new Dog(6)));

List dogs2 = new ArrayList<>(Arrays.asList(new Dog(3), new Dog(4), new Dog(7)));

System.out.println("dogs1=" + dogs1);

System.out.println("dogs2=" + dogs2);

System.out.println("interleave(dogs1,dogs2,2)=" + interleave(dogs1,dogs2,2));

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

Appendix 3

Create a Dog class for Problem 1 and copy this code into it.

public class Dog {

private int val;

public Dog(int val) {

this.val = val;

}

public String toString() {

return "d(" + val + ")";

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!