Comp201: Principles of Object-Oriented Programming I
Spring 2008 -- Exam 2    


Instructions

  1. The examination is open book/notes.  You are allowed to consult any written materials available under the sun that was published or posted before the start of the exam, except code from your current Comp201 classmates.  If your code is based on materials that we did not provide you, just give us the appropriate references.
  2. You are not allowed to discuss the exam in any way with anybody beside the instructors
  3. of this course (i..e. Nguyen and Wong, but not the labbies) .
  4. Do not post your questions on the newsgroup.  Use the WebCT exam chat rooms during the designated times or e-mail both of us, dxnguyen@rice.edu and swong@rice.edu, your questions instead. We will reply to you directly, and if deemed necessary, we will forward our reply to the newsgroup as well.  We will check our e-mail as often as we can to catch and respond to your questions
  5. Write your code in the most object-oriented way possible, that is with the fewest number of control statements and no checking of the states and class types of the objects involved in any way.
  6. You have 3 hours to do the regular questions on the exam.
  7. Submission instruction: you must submit your exam in two ways.
    •  a hard copy with all answers in typewritten form, except perhaps diagrams (if any) which can be hand-drawn.  Be sure to print your name and sign the honor pledge.  You do not need to print out the supplied code unless you have modified it.
    • Zip your work together with your honor pledge (included in the provided code) and upload to the usual upload page under "Exam2". 
  8. The deadline for submission is  Wed. April 6, 2005 @ 10:00 AM.

Refresh this page often!

Check the time stamp at the bottom of the page to be sure that you have the latest version with any typographical corrections!

Pledge of Honor

 

1.a 5 pts 1.b 5 pts 1.c 15 pts 2.a 15 pts 2.b 15 pts 3.a 15 pts 3.b 15 pts 3.c 15 pts
+ 10 pts extra
Total: 100 pts
+ 10 pts extra
                 

PRINT THIS PAGE AND ALL YOUR CODE, WRITE AND SIGN YOUR HONOR PLEDGE AND BRING IT TO CLASS ON THE DUE DATE.


Problem 1: An Anonymous Baby (25 pts total)

Consider the following behavior of an instance of the class Baby (each step is performed in sequence):

  1. Baby calvin = new Baby(125)  // weight = 125 ounces at instantiation
  2. calvin.getWeight()  returns 125.
  3. calvin.tryFeed()  returns "Slurp, slurp, slurp..."
  4. calvin.getWeight() returns 127.
  5. calvin.getWeight() returns 127.
  6. calvin.tryFeed() returns "Waaaaa! Waaaaa!"
  7. calvin.getWeight() returns 126.   (Don't ask why--you don't want to know!)
  8. calvin.getWeight() returns 126.
  9. calvin.tryFeed() returns "Slurp, slurp, slurp..."
  10. calvin.tryFeed() returns "Waaaaa! Waaaaa!"
  11. calvin.tryFeed() returns "Slurp, slurp, slurp..."
  12. calvin.getWeight() returns 129

 

Problem 1.a (5 pts):   In comments at the top of the supplied stub code for Baby.java, explain the above behavior in terms of states.   

 

Problem 1.b (5 pts):   In comments at the top of the supplied stub code for Baby.java, explain what methods and attributes are invariant with respect to the Baby's state and which methods and attributes are variant with respect to the Baby's state.

Problem 1.c (15 pts):   Using anonymous inner classes, complete the implementation of Baby.java such that the above behavior is replicated.   The test code is supplied.   Your code should conform to the following restrictions:

 

Problem 2:  Running Sums on Lists (30 pts total)

In this problem we will explore algorithms on both immutable and mutable lists that calculate the running sum as the list is traversed.

The "running sum" sum of a list is a list of the sums of all the elements up and including any given element of a list, starting at the front of the list.

Examples:

The running sum algorithm will be implemented as a visitor that takes no input parameters.

In the supplied stub code, the following sections are in separate projects.

Problem 2.a (15 pts):   Running sum of an immutable list (IList)

Problem 2.b (15 pts):   Running sum of an mutable list (LRStruct)

 

Problem 3:  Removing Duplicates from an LRStruct (45 pts total + 10 pts extra credit)

Given a list with duplicate values in it, there are (at least) two ways in which one can define the removal of the duplicate values.  For instance:

(a, 2, a, 3, c, d, 3, a, e)  becomes either 

(a, 2, 3, c, d, e)   or 

(2, c, d, 3, a, e) 

What should the algorithm do if the host list is empty?

The difference is whether you keep the first occurrence of a value by looking from left to right (the first result) or from right to left (the second result).   This is simply the difference between a forward and a reverse accumulation removal algorithm (Which is which?).

We wish to express the duplicate removal algorithm that mutates the host list such that all duplicate values are removed.    The list should contain Objects, not just Strings or Integers, so the equals method of Object should be used to compare if two values are equal, e.g. a.equals(b) returns true if a and b have the same value or returns false if they are different.

In the supplied stub code, the following sections are all in a single project.

Problem 3.a (15 pts): Duplicate detection algorithm 

Whether we are doing a forward or reverse accumulation duplicate removal algorithm, we always need a way to detect if a duplicate value exists in a list, so we'll write a utility algorithm first and separately.   We will incorporate it into our whole algorithm in the later sections of the problem.

 

Just in case...

In the following sections, if you could not get your Contain visitor to work properly, download the class file, Contain_Soln.class and place it in your prob3\classes\lrs\visitor folder.   (Careful, "Clean Build Directory" in DrJava will delete this file too!)   In your code, you can refer to Contain_Soln wherever Contain would be used.     It is an Honor Code violation to attempt to decompile this or any supplied class files.

Problem 3.b (15 pts):  Reverse accumulation duplicate removal.

 

Problem 3.c (15 pts + 10 pts extra):  Forward accumulation duplicate removal.

 

 

 


Last Revised Thursday, 03-Jun-2010 09:50:25 CDT

©2008 Stephen Wong and Dung Nguyen