7.2
Based on our Person and City object base of Figure 7.11,verify the type safety ( or point out potential type errors) of the following statements:
donald.spouse := cityOfLA.mayor;
cityOfLA.mayor.spouse := cityOfLA.mayor;
cityOfLA.name := cityOfLA.mayor.spouse.livesIn;
cityOfLA.name := cityOfLA.mayor.spouse.name;
Note that you are asked to verify the type safety ——and not to analyze whether the statements are semantically meaningful.
7.4
In the text, we explained the difference between the copy semantics of values and the reference semantics of objects. In our object base of Figure 7.11, the City named “Los Angeles” is a shared object that is referenced by all three Person objects via their livesIn attribute. Discuss the effects of the following program fragment:
…
donald.livesIn.mayor := donald;
print (donald.livesIn.mayor.name);
…
mickey.livesIn.mayor := mickey;
print (donald.livesIn.mayor.name);
Why is the effect substantially different from the characteristics of the following program fragment?
donald.age := mickey.age;
Print (donald.age);
…
mickey.age := 70;
print (donald.age);
7.9
Discuss the difference between the sort personSet and the object type PersonSet. Illustrate your analysis on the following two object types:
Type Person is
[…;
Children: PersonSet;]
Type Person’is
[…;
Children: personSet;]
Type PersonSet is
{Person};
Type personSet is
{Person’};
Furthermore, consider the six variables:
var mary, joe, littleJoe: Person;
betty, jim, jimbo: Person’;
Now describe the difference between the statements(1),(2)and(3),(4):
(0)joe.children.create;
(1)mary.children := joe.children;
(2)mary.children.insert(littleJoe);
(3)betty.children := jim.children;
(4)betty.children.insert(jimbo);
Illustrate your discussion by diagrams showing the states of the resulting object bases.
8.8
Augment the type definition of Cuboid. In particular, add the following operations:
(1) surface, yielding the surface value of the Cuboid;
(2) scale, which scales the size of the Cuboid——becareful that the scale operation is properly implemented for Cuboid that have an arbitrary orientation in the coordinate system;
(3) center, which determines the Vertex representing the center of the Cuboid;
(4) diagonal, which computes the length of the diagonal of the Cuboid;
(5) minDistance, a function that determines the minimum distance of a parameter Vertex from the receiver Cuboid——e.g., for assembly planning in order to avoid collision.
9.1
Reconsider the representation proposed for implementing binary 1:N relationship type as separate type, i.e.:
type TR
with extension is
body
[left: Tleft;
right: Tright];
end type TR ;
It is also possible to implement the relationship with a set-valued attribute right as follows:
type TR
with extension is
body
[left: Tleft;
right: {Tright};]
end type TR ;
(1)Investigate the pros and cons of these representations in comparison with the one proposed in the text.
(2)Outline the operations that needed to enforce the consistency constraints imposed by the 1:N functionality of the relationship type for both representation alternatives.