Spring 2020 [Bono]

CS 455 Midterm 1 Solution

Problem 1

Part A.

Part B.

[15, 22] [21, 25]
[15, 22] [15, 22]
Part C.    3


Problem 2 Many correct solutions.
Part 1. (wrong)

temp:    105
return value:  cool

Part 2. (right)

temp:    56
return value:  cool

Problem 3
Several correct solutions; including some that involve doing a cd to a different directory first.

    cp ../../baz/* .

Problem 4 Solution shown in bold.

//  Represents a "drunkard" doing a random walk along an integer number line. 
//  Drunkard chooses direction and step size randomly.  Steps range in
//  distance from 1 to MAX_STEP_SIZE, inclusive.
public class Drunkard1D {

   public static final int MAX_STEP_SIZE = 10;

   private int currLoc;
   private Random random;


   // Creates drunkard with given starting location.
   // @param startLoc starting location of drunkard
   public Drunkard1D(int startLoc) {
 
      random = new Random();
      currLoc = startLoc;

   }
   
   // Takes a random-length step of size in the range [1, MAX_STEP_SIZE] in
   // a random direction on the number line.
   public void takeStep() {
 
      boolean goRight = random.nextBoolean();
      int distance = random.nextInt(MAX_STEP_SIZE) + 1;
      if (!goRight) { distance = -distance; }
      currLoc += distance;
 


   // Gets the current location of the drunkard.  (accessor)
   // @return an int representing drunkard's current location
   public int getCurrentLoc() {

      return currLoc;

   }
}
Problem 5
I decided to change the point values for parts A and B to 6 and 7, respectively. Total points for problem did not change.

Part A.

   // returns location of target in namesArray or -1 if not found
   private int lookupLoc(String target) {

      for (int i = 0; i < namesArr.size(); i++) {

         int result = target.compareTo(namesArr.get(i));

         if (result < 0) { return -1; }

                     result == 0 
         if (target.equals(namesArr.get(i))) { return i; }
         
      }
      
      return -1;
   }
Part B.
You could get full credit by providing any seven of the following 10 test cases with expected results. You did not have to use the exact same test data in your tests, just the same cases tested. I added a descripton of each test case here.

Here's the sample namesArr given on the exam (called A in the tests):

    [Avinash, Carly, John, Mana, Peter, Sa, Yiqi]

target           namesArr       expected     description of test case

Avinash       A           0      first
Yiqi          A           6      last
Mana          A           3      somewhere in middle
Li            A           -1     not present, target would have been somewhere in middle
Andy          A           -1     not present, target comes before first element
Zhao          A           -1     not present, target comes after last element
Li           []           -1     empty namesArr
Carly        ["Carly"]    0      present, 1-elmt namesArr
Li           ["Carly"]    -1     not present, 1-elmt namesArr, target comes before element
Abby         ["Carly"]    -1     not present, 1-elmt namesArr, target comes after element

Problem 6

Part A.  M
Part B.  C
Part C.  M
Part D.  M, C

Problem 7

The most straightforward solution is one using nested loops, below. Another less common solution was one using only one loop though the whole array, where periodically you hanve to reset upTo and count to make sure you are putting the right values in the array.

public static int[] createSequence(int n) {

   int[] result = new int[n*(n+1)/2];
      
   int loc = 0;
      
   for (int upTo = 1; upTo <= n; upTo++) {  // upTo is the number we are counting up to
                                            // in this iteration of outer loop
      for (int count = 1; count <= upTo; count++) {
         result[loc] = count;
         loc++;
      }
   }

   return result;
}