Computer Science Assessment – Java Object Oriented Programming
Faculty of Science Department of Computing Final Examination 2013 Unit: COMP229 Object Oriented Programming Practices Release Date: 9:00am, November 15, 2013 Due Date: 11:45pm, November 19, 2013 Total Number of Questions: Six (6) Total Marks: Sixty Four (68) Instructions: Answer ALL questions. All references to program code or behaviour refer to the Java language. All answers to questions that ask for code must be written using Java. Every attempt has been made to make questions unambiguous. However, if you are not sure what a question is asking, make some reasonable assumption and state it at the beginning of your answer. COMP229 Object Oriented Programming Practices, November 2013 Question 1 (Design Patterns, 5 marks) The template method pattern and the strategy pattern both abstract some computation in the form of methods. What dening characteristic distinguishes the template method patern from the strategy pattern? Explain your answer. [5 marks] Question 2 (Concurrency, 12 marks) Consider the following class denition. This class is considered to be in an inconsistent state if the isConsistent() method returns false; public class Foo long mValue ; long mValueTimesTwo ; /** * Sets the state of our object . ** Pauses briefly between setting the first and second * values in order to increase the probability that the * object will be interrogated while in an inconsistent * state . ** @param pValue the value to update the current state with , */ public synchronized void setValues (long pValue ) mValue = pValue ; doPause (3); mValueTimesTwo = pValue * 2; /** * Checks to see if the current state of our object is * consistent . ** @return true if it is. */ public synchronized boolean isConsistent () return ( mValue * 2 == mValueTimesTwo ); /** * Utility routine – pauses our thread by calling * sleep and supressing any InterruptedException . */ private static void doPause (long pPause ) try Thread . sleep ( pPause ); catch ( InterruptedException e) e….