It is supposed to look like this
Week 7 Homework
The number of common elements is: 3
The number of common elements is: 3
I get
Week 7 Homework
The number of common elements is: 0
The number of common elements is: 1
Which is better than my many other attempts, if you could offer any help I would appreciate it.
JoeMac3313
Expand|Select|Wrap|Line Numbers
- import ch06.lists.*;
- public class CompareListsTest
- {
- private static int compareUnsortedLists(UnsortedListInterface list1, UnsortedListInterface list2) {
- int counter = 0;
- list1.reset();
- list2.reset();
- if(list2.contains(list1.getNext())&& list1.getNext() != null)
- {
- counter++;
- }
- return counter;
- }
- public static void main(String[] args) {
- // declare and populate list lst1
- UnsortedListInterface lst1 = new RefUnsortedList();
- lst1.add("alpha");
- lst1.add("tau");
- lst1.add("beta");
- lst1.add("kappa");
- lst1.add("gamma");
- lst1.add("delta");
- // declare and populate list lst2
- UnsortedListInterface lst2 = new RefUnsortedList();
- lst2.add("tim");
- lst2.add("gamma");
- lst2.add("beta");
- lst2.add("peter");
- lst2.add("kappa");
- // Self introduction
- System.out.println("Week 7 Homework");
- // Call compareUnsortedLists
- System.out.println("The number of common elements is: " + compareUnsortedLists(lst1, lst2));
- // The property should be symetric and therefore the same result should be
- // obtained if the parameters are swapped in the method call
- System.out.println("The number of common elements is: " + compareUnsortedLists(lst2, lst1));
- }
- }