Ans Doc369

CLICK HERE TO DOWNLOAD THIS ANSWER INSTANTLY

Question 1 (Design Patterns)
The template method pattern and the strategy pattern both abstract some computation in the form of methods. What defining characteristic distinguishes the template method pattern from the strategy pattern? Explain your answer.
Question 2 (Concurrency)
Consider the following class definition. This class is considered to be in an inconsistent state if the is Consistent () method returns false;
Public class Foo {
long mvalue ;
long mValueTimesTwo ;
/ * *
* S e t s t h e s t a t e o f o u r o b j e c t .
*
* P a u s e s b r i e f l y b e t w e e n s e t t i n g t h e f i r s t a n d s e c o n d
* v a l u e s i n o r d e r t o i n c r e a s e t h e p r o b a b i l i t y t h a t t h e
* o b j e c t w i l l b e i n t e r r o g a t e d w h i l e i n a n i n c o n s i s t e n t
* s t a t e .
*
* @ p a r a Error! Hyperlink reference not valid.
* /
public synchronized void setValues ( long pValue ) {
mValue = pValue ;
doPause (3);
mValueTimesTwo = pValue * 2;
}
/ * *
* C h e c k s t o s e e i f t h e c u r r e n t s t a t e o f o u r o b j e c t i s
* c o n s i s t e n t .
*
* @ r e t u r Error! Hyperlink reference not valid.
* /
public synchronized boolean isConsistent () {
return ( mValue * 2 == mValueTimesTwo );
}
/ * *
* U t i l i t y r o u t i n e – p a u s e s o u r t h r e a d b y c a l l i n g
* s l e e p a n d s u p r e s s i n g a n y I n t e r r u p t e d E x c e p t i o n .
* /
private static void doPause ( long pPause ) {
try {
Thread . sleep ( pPause );
} catch ( InterruptedException e) {
e. printStackTrace ();
}
}
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
Question 3 (Generics, 15 marks)
Consider the following interface definition
/ * *
* T h i s i s a n i n t e r f a c e w h i c h a b s t r a c t s t h e c o n c e p t o f a
* f u n c t i o n . T h e i n t e r f a c e h a s m e t h o d s f o r a n u m b e r o f
* p o s s i b l e f u n c t i o n c a l l s t y l e s .
*
* @ p a r a Error! Hyperlink reference not valid.
* * /
public interface Functions {
/ * *
* A f u n c t i o n w h i c h d o e s n o t r e t u r n a v a l u e
*
* @ p a r a Error! Hyperlink reference not valid.
* * /
public abstract void doIt (T in );
/ * *
* A f u n c t i o n w h i c h t u r n s a T i n t o a n i n t e g e r
*
* @ p a r a Error! Hyperlink reference not valid.
* * /
public abstract int calculate (T in );
/ * *
* A f u n c t i o n w h i c h t u r n s a T i n t o a n S t r i n g
*
* @ p a r a Error! Hyperlink reference not valid.
* * /
public abstract String show (T in );
/ * *
* A f u n c t i o n w h i c h c r e a t e s a T b a s e d o n a n i n t e g e r
*
* @ p a r a Error! Hyperlink reference not valid.
* * /
public abstract T generate ( int in );
/ * *
* T h i s f u n c t i o n , c a l l e d ” t r a n s f o r m ” r e t u r n s a v a l u e o f t h e s a m e t y p e
* a s t h e p a r a m e t e r i t w a s g i v e n
* @ p a r a Error! Hyperlink reference not valid.
* @ r e t u r Error! Hyperlink reference not valid.
* * /
/ / F I X M E : W r i t e t h i s m e t h o d s i g n a t u r e
}
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)
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.
public class Infer {
Expr expr ;
public Infer (Expr e){
expr = e;
}
/ * *
* @ p a r a Error! Hyperlink reference not valid.
* 0 i s s i m p l e i n f e r e n c e , 1 i s p o l y m o r p h i c i n f e r e n c e ,
* 2 i s e x p l i c i t i n f e r e n c e .
* * /
public boolean doInference ( int inferenceType ){
if ( inferenceType == 0)
if ( expr . isVal ())
if ( expr . isDefined ())
return true ;
else
return false ;
else if ( expr . isPlus ()){
return ( new Infer < Number >( expr . left )). doInference ( inferenceType )
&& ( new Infer < Number >( expr . right )). doInference ( inferenceType );
}
else if ( expr . isMinus ()){
return ( new Infer < Number >( expr . left )). doInference ( inferenceType )
&& ( new Infer < Number >( expr . right )). doInference ( inferenceType );
}
else if( expr . isAnd ()){
return ( new Infer < Boolean >( expr . left )). doInference ( inferenceType )
&& ( new Infer < Boolean >( expr . right )). doInference ( inferenceType );
}
else if( inferenceType == 1)
if ( expr . isVal ())
if ( expr. isDefined())
return false ;
else
return true ;
else if ( expr . isPlus ()){
return ( new Infer < Number >( expr . left )). doInference ( inferenceType )
&& ( new Infer < Number >( expr . right )). doInference ( inferenceType );
}
else if ( expr . isMinus ()){
return ( new Infer < Number >( expr . left )). doInference ( inferenceType )
&& ( new Infer < Number >( expr . right )). doInference ( inferenceType );
}
else if( expr . isAnd ()){
return ( new Infer < Boolean >( expr . left )). doInference ( inferenceType )
&& ( new Infer < Boolean >( expr . right )). doInference ( inferenceType );
}
else
return true ;
}
}
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.
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
Question 6 (Testing)
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.