Solutions and rubrics for Lab 10 (Lists and Sets)

The following was emailed to the UIs: 

Lab 10 contains two problems. Each is worth 50%. The first problem has two parts 
(union and intersection) and each is worth the same. The second problem is also in 
two parts (union and intersection) and each one is worth the same. Early on I 
explained that we grade as follows: for each problem we try to understand what the 
student attempted to accomplish and if all is well we give all the points. If 
something is missing we try to evaluate (according to the expressed intent in the 
code, in other words based on the approach and style in which the student 
attempted the problem evaluate/assess) what is missing. From that we evaluate the 
simplest fix to the code and the amount of points it's worth (like they have done 
it three times by now in their labs: what needs to be erased, what needs to be 
added and what's the reasoning that determined those specifically, so as I 
mentioned many times we take away points for fixes due to parts and labor). After 
you evaluate the cheapest way to fix the students' code you take that amount of 
fix from 100 (for each part or subpart of the problem) and then you take the 
average as I indicated above and that's the grade/score you post. A short 
statement explaining what was wrong, what needed to be added and how you figured 
that out should be added as feedback to indicate how you reached those scores. If 
the comment is missing and/or unclear students should talk to you about that in 
lab or office hours.

Here's a sample solution for the first problem (due to Feng Lou). <p> 

// this is the first lab problem, please understand this solution is provided as an
// example only, since there are many other ways in which you could have approached
// this problem as the TAs should have discussed them with you in lab/office hours

import java.util.*;

public class Second { // this is problem R15.18
  public static <T> LinkedList<T> union (LinkedList<T> List1, LinkedList<T> List2){
    LinkedList<T> temp = new LinkedList<T>(List1);
    ListIterator<T> iter2 = List2.listIterator();
    
    while(iter2.hasNext()){
      T s = iter2.next();
      if(!List1.contains(s)){
        temp.add(s);
      }
    }
    return temp;
  }
  public static <T> LinkedList<T> intersection (LinkedList<T> List1, LinkedList<T> List2){  
    LinkedList<T> temp = new LinkedList<T>(List1);
    ListIterator<T> iter2 = temp.listIterator();
    while(iter2.hasNext()){
      T s = iter2.next();
      if(!List2.contains(s)){
        temp.remove(s);
      }
    }
    return temp;
  }    
    
    
  public static void main(String[] args){
    LinkedList<String> staff1 = new LinkedList<String>();
    staff1.add("a");
    staff1.add("b");
    staff1.add("c");
    LinkedList<String> staff2 = new LinkedList<String>();
    staff2.add("a");
    staff2.add("b");
    staff2.add("d");   
    System.out.println(union(staff1,staff2).toString());
    System.out.println(intersection(staff1,staff2).toString());
  }
}

Here's a sample solution to the second problem (also due to Feng Lou). <p> 

// this is the second problem, please understand this solution is provided as an 
// example only, since there are many other ways in which you could have approached
// this problem as the TAs should have discussed them with you in lab/office hours

import java.util.*;

public class Second { // this is problem R15.19
  public static HashSet<String> unionSet(LinkedList<String> u1, LinkedList<String> u2){
    HashSet<String> staff = new HashSet<String>(u1);
    staff.addAll(u2);
    return staff;
  }
  public static HashSet<String> intersectionSet(LinkedList<String> u1, LinkedList<String> u2){
    HashSet<String> staff = new HashSet<String>(u2);
    staff.retainAll(u1);
    return staff;
  }
  
  public static void main(String[] args) {
    LinkedList<String> staff1 = new LinkedList<String>();
    staff1.add("a");
    staff1.add("b");
    staff1.add("c");
    LinkedList<String> staff2 = new LinkedList<String>();
    staff2.add("a");
    staff2.add("b");
    staff2.add("d");   
    System.out.println(unionSet(staff1,staff2).toString());
    System.out.println(intersectionSet(staff1,staff2).toString());
  }
}

Sincerely,
Adrian German