cs205: engineering software?
(none)
20 September 2010
Final Exam Out: 4 December 2006
Due: Friday, 8 December, before 4:55pm


Name: _________________________________________________________

Directions

Work alone. You may not discuss these problems or anything related to the material covered by this exam with anyone except for the course staff between receiving this exam and 5pm Friday.

Open non-human resouces. You may use any books you want, lecture notes and slides, your notes, websites, and problem sets. If you use anything other than the course books and notes, cite what you used. You may not use other people.

Closed tools. You may not run a Java compiler for this exam. For questions that expect you to write code, you will not lose points for minor syntactic mistakes.

Think. Unlike the quizzes, all of the questions on this exam are intended to make you think, not just repeat something you already know. You shouldn't expect to find the exact answers to the questions here in the course materials (or any other resources you find), but should be able to deduce good answers based on your understanding of the course material and ability to reason using that understanding.

Answer well. Write your answers on this exam. You should not need more space than is provided to write good answers, but if you want more space you may attach extra sheets. If you do, make sure the answers are clearly marked. If you prefer to type your answers instead of writing on this exam, that is fine as long as your answers are clearly marked.

This exam has 8 graded questions, in addition to the teammate assessment questions and optional ungraded questions. The questions are not necessarily in order of increasing difficulty, so if you get stuck on one question you should continue on to the next question. There is no time limit on this exam.

Full credit depends on the clarity and elegance of your answer, not just correctness. Your answers should be as short and simple as possible, but not simpler.

Teammate Assessment

These questions ask you to assess the contributions of yourself and your teammates on the project assignment. Please answer them honestly, and feel free to write any clarifying comments you wish. Your answers will not effect your exam grade, but may effect how grades are assigned for the project assignment.

T1. Who was the leader of your team?










T2. For each teammate (including yourself), list the percentage of effort they contributed to your team, and the percentage of the credit they deserve for successful parts of your project.

                        Effort           Credit Share         


         Yourself:    ____________       ____________         


_________________:    ____________       ____________         


_________________:    ____________       ____________         


_________________:    ____________       ____________         

Design

You are designing software for a GPS navigation device. The software must maintain a map that keeps track of roads, highways, cities, hotels, and restaurants. Consider these two designs:

Design A


Design B
In both designs, the MapLocation datatype represents a location on a map (for example, using its longitude and latitude), the Road, Highway, and Exit datatypes represent roads, highways, and exits on the map, and the Restaurant and Hotel datatypes represent places to eat and sleep. (Arrows pointing up indicate subtyping relationships; arrows pointing down indicate dependencies without any subtyping.)

1. (10) Which design is better? State clearly the reasons the design you choose is better than the other design.

Consider the specifications of the Road and Highway datatypes shown below. (You may want to remove this page from your exam so you can look at it separately.)
public class Road {
   OVERVIEW: A Road represents a road on the map.  A typical road is a 
    sequence of waypoints and a set of intersections: 
      < waypoints, intersections > where 
         waypoints = < loc_1, ..., loc_n >
         intersections = { < loc_a, road_a >, ... < loc_k, road_k > } 
    The waypoints give the path of the road, and the intersections set 
    contains the roads it intersects with.  Each loc_i is a MapLocation.  
    The road must have at least two waypoints.  Each intersection is a 
    < MapLocation, Road > pair. 

   public Road (Vector<MapLocation> waypoints)
      REQUIRES: waypoints must contain at least 2 elements.
      EFFECTS: Initializes this to a road conecting the waypoints in order.

   public void addIntersection (MapLocation loc, Road road) 
        thows BadLocationException, DuplicateIntersectionException
      MODIFIES: this
      EFFECTS:  If loc is not a location on the road (that is, it is not
         on the line between two adjacent locations in this.waypoints),
         throws BadLocationException.  If there is already an
         intersection at loc, throws DuplicateIntersectionException.
         Otherwise, adds < loc, road> to this.intersections.

   public Road getIntersection (MapLocation loc) throws NoIntersectionException
      EFFECTS: If there is no intersection at location loc, throws
         NoIntersectionException.  Otherwise, returns the road that
         interesects with this at loc (the element of this.intersections
         such that el.loc = loc).
}

public class Highway extends Road {
   OVERIVEW: A Highway represents a highway on the map.  A highway is
       a road with exits.  A typical highway is a sequence of waypoints
       and a sequence of exits: < waypoints, exits > where
         waypoints = < < loc_1, ..., loc_n >
         exits = { < loc_a, exit_a >, ... < loc_k, exit_k > } 
       Each loc_i is a MapLocation.  The road must have at least
       two waypoints.  Each exit is a < MapLocation, Exit > pair. 

   public Road (Vector<MapLocation> waypoints)
      REQUIRES: waypoints must contain at least 2 elements.
      EFFECTS: Initializes this to a road conecting the waypoints in order.

   public void addIntersection (MapLocation loc, Exit exit) 
        thows BadLocationException, DuplicateIntersectionException
      MODIFIES: this
      EFFECTS:  If loc is not a location on the highway (that is, it is not
         on the line between two adjacent locations in this.waypoints),
         throws BadLocationException.  If there is already an
         exit at loc, throws DuplicateIntersectionException.
         Otherwise, adds < loc, exit> to the end of this.exits.

   public Exit getIntersection (MapLocation loc) throws NoIntersectionException
      EFFECTS: If there is no intersection at location loc, throws
         NoIntersectionException.  Otherwise, returns the exit that
         interesects with this at loc (the element of this.intersections
         such that el.loc = loc).
}

As shown, it is obvious that Highway is not a behavioral subtype of Road since the return type of getIntersection for the Highway datatype (Exit), is not a subtype of the the return type for the Road datatype's getIntersection method (Road).

2. (10) Cathy Cartographer suggests making Exit a subtype of Road. If this is done is Highway now a behavioral subtype of Road? Explain why or why not. If not, explain what other changes would be needed to make Highway a proper behavioral subtype of Road.



















3. (15) What is a good data representation for the Road datatype specified above? Include both a representation invariant and abstraction function for your data representation.

4. (15) Cathy wants to add a findNearestExit method to the Highway datatype for finding the nearest exit to a given location. It should take a MapLocation parameter, and return the Exit that is closest to the given location on the Highway object. Write a declarative, total specification for the findNearestExit method.

Concurrency

The next two questions concern the Philosopher class from PS5 (and Quiz 4). It is repeated here:
public class Philosopher {
   private Philosopher colleague;
   private String name;
   private String quote;

   public Philosopher(String name, String quote) {
      this.name = name;
      this.quote = quote;
   }

   public synchronized void setColleague(Philosopher p) {
      colleague = p;
   }

   public synchronized void argue() { ... } // elided

   public void philosophize () {
      Object lock1, lock2;

      if (colleague != null) { // Need a colleague to start and argument.
         // Always grab the lock for whichever name is alphabetically first
         if (name.compareTo (colleague.name) < 0) {
            lock1 = this;
            lock2 = colleague;
         } else {
            lock1 = colleague;
            lock2 = this;
         }
	    
         synchronized (lock1) {
            synchronized (lock2) {
                 System.err.println (name + "[Thread " 
                 + Thread.currentThread().getName () + "] says " + quote);
                 colleague.argue ();
	    } 
         }
      }
   }
}

5. (10) Recall the first problem explained in the PS5 comments for question 5:
The race condition is that the colleague of the this object could change between the name.compareTo call in the this object's associated thread and the name.compareTo call in the colleague object thread. Since philsophize is not synchronized, the (synchronized) setColleague method could execute in another thread while philosophize is executing. Then, the wrong lock would be grabbed! Instead of locking our current colleague, we would grab the lock for our previous colleague and then call colleague.argue (which is synchronized, and will wait for the lock on our current colleague).
Suggest a way to fix this problem. A good answer will show clearly how you would change the code. Be careful to make sure that your fix does not allow any new deadlock opportunities.



















6. (10) According to the PS5 comments (question5), "Even without the race condition, there are ways in which our solution could still deadlock: we could have three philosophers who have non-reflexive colleagues." Explain in detail a scenario where such a deadlock could occur, or explain why no scenario involving three philosophers with non-reflexive colleagues could produce a deadlock.

Type Safety

7. (15) What must the Java bytecode verifier prove to know the following instruction sequence is type safe? (Hint: a good answer will explain the all preconditions that need to be true before the iload_1 instruction.)
    iload_1
    iadd
(See Class 30 for information on the instructions.)



















Abstraction

8. (15) Identify three different types of abstraction. For each, explain what language features Java provides to support that type of abstraction. Especially good answers will also discuss how well Java's constructs support the desired form of abstraction.

9. (Optional, no credit) Is there anything you think I should take into account in determining your final grade that won't be apparent from your performance on this exam, the problem sets and project, the quizzes, and exams?





















10. (Optional, no credit) Is there anything else you want me to know about CS205? Any suggestions for improving future offerings of the course are especially appreciated.

















End of Exam. Have a great winter break!