CS367 Homework 2
Lecture 3, Fall 2015
Due by 11:59 pm on Friday, October 2, 2015 (not accepted late)

Announcements

Check here periodically.

9/22/2015  Homework assigned. To ask questions about the homework and see questions posed by other students and their answers, go to: https:piazza.com/wisc/fall2015/cs3673 and sign-in using your wisc.edu account.

Questions

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

Question 1:

Consider the following pseudo-code:

void main( ) {
    println("main enter");
    a();
    try {
        b();
        c();
    } catch (Exc1 exc) {
        println("main caught Exc1");
    } catch (Exc3 exc) {
        println("main caught Exc3");
    }
    println("main exit");
}

void a( ) {
    println("a enter");
    if (var0)
        throw new Exc3();
    println("a exit");
}
   
void b( ) {
    println("b enter");
    try {
        if (var1)
            c();
        else
            throw new Exc1();
    } catch (Exc2 exc) {
        println("b caught Exc2");
    } finally {
        println("b finally");
    }
    println("b exit");
}
   
void c( ) {
    println("c enter");
    try {
        if (var2)
            throw new Exc2();
        if (var3) 
            throw new Exc3();
    } catch (Exc3 exc) {
        println("c caught Exc3");
    }
    println("c 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 var variables as specified below. Assume the exception classes Exc1, Exc2, and Exc3 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 var0, var1, var2, and var3 are all false?
  2. What would be output if var0 is true and var1, var2, and var3 are false?
  3. What would be output if var0, var1, and var2 are true and var3 is false?
  4. What would be output if var1 and var2 are true and var0 and var3 are false?
  5. What would be output if var1 and var3 are true and var0 and var2 are false?
  6. How would the code need to be modified if exception type Exc2 were a checked exception?

Question 2:

Complete the following Java method, forceAdd, as part of the SimpleArrayList<E> class. Do not check pos yourself! Instead, you must call add and catch and handle any IndexOutOfBoundsExceptions that result.

public void forceAdd(int pos, E item) {
    // Add the item to the list at position pos just as the ListADT.add  
    // method would do.  However, if the call to add throws an
    // IndexOutOfBoundsException, add the item to the end of the list.
     
}

Handing in

Please include your name at the top your file.

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

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

Last Updated: 8/21/2015     © 2015 Beck Hasti and Charles Fischer