here

AMCM048 Advanced Program Design
Test: 31 October 2008
Write your exam number* here:
Time: 15.05­16.55
Answer every question in the box provided.
Each question is worth 10 marks. Total 100 marks.
Answers
Use the supplementary boxes on the last sheet if you want to change any answer.
Question 1)
Association and aggregation are two forms of composition. Explain the difference between them.
Composition can be defined as “an organized collection of components
interacting to achieve a coherent, common behaviour”. In a software system
developed in an object-oriented way those components would be objects.
An object may contain links to further objects. If the objects it links to exist
only to make up the whole object, are not shared by any other outside object,
and thus can be considered just as parts of this object, then this is
“aggregation”. Aggregation is when parts which do not have an independent
existence are put together to form a whole.
If the objects an object links to are shared, so that other objects may also
have links with them and access them directly, then this is “association”.
Association is when we put together access to other things to make a whole
without it having complete control of those other things.
Question 2)
Explain the difference between a mutable and an immutable object
An object contains links to data values and other objects. If these are set
when it is constructed and there is no way of changing them, then the object
is immutable. This requires that all fields are private or final, so they cannot
be changed by direct access and that there are no methods whose effect is
to change the value of a field or to change the object it refers to. It also
requires that none of the objects the fields refer to is aliased by a field of
another object or could be aliased as a result of a method call (“exposing
the rep”), unless the shared object is also immutable. As an exception to
this, changes can be made internally (“benevolent side effect”) if these
changes have no effect on the value returned by any public method of the
object (this would be done for efficiency reasons, so it could change the
time and/or space requirements of the object).
Any object which is not immutable is mutable.
*The exam number consists of two letters and three digits, e.g. AB123. It is on your Queen Mary identity card, if you do
not have it, put your name here.
The following three Java classes form part of a program for a fantasy world game. They will
be used in the next five questions.
class Being
{
protected int strength;
public Being(int s)
{
strength=s;
}
public int getStrength()
{
return strength;
}
public String toString()
{
return strength+” strength being”;
}
}
class Monster extends Being
{
private String colour;
private int legs;
public Monster(int s,int l,String c)
{
super(s);
legs=l;
colour=c;
}
public String toString()
{
return colour+” “+legs+”­legged monster”;
}
}
class Hero extends Being
{
private String name;
private int weapon;
public Hero(int s,String nm,int w)
{
super(s);
weapon=w;
name=nm;
}
public int getStrength()
{
return strength+weapon;
}
public String toString()
{
return name;
}
public void pickup(int w)
{
weapon=w;
}
}
Question 3)
Suppose we have the following variable declarations:
Hero h1,h2;
Monster m;
Being b;
For each of the following code fragments, write what executing it would cause to be
printed:
i)
m = new Monster(10,6,"blue");
System.out.println(m);
blue 6-legged monster
ii)
m = new Monster(8,4, "red");
b=m;
System.out.println(b);
red 4-legged monster
iii)
h1 = new Hero(12, "Hercules",10);
h1.pickup(9);
System.out.println(h1+" has strength "+h1.getStrength());
Hercules has strength 21
iv)
h1 = new Hero(7, "Jason",5);
h2 = new Hero(6, "Perseus",4);
h1.pickup(8);
h1 = h2;
System.out.println(h1+" and "+h2.getStrength());
Perseus and 10
v)
h1 = new Hero(6, "Odysseus",2);
h2 = new Hero(8, "Theseus",5);
h1 = h2;
h1.pickup(7);
System.out.println("Strengths are: "+h1.getStrength()+""+h2.getStrength());
Strengths are 15 15
vi)
b = new Being(5);
System.out.println("First: "+b+" with "+b.getStrength());
h1 = new Hero(6, "Achilles",5);
b=h1;
System.out.println("Second: "+b+" with "+b.getStrength());
h1.pickup(3);
System.out.println("Third: "+b+" with "+b.getStrength());
First: 5 strength being with 5
Second: Achilles with 11
Third: Achilles with 9
Question 4)
Write an instance method swap to go in class Hero which would have the effect that if
h1 and h2 are of type Hero, the call h1.swap(h2) will cause the objects referred to by
these variables to swap their weapon values. So:
h1 = new Hero(6,"Odysseus",3);
h2 = new Hero(8, "Theseus",7);
System.out.print(h1+" with "+h1.strength()"+" and ");
System.out.println(h2+” with “+h2.strength()”);
h1.swap(h2);
System.out.print(h1+" with "+h1.strength()"+" and ");
System.out.println(h2+” with “+h2.strength()”);
will cause:
Odysseus with 9 and Theseus with 15
Odysseus with 13 and Theseus with 11
to be printed
void swap(Hero h)
{
int temp = weapon;
weapon=h.temp;
h.weapon=temp;
}
Question 5)
Write a method equals to go inside class Monster which would override the method
equals which is inherited from Object. If m1 and m2 are of type Monster, the call
m1.equals(m2) should return true if and only if all the fields of the objects they refer to
are equal.
public boolean equals(Object obj)
{
if(! obj instanceof Monster)
return false;
Monster m = (Monster) obj;
return strength==m.strength&&legs=m.legs&&colour.equals(m.colour);
}
Question 6)
Suppose we have a class Coffee which has in it a method with signature:
public int getStrength()
We want to write some code which relies only on the method getStrength(), and we want it to be applicable to Coffee, Being, Monster and Hero objects. So, for example, we want to write a single method static method stronger which takes two objects of any of these types and returns whichever is the strongest according to their getStrength() method. So if h1 and h2 are of type Hero we can have stronger(h1,h2) which would return h1 or h2, and if c1 and c2 are of type Coffee we can have stronger(c1,c2) which would return c1 or c2.
Explain what is needed to achieve this, give the code for the method stronger and any other code which is needed for it.
We need to create a type which covers an objects which have a getStrength
method with 0 arguments and return type int. This is done through a Java
interface which contains just that method header, here it is:
interface StrengthObject
{
public int getStrength();
}
having given it the name StrengthObject this means a variable of type
StrengthObject can refer to an object of any type which has the method
getStrength with this header in it and which is declared as ‘implements
StrengthObject’. So we must add ‘implements StrengthObject’ after the class
name at the start of class Being and class Coffee. We don’t have to do this for
class Monster and class Hero because they inherit it from class Being. This
makes Coffee, Being, Hero and Monster all subtypes of StrengthObject.
Now we can write a method whose parameters are of type StrengthObject, and
when called these may be set to refer to any object which is of a subtype of
StrengthObject. It can also have return type StrengthObject and return an object
of that type. The method getStrength can be called on variable of type
StrengthObject. When the code for that method runs dynamic binding means the
code for getStrength comes from the class of the object.
Here is the code for the method stronger:
static StrengthObject stronger(StrengthObject s1, StrengthObject s2)
{
if(s1.getStrength()>s2.getStrength())
return s1;
else
return s2;
}
Question 7)
Suppose we have decided that two Hero objects should be regarded as equal if they have the same strength as measured by their getStrength method. So we override the equals method from Object with a method which has the effect that if h1 and h2 are of type Hero then the call h1.equals(h2) will return true if and only if h1.getStrength() returns the same value as h2.getStrength(). Explain why this might cause problems.
There are two problems here.
As this means that two Hero objects would be regarded as equal even though
they have different attributes, different names for example, we need to be sure
we are happy with that for all cases where the method equals is used, as it is
widely used in code provided with the Java system, for example in code for
collection classes.
Also objects of class Hero are mutable, as the pickup() method changes their
weapon field, and this changes the value returned by the getStrength method.
We need to be aware that our proposed definition of equals will mean that if h1
and h2 refer to Hero objects, the value of h1.equals(h2) may change during
run time. Code is often written under the assumption that an equals
relationship between two objects is permanent, so this could cause such code
to behave in an unexpected way.
Question 8)
Explain what is meant by a representation invariant for an implementation of a data abstraction.
The relationship between the data in an object and the abstraction it
implements is called the “abstraction function”. Often it is required that the
data must take particular values in order for it to represent an abstraction, the
methods which provide the public interface to the abstraction rely for their
correct working for the data to be in this form.
For example, a set of integers could be represented by an array of integers,
one requirement would be that no integer is duplicated in the array, it could
also be required that the integers are stored in numerical order so that
membership testing could be done efficiently using binary search.
The representation invariant is a logical statement on the data in an object
stating what values it must take in order for it to be a valid representation of an
abstraction. It is an invariant because for correct operation of the code, it must
always evaluate to true after every method is finished.
Liskov recommends having a method called repOk which tests the data to
ensure it meets the representation invariant. This should be used during
development to check that if the code has been changed it has not
accidentally caused the possibility that data could be left in an invalid format.
Question 9)
Suppose in Java class Alpha we have a method with signature:
public void meth(Beta b) throws BigException, SmallException;
and in class Beta we have a method with signature:
public void mutate();
a)
Suppose we have a variable alf of type Alpha set to refer to an Alpha object, and a variable bet of type Beta set to refer to a Beta object.
Write a fragment of Java code which calls the method meth on alf with bet as its argument. If that call throws a SmallException the message "Small" should be printed, if it throws a BigException the message "Big" should be printed, and if it does not throw an exception the message "OK" should be printed.
try {
alf.meth(bet);
System.out.println(“OK”);
}
catch(SmallException e) {
System.out.println(“Small”);
}
catch(BigException e) {
System.out.println(“Big”);
}
b)
Write a fragment of Java code which calls the method meth on alf with bet as its argument. If the call throws a SmallException the method mutate should be called on bet, and the call to meth made again, and this should be repeated each time the call to meth throws a SmallException until it either throws a BigException or succeeds without throwing an exception. If the final call of meth succeeds without an exception, the message "Success" should be printed, if it throws a BigException the message "Failure" should be printed.
while(true)
try {
alf.meth(bet);
System.out.println(“Success”);
break;
}
catch(SmallException e) {
bet.mutate();
}
catch(BigException e) {
System.out.println(“Failure”);
break;
}
Question 10)
a)
Explain what is meant by a partial specification
A partial specification is one which has a requirement that the values passed
as arguments to a method fall within some specified range of the possible
values as given just by the argument types. The effects of the method are
undefined if it is called with arguments which fall outside this range.
For example, if a method takes an integer and an array of integers, and the
specification just says it must return a position in the array where the integer
argument occurs, it is partial if it not specified what should be returned if the
integer does not occur in the array. The specification should note that it is a
requirement that the integer occurs in the array.
b)
Explain what is meant by an underdetermined specification
An underdetermined specification is one where for some particular arguments
there are more than one effect the method could have but each possible effect
still fits the specification given for the method effects. For example, if a
method takes an integer and an array of integers, and the specification just
says it must return a position in the array where the integer argument occurs,
it is underdetermined because if the integer occurs more than once any of its
positions would be a valid return value
c)
Explain what is meant by a deterministic implementation of an underdetermined specification.
A deterministic implementation is one which always has the same effects for
the same arguments. So for an undetermined specification, even when there
is more than one possible effect for the arguments the implementation ensures
that it is always the same possible effect that is given.
Supplementary Answer Boxes
Question:
Question:
Question:
Question: