Fall 2020 [Bono]

CS 455 Midterm 1 Solution

Q1 (Student statement agreement)


Q2.1.     Bookshelf methods faster with LL than AL:

addFront(height)
removeFront
Q2.2.     Bookshelf methods slower with LL than AL:
getHeight(position)


Q3.1
unchecked exception

Q3.1 Why?
Because satisfying a precondition on a method is something the programmer has control over; if the client doesn't satisfy the precondition we want the program to crash so they fix the bug, rather than writing a handler for that eventuality.


Q4.1

Opening file...
File not found
10
Exiting program.

Q4.2

Opening file...
Problem with file
10
Exiting program.

Q4.3 (Note: InputMismatchException is a subclass of NoSuchElementException [on code handout])

Opening file...
Problem with file
10
Exiting program.

Q4.4

Opening file...
13
Exiting program.


Q5 A common small mistake was to use == on Strings and/or on Integers: you need to use equals method instead. (We didn't deduct for this.)

public static Map<String, Integer> inBoth(Map<String, Integer> map1,
                                          Map<String, Integer> map2){

   Map<String, Integer> result = new HashMap<>();

   for (Map.Entry<String, Integer> entry1: map1.entrySet()) {

      String key1 = entry1.getKey();
      Integer value1 = entry1.getValue();
      
      Integer value2 = map2.get(key1);
      
      if (value2 != null && value1.equals(value2)) { 
                                   // note: null test above is optional because of 
                                   // the way equals is normally implemented
         result.put(key1,  value1);
      }
   }
         
   return result;
}
  


}


Q6

public static void atRisk(Map<String, Integer> map) {
   map.forEach(new PrintIfLT60());
}

class PrintIfLT60 implements BiConsumer<String, Integer> {
   public void accept(String key, Integer value) {
      if (value < 60) {
         System.out.println(key + " " + value);
      }
   }
}


Q7 Two main approaches, I'll show them both here. Note: both are tail-recursive

Approach 1: Test all three in one call

public static boolean hasTriple7(int[] nums) {

   return hasTriple7R(nums, 0);
}

// returns true iff the part of the array with index range
// [start, nums.length) contains three adjacent 7s anywhere
// PRE: start >= 0
private static boolean hasTriple7R(int[] nums, int start) {
   if (start + 3 > nums.length) {
      return false;
   }
   
   if (nums[start] == 7 && nums[start+1] == 7 && nums[start + 2] == 7) {
      return true;
   }
      
   return hasTriple7R(nums, start + 1);

}


Approach 2: Keep a count of adjacent 7's and pass to recursive call

public static boolean hasTriple7(int[] nums) {

   return hasTriple7R(nums, 0, 0);
}

// if adj7s is 0 returns true iff the part of the array with index range
// [start, nums.length) contains three adjacent 7s anywhere.
// if adj7s is > 0 it will return true for the above condition, or if the
// first k elements in above range are 7s, for the k such that (k + adj7s = 3)
// PRE: start >= 0 and adj7s is in range [0, 3]
public static boolean hasTriple7R(int[] nums, int start, int adj7s) {
    
   if (adj7s == 3) { return true; }
            
   if (start == nums.length) { return false; }

      
   if (nums[start] == 7) {
      adj7s++;
   }
   else {
      adj7s = 0;
   }
    
   return hasTriple7R(nums, start + 1, adj7s);
}