CMPSCI 187: Programming With Data Structures

Solutions to Third Midterm Exam

David Mix Barrington

Posted 17 November 2011

Directions:

  Q1: 10 points
  Q2: 15 points
  Q3: 15 points
  Q4: 15 points
  Q5: 15 points
  Q6: 10 points
  Q7: 20 points
 Total: 100 points

Here is a code base from previous assignments, that is assumed to be available throughout the exam:

public class Dog {
   private String name;
   private int age;
   // public get and set methods, two-parameter constructor
   }

public class SledDog extends Dog {
   private String breed = "Husky";
   // public get and set methods, three-parameter constructor
   }

public class LinearNode<T> {
   private T element;
   private LinearNode<T> next;
   // public get and set methods, zero-parameter and one-parameter constructors
   }

public class DogTeam {
   private LinearNode<SledDog> leadNode;
   private int size;
   // public get and set methods, zero-parameter constructor
   public void addToLead (SledDog newLead) {…}
   public SledDog removeLead ( ) {…}
   public void switchLastTwo ( ) {…}
   public SledDog removeYoungest ( ) {…}
   public int countHuskies ( ) {…}
   }

Code run before other code fragments:

   Dog ace = new Dog ("Ace", 6);
   Dog biscuit = new Dog ("Biscuit", 1);
   Dog cardie = new Dog ("Cardie", 3);
   Dog duncan = new Dog ("Duncan", 1);
   SledDog balto = new SledDog ("Balto", 92, "Husky");
   SledDog king = new SledDog ("King", 73, "Husky");
   SledDog buck = newSledDog ("Buck", 108, "Mixed");

Some useful API information for L&C's code base:

   public interface ListADT<T> extends Iterable<T> {
      public T removeFirst( );
      public T removeLast( );
      public T remove (T element);
      public T first( );
      public T last( );
      public boolean contains (T target);
      public boolean isEmpty( );
      public int size( );
      public Iterator<T> iterator( );
      public String toString( );}
   public interface OrderedListADT<T> extends ListADT<T> {
      public void add (T element);}
   public interface UnorderedListADT<T> {
      public void addToFront (T element);
      public void addToRear (T element);
      public void addAfter (T element, T target);}

// Note: For this exam is is assumed that the class ArrayList implements UnorderedListADT

   public class SortingandSearching {
      public static <T extends Comparable<? super T >> boolean
           linearSearch (T [ ] data, int min, int max, T target) {...}
      public static <T extends Comparable<? super T >> boolean
           binarySearch (T [ ] data, int min, int max, T target) {...}
      public static <T extends Comparable<? super T >> void
           selectionSort (T [ ] data) {...}
      public static <T extends Comparable<? super T >> void
           quickSort (T [ ] data, int min, int max) {...}
      public static <T extends Comparable<? super T >> void
           mergeSort (T [ ] data, int min, int max) {...}}
   





    public DogTeam[ ] listSubteams ( ) {
       DogTeam [ ] output = new DogTeam (twoToThe(size)); // no cast, not generic
       if (isEmpty( )) {
          output[0] = new DogTeam( ); // we return array of one empty team
          return output;}
       DogTeam temp = clone( ); // clone method using given methods is below
       SledDog lead = temp.removeLead( );
       DogTeam[ ] half = temp.listSubTeams( ); // array of 2^{size - 1} teams
       for (int i = 0; i < half.length; i++) {
          output[i] = half[i];
          half[i].addToLead(lead);
          output[i + half.length] = half[i];}
       return output;}

    public DogTeam clone( ) { // uses iterator from Project #6
       DogTeam ret = new DogTeam( );
       DogTeam temp = new DogTeam( );
       for (dog : this) temp = addToLead (dog); // temp is this backwards
       for (dog : temp) ret.addToLead (dog); // ret is now clone of this
       return ret;}

Last modified 19 November 2011