Question: Lab 3 When finished upload the following files : ArrayList.java source file LinkedList.java source file LabDeleteListIterator.java source file RefListIterator.java source file ArrayListIterator.java source file Output

Lab 3

When finished upload the following files

: ArrayList.java source file

LinkedList.java source file

LabDeleteListIterator.java source file

RefListIterator.java source file

ArrayListIterator.java source file

Output produced by all three Drivers.

1. Define an equals(Object) method for Lists. Two lists are equal if and only if they store the same values (in the same order). An ArrayList can be equal to a LinkedList. You can put the API in your List interface:

/** @return true only if the parameter obj is a List and contains the same

elements

* (in the same sequence) as this List.

*/

boolean equals (Object obj);

You may use the instanceof operator to determine whether the parameter is a List. If so, cast it as a List, storing a

reference to it in a local variable. Your solution should be efficient. Test your solution with DriverLabEquals.

Hints:

First compare the sizes of the two lists. If they have different sizes, they could not be equal.

Use Iterators.

2. Define a class named Student in your listDriver package. Every Student has a name, which is a String,

and every Student has an id number, which is an int. Two Students are considered equal if they have the same id

number, even if they have different names. Also include methods getName() and getID() in that class. In the driver,

LabDeleteListIterator, there is a method which needs to be completed. Its name is deleteFirstOfDup(). It is

supposed to search a List named roster for the first pair of neighboring Students which are equal; then it should

delete the first Student of that pair from the List.

You may give the main method a parameter, but do not make other changes to the driver.

Hint: Use a ListIterator.

3. Include an add(E) method in your ListIterator interface. It should add a value to the List

just prior to the implicit cursor. (See the API in the java.util package)

/** Insert the given value just prior to the implicit cursor position. A subsequent

call to previous()

should return the inserted value, and a subsequent call to next() should be

unaffected.

*/public void add (E value);

Implement the add(E) method in your ArrayListIterator and RefListIterator classes.

Test your solution using DriverLabListIterator.

DriverLabEquals.java

package listDriver; import list.*;

/** * Data Structures and Algorithms. * *Compare lists for equality * * @author (sdb) * @version (2020) */ public class DriverLabEquals { static final int MAX = 1000000; public static void main() { System.out.println ("Testing the equals(Object) method for lists"); List strings1 = new LinkedList (); List strings2 = new ArrayList (); if (! strings1.equals(strings2)) System.err.println ("Error"); if (! strings2.equals(strings1)) System.err.println ("Error"); init(strings1); init(strings2); testEquals(strings1, strings2); System.out.println ("Testing finished"); }

private static void init(List list) { java.util.Random rand = new java.util.Random(10); //Random number generator for (int i=0; i

private static void testEquals (List l1, List l2) { if (l1.equals("l1")) System.err.println ("Error in equals(Object)"); if (l2.equals("l2")) System.err.println ("Error in equals(Object)"); if (! l1.equals(l2)) System.err.println ("Error in equals(Object)"); if (! l2.equals(l1)) System.err.println ("Error in equals(Object)"); l1.add ("Mary"); l2.add (new String ("Mary")); if (! l1.equals(l2)) System.err.println ("Error in equals(Object)"); if (! l1.equals(l2)) System.err.println ("Error in equals(Object)"); l1.add("Joe"); l1.add("joe"); l2.add("joe"); l2.add("joe"); if (l1.equals(l2)) System.err.println ("Error in equals(Object)"); if (l1.equals(l2)) System.err.println ("Error in equals(Object)"); } }

DriverLabListIterator.java

package listDriver;

import list.*; /** * Test Iterators and ListIterators. * * @author (sdb) * @version (Sep 2020) */ public class DriverLabListIterator { public static void main(String [] args) { List names; System.out.println ("Testing Iterators for ArrayLists"); testIterators (new ArrayList()); System.out.println ("Testing Iterators for LinkedLists"); testIterators (new LinkedList()); System.out.println();

System.out.println ("Testing ListIterators for ArrayLists"); testListIterators (new ArrayList ()); System.out.println (" Testing ListIterators for LinkedLists"); testListIterators (new LinkedList ()); }

private static void testIterators (List names) { names.add ("jim"); names.add ("mary"); names.add ("joe"); names.add ("sue"); Iterator itty = names.iterator(); while (itty.hasNext()) if (itty.next().length() > 3) itty.remove(); System.out.println (names); // should be [jim, joe, sue] } private static void testListIterators (List names) { names.add ("james"); names.add ("mary"); names.add ("joe"); names.add ("sue"); System.out.println (names); ListIterator itty = names.listIterator(); System.out.println ("Test the add method:"); System.out.println (itty.next()); // "james" System.out.println (itty.next()); // "mary" itty.add ("harry"); System.out.println (names); // [james, mary, harry, joe, sue] System.out.println (itty.next()); // "joe" itty.add ("bill"); System.out.println (itty.previous()); // bill itty.add ("james"); System.out.println (itty.next()); // bill System.out.println (names); // [james, mary, harry, joe, james, bill, sue] if (names.size() != 7) System.err.println ("Error in ListIterator");

System.out.println("Names in reverse order: "); itty = names.listIterator(names.size()); while (itty.hasPrevious()) System.out.print(itty.previous()+" "); // sue bill james joe harry mary james System.out.println(); }

LabDeleteListIterator.java

package listDriver; import list.*;

/** * Lab Problem. Method to search a list for the first pair of equal neighbors. * Delete the first member of that pair. * * @author (sdb and PUT YOUR NAME HERE!) * @version (2020) */ public class LabDeleteListIterator { static List roster; public static void main() { roster = new ArrayList(); test(); roster = new LinkedList(); test(); } private static void init() { deleteFirstOfDup(); roster.add (new Student ("jim", 2222)); deleteFirstOfDup(); roster.add (new Student ("joseph", 2345)); roster.add (new Student ("joe", 2345)); roster.add (new Student ("mary", 3333)); roster.add (new Student ("maryLou", 3333)); System.out.println("Before deletions " + roster); } private static void test() { init(); deleteFirstOfDup();

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!