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 defining characteristic distinguishes the template method patern from the strategy pattern? Explain your answer. [5 marks] Question 2 (Concurrency, 12 marks) Consider the following class definition. This class is considered to be in an inconsistent state if the isConsistent() method returns falsefalsefalse; publicpublicpublic classclassclass Foo { longlonglong mValue; longlonglong 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 , */ publicpublicpublic synchronizedsynchronizedsynchronized voidvoidvoid setValues(longlonglong 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. */ publicpublicpublic synchronizedsynchronizedsynchronized booleanbooleanboolean isConsistent () { returnreturnreturn (mValue * 2 == mValueTimesTwo ); } /** * Utility routine - pauses our thread by calling * sleep and supressing any InterruptedException. */ privateprivateprivate staticstaticstatic voidvoidvoid doPause(longlonglong pPause) { trytrytry { Thread.sleep(pPause ); } catchcatchcatch (InterruptedException e) { e.printStackTrace (); } } } Page 1 of 5 COMP229 Object Oriented Programming Practices, November 2013 a. Imagine a hypothetical version of Java where the object lock is replaced by a method lock. Under this system a call to a synchronised method would assign a lock for that method to the calling thread. No other thread could then call this method because the lock is already allocated. However, other methods of the same object could still be called. Upon the method completing, the lock is released. Under this system, is it possible to put an instance of the Foo class into an inconsistant state? If so, give a code example which could create this situation and explain how it does so. If not, explain how the method lock preven ...
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 defining characteristic distinguishes the template method patern from the strategy pattern? Explain your answer. [5 marks] Question 2 (Concurrency, 12 marks) Consider the following class definition. This class is considered to be in an inconsistent state if the isConsistent() method returns falsefalsefalse; publicpublicpublic classclassclass Foo { longlonglong mValue; longlonglong 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 , */ publicpublicpublic synchronizedsynchronizedsynchronized voidvoidvoid setValues(longlonglong 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. */ publicpublicpublic synchronizedsynchronizedsynchronized booleanbooleanboolean isConsistent () { returnreturnreturn (mValue * 2 == mValueTimesTwo ); } /** * Utility routine - pauses our thread by calling * sleep and supressing any InterruptedException. */ privateprivateprivate staticstaticstatic voidvoidvoid doPause(longlonglong pPause) { trytrytry { Thread.sleep(pPause ); } catchcatchcatch (InterruptedException e) { e.printStackTrace (); } } } Page 1 of 5 COMP229 Object Oriented Programming Practices, November 2013 a. Imagine a hypothetical version of Java where the object lock is replaced by a method lock. Under this system a call to a synchronised method would assign a lock for that method to the calling thread. No other thread could then call this method because the lock is already allocated. However, other methods of the same object could still be called. Upon the method completing, the lock is released. Under this system, is it possible to put an instance of the Foo class into an inconsistant state? If so, give a code example which could create this situation and explain how it does so. If not, explain how the method lock preven ...