Comp201: Principles of Object-Oriented Programming I
Spring 2006 -- 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 WebCT upload page under "Exam 2"
  8. The deadline for submission is  Wed. April 5, 2006 @ 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.i 10 pts 2.a.ii 10 pts 2.a.iii 10 pts 2.b 15 pts 3.a 15 pts 3.b 15 pts Total: 100 pts
 
                   

Download the provided code: Comp201S06Ex2.zip, which contains all the exam materials in a directory called Comp201Ex2.   Each question has its own project, perhaps multiple projects for the different sub-sections.

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


Problem 1: Modeling a Car (25 pts total)

Below is the stub code for the class Car.   You are to complete the code as specified in the problem questions that follow.

/**
 * Modeling a car with state-dependent behaviors.
 */
public class Car {
    private int mileage = 0;   
    private static abstract class ACarState {
    // STUDENT TO DETERMINE WHAT THE STATE-DEPENDENT METHODS ARE AND
    // ADD THE CORRESPONDING ABSTRACT METHODS TO THIS ABSTRACT ACarState.
    // See problem 1.b













    } 
    // STUDENT TO FIGURE OUT WHAT THE CONCRETE STATES ARE AND
    // CREATE THE CONCRETE STATES AS ANONYMOUS INNER CLASSES.
    // See problem 1.c; code for concrete car states goes here...








































    // THIS IS THE STATE OF A CAR;
    // STUDENT TO SET aCarState TO AN APPROPRIATE INITIAL STATE
    private ACarState aCarState; 
    
    // STUDENT TO APPLY THE STATE PATTERN TO DELEGATE
    // THE STATE-DEPENDENT BEHAVIORS OF A CAR TO ITS STATE.
    // See problem 1.b
    public String startEngine() {
       // STUDENT TO COMPLETE


    }
    
    public String stopEngine() {
       // STUDENT TO COMPLETE


    }
    
    public String pushAccelerator() {
       // STUDENT TO COMPLETE


    }
    
    public String hitTree() {
       // STUDENT TO COMPLETE


    }
    
    public int getMileage() {
       // STUDENT TO COMPLETE


    }
}

The following is an example of a sequence of interactions with a Car object showing its response to each method call.

  1. Car car = new Car();
  2. car.getMileage() returns 0
  3. car.pushAccelerator() returns "[Nothing happens]"
  4. car.hitTree() returns "[Nothing happens]"
  5. car.stopEngine() returns "[No sound]"
  6. car.getMileage() returns 0
  7. car.startEngine() returns "Roar!"
  8. car.startEngine() returns "Grind!!"
  9. car.pushAccelerator() returns "Zoom, zoom!!"
  10. car.getMileage() returns 10
  11. car.pushAccelerator() returns "Zoom, zoom!!"
  12. car.getMileage() returns 20
  13. car.stopEngine() returns "Rrrrr....sssss....[no more noise]"
  14. car.stopEngine() returns "[No sound]"
  15. car.startEngine()returns "Roar!"
  16. car.hitTree() returns "Crash! Bang!"
  17. car.getMileage() returns 20
  18. car.pushAccelerator() returns "[Nothing happens]"
  19. car.getMileage() returns 20
  20. car.stopEngine() returns "[Nothing happens]"
  21. car.hitTree() returns "[Nothing happens]"
  22. car.startEngine() returns "Sputter, sputter"

(The above sequence is replicated in the supplied test code).

Problem 1.a (5 pts):   From the above sequence interactions with a Car object, it is clear that the Car object changes its behavior dynamically.  This dynamic change of behavior can be implemented using the state design pattern.  How many states does Car have?  What are they?  Write your answer at the top of the supplied Car.java code.









 

Problem 1.b (5 pts):   Of the five Car methods, startEngine, stopEngine, pushAccelerator, hitTree, getMileage, which ones are state dependent?  Is the field mileage state-dependent?  Add the corresponding state-dependent methods to the abstract class ACarState that represents the union of all the states of the Car.  Write your answer at the top of the supplied Car.java code. and complete the code for the aforementioned five Car methods.


 

Problem 1.c (15 pts):   Use anonymous inner classes to implement all the concrete states of Car such that the above behavior is replicated.   Your code should conform to the following restrictions:


 

Problem 2:  Palindrome Lists (45 pts total)

Definition of Palindrome: According to Webster.com, a palindrome is a word, verse, or sentence (as "Able was I ere I saw Elba") or a number (as 1881) that reads the same backward or forward.  In this problem, you are to determine whether or not an LRStruct is a palindrome in two different ways.

Problem 2.a (30 pts total): One way is to check the LRStruct against its reverse to see if they both contain the same elements in the same order.  To implement this algorithm do the following.

  1. (10 pts) Write a visitor called LRSReverse to return a new LRStruct that is the reverse of the host.  The host should remain unchanged.  Complete the stub code below.

public class LRSReverse implements IAlgo {

    // STUDENT TO COMPLETE SINGLETON PATTERN CODE IF APPLICABLE.

 

 

    public Object emptyCase(LRStruct host, Object... nu) {

        // STUDENT TO COMPLETE

 

    }

   

    public Object nonEmptyCase(LRStruct host, Object... nu) {

        // STUDENT TO COMPLETE

 
 

 

    }

}

 

  1. (10 pts) Write a visitor called LRSEqual to return Boolean. TRUE if the host and the input parameter contain equal elements in the same order and Boolean.FALSE otherwise.  Complete the stub code below.  Write additional helper visitors as anonymous inner classes if necessary.

public class LRSEqual implements IAlgo {

    // STUDENT TO COMPLETE SINGLETON PATTERN CODE IF APPLICABLE.

 

 

    public Object emptyCase(LRStruct host, Object... other) {

        // STUDENT TO COMPLETE

 

    }

   

    public Object nonEmptyCase(LRStruct host, Object... other) {

        // STUDENT TO COMPLETE

 

 

 

 

    }

}

 

  1. (10 pts) Write a visitor called LRSPalindrome to return Boolean.TRUE if the host is a palindrome and Boolean.FALSE otherwise.  This visitor must make use of the visitors in part i and part ii above.  Complete the stub code below.

public class LRSPalindrome implements IAlgo {

    // STUDENT TO COMPLETE SINGLETON PATTERN CODE IF APPLICABLE.

 

 

    public Object emptyCase(LRStruct host, Object... nu) {

        // STUDENT TO COMPLETE

 

    }

   

    public Object nonEmptyCase(LRStruct host, Object... nu) {

        // STUDENT TO COMPLETE

 

 

 

 

    }

}

Problem 2. b (15 pts):  Another way to determine whether or not a given LRStruct is a palindrome is first to observe the following.  Translate the above observation into a visitor for LRStruct called LRSPalin that returns Boolean.TRUE when the host LRStruct represents a palindrome, Boolean.FALSE otherwise.    You may use the visitor RemLast (discussed in lecture 29) that removes the last element in the host LRStruct without explanation.  In this visitor, it is acceptable for the host LRStruct to mutate.   Complete the stub code below.  Write additional helper visitors as anonymous inner classes if needed.

public class LRSPalin implements IAlgo {

    // STUDENT TO COMPLETE SINGLETON PATTERN CODE IF APPLICABLE.

 

 

    public Object emptyCase(LRStruct host, Object... nu) {

        // STUDENT TO COMPLETE

 

    }

   

    public Object nonEmptyCase(LRStruct host, Object... nu) {

        // STUDENT TO COMPLETE

 

 

 

 

    }

}

Problem 3:  Randomized Lists (30 pts total)

In this problem we will explore algorithms on a mutable list that will enable us to randomize the order of the elements in the list.

Problem 3.a (15 pts): RandomSwap   First, we will need an algorithm to swap the first element of a LRStruct list with a random element in the rest of the list. 

The problem here is that in order to randomly pick an element from the rest of the list, we must first know how may elements are in that list.   Otherwise, we cannot insure an equal probability for swapping with any given element.   After we know how many elements are in the rest of the list, we can pick a random number that is at most that large and then swap with that number element in the rest of the list.  

We could do this in two passes of the list, one to get the length and another to swap with the randomly chosen element.    NOT!

Instead we will accumulate the length using a forward accumulation (i.e. on the way in) and then swap the element on the way back out.   We do this by returning a count, which is a random number that is between zero and the length of the list.   The count gets decremented every time the algo returns, so if we swap only when the count exactly equals zero, it will swap at the desired point and swap only once.  Ta da! One pass through the list!

More succinctly, the entire algorithm looks like this:

 You are to do the following:

Problem 3.b (15 pts): Randomize   To randomize the whole LRStruct is now almost trivial. 

You are to do the following:

Note:  The testing routine utilizes the fact that computerized random number generators don't really produce random numbers.  If you initialize the random number generator with the same "seed" value every time, then it will give you exactly the same sequence of numbers out every time.   Since most security software that relies on random numbers, this fact causes massive problems from keno games to electronic voting systems.   We are using this fact to our advantage here to enable us to test the outcome of the randomization since the random number sequence is predictable.

 


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

©2006 Stephen Wong and Dung Nguyen