Exam 1 Lab

Do this lab with your buddy in the usual way.

Part A. Null arrays and null pointer exceptions

1. Look at question 1 in the exam (in the D2L content area). Many people got confused about the fact that an array can be null.

Consider this class:

public class Question1
{
   private static int[] scores;

   public static int sumLastValues(int[] a, int numberValues)
   {
      int sum = 0;
      try
      {
         for (int i = a.length - numberValues - 1; i < a.length; i++)
            sum += a[i];
         return sum;
      }
      catch (IndexOutOfBoundsException exception)
      {
         return sum;
      }
   }

   public static void main(String[] args)
   {
      System.out.println(sumLastValues(scores, 100));
   }
}

What exception do you expect to get? Why?

2. Now run the program. What exception did you get? Why?

3. What was the value of the parameter a?

4. Declare variables s1, s2, s3, s4 of type String[] and initialize them

Part B.  Tracing Recursion

If you or your buddy solved the substring generator from the exam prep, trace through the recursive calls and show how your program finds the substrings of "fred".

If you didn't do this, trace through the following call in the solution of homework 4:

Puzzle p = new Puzzle("123","4X6","789");
Puzzle q = solvePuzzle(p)

Do this by hand, with pencil and paper, not with the debugger.

In either case, make a card per recursive call. On the top of the card, write the method call

methodName(arg1 = value1, arg2 = value2, ...)

Each time a local variable changes, write down the new name.

When you get to a recursive call, write the call on the card, then start a new card, and write the call again on the top.

When you return from a recursive call, pick up the card to which the call returned, and write the result after the method call.

To see an example of the card format, check out the solution to the exam.

Part C. Reading Code

Look at my solution to homework 3B. Take this input:


Counting Instead of Printing
Suppose you want to change the FileFinder class so that it counts the matching files 
rather than rather than printing them. What changes would you need to make?
A) Add a local variable counter, increment it in the second if statement
of the find method and return it.
B) Add an instance variable counter to the FileFinder class and increment
it in the second if statement of the find method
*C) Add a local variable counter, update it in both if statements
of the find method and return it.
D) Add an instance variable counter and update it in both if statements
of the find method.

1. What local variables are in the main method?

2. What are the settings of these variables when the  while (options.size() == 0) loop has traversed once (i.e. after reading in the second line)?

3. What are the settings of these variables when the  while (options.size() == 0) loop has traversed twice (i.e. after reading in the third line)?

4. What are the settings of these variables when the  while (options.size() == 0) loop has traversed three times (i.e. after reading in the fourth line)?

5. What is the significance of the questionText variable? How does it capture a description that contains multiple lines? When is it finished?

6. The program is unable to handle options that have more than one line. What specifically will go wrong when it reads the example above?

7. This is the problem that you were asked to fix on the exam. The exam has the hint: Make a string, add \n + a line to it whenever you need to add a line, and save it when you know that it is complete. What variable is already in the program that works in the same way?

8. Ok, make another variable optionText. When do you initialize it?

9. When do you add another line to optionText?

10. When should you stop adding more lines to optionText?

11. When should you add optionText to theoptions array list?

12. Now provide a solution to exam question 6, using the optionText variable.