Suppose we have a need to swap the values of two variables that are of the same type a large number of times. A brute force solution would be:
save_var = var_a;
var_a = var_b;
var_b = save_var;
Since we need to perform the swap of variable frequently, we may consider the following:
save_var = var_a.getValue();
var_a.setValue(var_b.getValue());
var_b.setValue(save_var);
This is useful for all classes we write, but is not a solution for built-in classes, like Integer. We can use a wrapper pattern to wrap these classes in our own classes:
public class IntegerVariable
{
Integer value;
public IntegerVariable(Integer _value)
{
this.value = _value;
} // IntegerVariable(Integer)
public void setValue(Integer newvalue)
{
this.value = newvalue;
} // setValue(Integer)
public Integer getValue()
{
return this.value;
} // getValue()
public void swap(IntegerVariable other)
{
Integer tmp = this.value;
this.setValue(other.getValue());
other.setValue(tmp);
} // swap(IntegerVariable other)
} // class IntegerVariable
There is a problem with this solution. The problem is that we have to implement a XXXVariable class for every class XXX.
1. Transform the implementation for class IntegerVariable to a generic class called Variable. Submit all .java files.
2. Provide a 1-2 page design description of your implementation based on the following
3. Provide JUnit tests for your implementation.