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

Consider the following Java method below that is passed a generic List ADT. If o

ID: 3664960 • Letter: C

Question

Consider the following Java method below that is passed a generic List ADT.

If origL initially contains these three letter words in this order [ "eat", "you", "new", "ate", "pie", "old", "bit", "ham", "how", "now" ],what will origL contain after method secret is executed? To get the maximum practice from this question, carefully trace the code on the input above rather than attempting to run it.

Assume the following:

The ListADT<E>, Iterable<E> and Iterator<E> interfaces are as discussed in lecture.

The SimpleArrayList<E> class implements the ListADT<E> and Iterable<E> interfaces.

The SimpleArrayListIterator<E> class implements the Iterator<E> interface.

The items can be compared with the equals method.

null and duplicate items are not in ListADT instances.

The NullListException class is implemented as an unchecked exception with a zero-argument constructor.

Complete the Java generic method specified below, which is part of some main class. To receive full credit, your solution:

May use the add and remove List ADT methods.

May not use either the get or contains List ADT methods.

Must explicitly use iterators for traversing lists. May not use for-each loops.

May not use the remove Iterator method.

Must not change the contents of any of the parameters.

Must produce the resulting list in the order shown in the examples below.

Explanation / Answer

Part 1:
After first loop :
Old :- you ate old ham now
New :- eat new pie bit how

After second loop
Old :- you ate old ham now eat new pie bit how
New :- <empty>

newL.add(i, origL.remove(i)); -- It means add at position 'i' the element returned from origL.remove(i)
* And after the 'remove' operation on the list, indexes of element also changes to fill the empty space created
* When
origL.add(newL.remove(0)); is executed
It always removes the first element and the remaining elements rearranges them.
Setting the second element to position of first, and so on...

Part 2:
public static <E> ListADT<E> intersection( ListADT<E> list1, ListADT<E> list2 ) {
// If list1 or list2 (or both) is null, throw a NullListException.
// If list1 or list2 (or both) is empty, return a new empty list.
// Otherwise return a new list that contains the objects that are in both list1 and list2.
// For example:
// list1: A,B,C list2: X,Y,Z result: empty
// list1: A,B,C,D list2: Z,D,Y,B,X result: B,D
// list1: A,B,C list2: A,B,C,Z result: A,B,C
// list1: A,B,C,X list2: C,B,A result: A,B,C
// list1: X,C,B,A list2: A,Z,C,B result: C,B,A

Steps :
1). Sort the two ListADTs in ascending order
2). Traverse the ListADTs simultaneously as per below logic :
(a). if the two elements of the list are equal, add element to result list, and move both the iteartors one step ahead
(b). if (iter1->element) > (iter2->element), move one step ahead iterator2
(c). if (iter1->element) < (iter2->element), move one step ahead iterator1
Repeat step(2). until any one list reaches its end


new Comparator<E>() {
@Override
public int compare(E o1, E o2) {
return o1 < o2;
}
}

public static <E> ListADT<E> intersection( ListADT<E> list1, ListADT<E> list2 )
{
ListADT<E> result = new ListADT<E>();
/* Sort the two input lists - start */
sort(list1<E>, Comparator<E>);
sort(list2<E>, Comparator<E>);
/* Sort the two input lists - end */

/* Logic to find the intersection - start */
Iterator<E> it1 = list1.iterator();
Iterator<E> it2 = list2.iterator();
E element = new E;
element1 = it1.next();
element2 = it2.next();
for(it1, it2; it1.hasNext() && it2.hasNext; )
{
if(element1 == element2)
{
result.add(element1);
element1 = it1.next();
element2 = it2.next();
}
else if(element1 > element2)
{
// element1 = it1.next();
element2 = it2.next();
}
else if(element1 < element2)
{
element1 = it1.next();
// element2 = it2.next();
}
}
/* Logic to find the intersection - end */

}

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