CS367 Homework 1
Lecture 4, Fall 2017
Due by 11:59 pm on Friday, September 22 (not accepted late)

Questions

Homework assignments must be done individually. Collaboration on homework assignments is not allowed.

Point allocation: 25%/25%/50%

Question 1:

Consider the following code fragment that is intended to remove every other item from a List<String> object. For example, given the list [ "Ben", "Beck", "Eric", "Vidya", "Jason", "Xiaoming" ], it should remove "Ben", "Eric", and "Jason" and leave the list as just [ "Beck", "Vidya", "Xiaoming" ].

for (int i = 1; i <= words.size(); i += 2) {
    words.remove(i); 
} 
  1. If words initially contains

    [ "washington", "j. adams", "jefferson", "madison", "monroe", "j.q. adams", "jackson", "van buren" ]

    then what is left in words after this code is done running?

  2. Does this code work correctly? If not, briefly describe (in a sentence or two) how it differs from what we said it should do.

  3. If the code is broken, describe how to fix it.

Question 2:

Assume that the ArrayList constructor initially reserves enough room to store ten elements without resizing. When resizing is needed, assume that the larger array is three times as big as the old one.

  1. Suppose we have called ArrayList.add 200 times on a single list, and have never removed any of the added elements. How many times has the array been resized? How many more elements can we add (with no removals) without causing the array to resize again?

  2. More generally, if an ArrayList has an initial capacity of i elements, and the array is tripled in size d times, then what will the total capacity be after these triplings?

  3. Conversely, if an ArrayList has an initial capacity of i elements, then how many triplings are needed to reach a total capacity of at least c elements?

Question 3:

Consider the following pseudo-code:

void main( ) {
    println("main enter");
    a();
    try {
        c();
        if (v4 == true) throw new Exc4();
    } catch (Exc4 ex) {
        println("main caught Exc4");
    } catch (Exc2 ex) {
        println("main caught Exc2");
    } catch (Exc1 ex) {
        println("main caught Exc1");
    }
    println("main exit");
}

void a( ) {
    println("a enter");
    try {
        b();
    } catch (Exc1 ex) {
        println("a caught Exc1");
    }
    println("a exit");
}
   
void b( ) {
    println("b enter");
    if (v2 == true) throw new Exc2();
    println("b exit");
}
   
void c( ) {
    println("c enter");
    try {
        d();
        if (v4 == true) throw new Exc3();
    } catch (Exc3 ex) {
        println("c caught Exc3");
        if (v3 == true) throw new Exc4();
    } catch (Exc4 ex) {
        println("c caught Exc4");
    }
    println("c exit");
}

void d( ) {
    println("d enter");
    if (v1 == true) throw new Exc1();
    if (v3 == true) throw new Exc3();
    if (v5 == true) throw new Exc5();
    println("d exit");
}

For the each part below determine the complete output that would be generated if the pseudo-code above was run with the values of the v variables as specified below. Assume the exception classes Exc1, Exc2, Exc3, Exc4, and Exc5 each extend RuntimeException. If an exception is passed out of main, show the output of the runtime environment as "Program terminated due to Exception ExcN    ", where N is the particular exception number.

  1. What would be output if v1 is true and the other variables are false?
  2. What would be output if v2 is true and the other variables are false?
  3. What would be output if v3 is true and the other variables are false?
  4. What would be output if v4 is true and the other variables are false?
  5. What would be output if v1 and v4 are true and the other variables are false?
  6. How would the code need to be modified if exception type Exc2 were a checked exception?
  7. How would the code need to be modified if exception type Exc5 were a checked exception?

Handing in

Please include your name at the top your file.

Put your answers to all questions into one file named Homework1 with the appropriate file extension, e.g., Homework1.pdf (see File Format for acceptable file formats).

Electronically submit your work to the Homework 1 Dropbox on Learn@UW.

Last Updated: 8/16/2017     © 2016-17 Beck Hasti and Charles Fischer