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 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 prevents the possibility of inconsistent state. [7 marks]
b. Provide one possible motivation for choosing this hypothetical method lock over an object lock. Explain your answer. [5 marks]
Question 3 (Generics, 15 marks)
Consider the following interface definition
/**
* This is an interface which abstracts the concept of a
* function. The interface has methods for a number of
* possible function call styles.
*
* @param <T> The type over which these functions work.
**/
publicpublicpublic interfaceinterfaceinterface Functions <T> {
/**
* A function which does not return a value
*
* @param in The parameter to this function
**/
publicpublicpublic abstractabstractabstract voidvoidvoid doIt(T in);
/**
* A function which turns a T into an integer
*
* @param in The parameter to this function
**/
publicpublicpublic abstractabstractabstract intintint calculate(T in);
/**
* A function which turns a T into an String
*
* @param in The parameter to this function
**/
publicpublicpublic abstractabstractabstract String show(T in);
/**
* A function which creates a T based on an integer
*
* @param in The parameter to this function
**/
publicpublicpublic abstractabstractabstract T generate(intintint in);
/**
* This function , called "transform" returns a value of the same type
* as the parameter it was given.
Page 2 of 5
COMP229 Object Oriented Programming Practices, November 2013
*
* @param in The parameter to this function
* @return A value of the same type as the input parameter
**/
// FIXME: Write this method signature
}
a. This interface is part of an implementation of a design pattern. Which design pattern is it from? Give the standard class diagram for the pattern and identify which part of the pattern is being implemented with the above interface. [6 marks]
b. How does this instance of the pattern differ from the standard version? [2 marks]
c. The final method signature has been left out. Write what you think that signature should be. [7 marks]
Question 4 (Refactoring, 16 marks)
Consider the following class definition. It is not necessary that you know anything about the system from which this class was taken, nor is it necessary to know anything about the classes Expr, Number or Boolean.
publicpublicpublic classclassclass Infer <T> {
Expr <T> expr;
publicpublicpublic Infer(Expr <T> e){
expr = e;
}
/**
* @param inferenceType The type of inference to perform.
* 0 is simple inference , 1 is polymorphic inference ,
* 2 is explicit inference.
**/
publicpublicpublic booleanbooleanboolean doInference(intintint inferenceType ){
ififif (inferenceType == 0)
ififif (expr.isVal ())
ififif (expr.isDefined ())
returnreturnreturn truetruetrue;
elseelseelse
returnreturnreturn falsefalsefalse;
elseelseelse ififif (expr.isPlus ()){
returnreturnreturn (newnewnew Infer <Number >(expr.left )). doInference(inferenceType)
&& (newnewnew Infer <Number >(expr.right )). doInference(inferenceType );
}
elseelseelse ififif (expr.isMinus ()){
returnreturnreturn (newnewnew Infer <Number >(expr.left )). doInference(inferenceType)
&& (newnewnew Infer <Number >(expr.right )). doInference(inferenceType );
}
elseelseelse ififif(expr.isAnd ()){
returnreturnreturn (newnewnew Infer <Boolean >(expr.left )). doInference(inferenceType)
&& (newnewnew Infer <Boolean >(expr.right )). doInference(inferenceType );
}
elseelseelse ififif(inferenceType == 1)
ififif (expr.isVal ())
Page 3 of 5
COMP229 Object Oriented Programming Practices, November 2013
ififif (expr.isDefined ())
returnreturnreturn falsefalsefalse;
elseelseelse
returnreturnreturn truetruetrue;
elseelseelse ififif (expr.isPlus ()){
returnreturnreturn (newnewnew Infer <Number >(expr.left )). doInference(inferenceType)
&& (newnewnew Infer <Number >(expr.right )). doInference(inferenceType );
}
elseelseelse ififif (expr.isMinus ()){
returnreturnreturn (newnewnew Infer <Number >(expr.left )). doInference(inferenceType)
&& (newnewnew Infer <Number >(expr.right )). doInference(inferenceType );
}
elseelseelse ififif(expr.isAnd ()){
returnreturnreturn (newnewnew Infer <Boolean >(expr.left )). doInference(inferenceType)
&& (newnewnew Infer <Boolean >(expr.right )). doInference(inferenceType );
}
elseelseelse
returnreturnreturn truetruetrue;
}
}
a. Identify as many code smells as you can in the above class. Hint: There are at least two. [6 marks]
b. Refactor the class to remove all the code smells you identified. Give the full definition of your refactored class and an explanation of the refactorings you performed.
[10 marks]
Question 5 (Design Patterns, 8 marks)
(a) There is something wrong with the following instance of the Template Method pattern.
Explain what is wrong. Modify the system so that it conforms to the pattern. Explain each of the changes you made to the system. Draw the class diagram for your modified system. [8 marks]
Page 4 of 5
COMP229 Object Oriented Programming Practices, November 2013
Question 6 (Testing, 12 marks)
(a) The following diagram shows the classes used in a calculator application. This cal- culator application can do addition, multiplication, powers, logarithms, factorials and permutations. Some of these calculations can take a long time on the low-powered de- vice on which it is designed to run. For this reason, the calculator takes each calculation it is asked to do and sends it over the internet to a supercomputer for computation as well as attempting to calculate it locally. If the supercomputer returns the result faster than the program can calculate it, it uses that result. Furthermore, the computer on which this application is running has a large amount of memory, so the application will store all the calculations it has already performed in a database. If it is asked to do a calculation it has done before, it uses the value it has stored in the database. This database can store 100,000 results before it is full and when it is full the calculator will have to do any new calculations every time they are requested.
At least the database class will need to be stubbed with mock objects for testing be- cause you need to test for empty, partially full, and completely full databases. Which other classes do you think will need to be stubbed with mock objects for testing the application? For each class you identify explain why you think that class needs mocking and at least three different mocked behaviours you will need. [12 marks]
END OF EXAMINATION
Page 5 of 5