《高级数据库系统习题解答1》由会员分享,可在线阅读,更多相关《高级数据库系统习题解答1(25页珍藏版)》请在金锄头文库上搜索。
1、高级数据库系统习题解答 (1)第一次作业7.2解:第三句有问题,左边为string类型,右边是City类型。CityOfLA.name := cityOfLA.mayor.spouse.livesIn; 第一次作业7.4解:前一种的输出结果为:Donald DuckMickey Mouse后一种的输出结果为:6060因为前一种是引用语义,而后一种是复制语义。 第一次作业7.9解:(1), (2) 从引用语义考虑,(3), (4) 从复制语义考虑。 (1)(2)执行完毕后,mary.chilaren=joe.children=littleJoe第一次作业7.9解: (3)(4)执行完毕后,bet
2、ty.children = jimbo, jim.children = 。第二次作业8.8解:surface: 计算表面积。scale: 按比例放大/缩小Cuboid的尺寸。center: 返回Cuboid的中心坐标。diagonal: 计算对角线长度。minDistance: 计算Vector参数到Cuboid的最短距离。第二次作业8.8解:persistent type Cuboid ispublic length, width, height, surface, volume, weight, translate, scale, rotate, certer, diagonal, min
3、Distance;body v1, v2, v3, v4, v5, v6, v7, v8 : Vetex; mat : Material; value : float;operations declare surface : float; declare scale : Vertex void code scaleCuboid; declare center : Vertex; declare diagonal: float; declare minDistance : Vertex float code minDistanceCode; 第二次作业8.8解:implementationdef
4、ine surface isreturn 2.0 * (self.length*self.width + self.length*self.height + self.width*self.height); define scaleCuboid(s) is begin self.v1.scale(s); self.v8.scale(s); end define scaleCuboid; 第二次作业8.8解:define center is var c : Vertex; beginc.create;c.x = 0.5 * (self.v1.x + self.v7.x);c.y = 0.5 *
5、(self.v1.y + self.v7.y);c.z = 0.5 * (self.v1.z + self.v7.z);return c; end define certer; define diagonal isreturn self.v1.distance(self.v7); 第二次作业8.8解:define minDistanceCode(v) is var v0; begin/将长方体的6个面无限延伸,可将整个空间分为27个区域if (v在长方体内部或表面上) return 0; else begin 根据v所在区域,可简单判断出长方体上距v最近的点v0所在 的面/棱/顶点,进而求出v
6、0; return v.distance(v0); end elseend deine minDistanceCode;end type Cuboid; 第二次作业9.1解:(1)方法一采用1:1关系表示1:N关系,存在较多冗余;不考虑索引,已知left查询对应的right集时,方法二效果明显好于方法一;已知right查询对应的left时,方法一效果好于方法二。当插入新关系时,两种方法都无法保证一致性,即原关系1:N的语义约束可能被违反,需要对insert操作做修改,保证每一个Tright实例仅有至多一个对应的Tleft实例。 删除关系时,方法一中直接删除对应的TR实例,方法二中只需修改rig
7、ht集合,直到right集合为空时,才需要删除对应的TR实例。更新操作由插入删除操作组合而成,不再讨论。(2)方法一、二的insert操作均需修改,以保证一致性,方法二的delete操作也需要修改。修改思想上边已说明,具体算法不再给出。 第二次作业9.7解:在对象内部使用计数器对于专用对象,生成实例,置为1,被引用,置为0;对于依赖对象,引用+1,不再引用-1,为0时删除对象。第三次作业10.5解:合法的重定义要求:操作名不变,参数个数不变;操作的接受者类型是原操作中接受者类型的子类;操作的返回值类型是原操作返回值的子类;操作的参数类型是原操作参数类型的超类。题中的重定义仅满足(1)(2)(3
8、),但违反(4)。ConicalPipe是Pipe的子类而非超类,故不合法。考虑程序段:var aPipe, anotherPipe : Pipe;aConicalPipe : ConicalPipe;anotherPipe := aConicalPipe; / 可替换性,合法anotherPipe.connect(aPipe); / 编译通过,执行时动态绑定错误。第三次作业10.6解:继承属性的类型是不能重定义的,必须保持原类型。(1) 子类中继承属性的类型不能是该类型的子类,即特化不合法。特化举例:type Person isbody name : string; age : int;ty
9、pe Employee supertype Person isbody boss : Employee;type Manager supertype Employee isbody refine boss : Manager;第三次作业10.6解:程序段:var anEmp : Employee; aMgr : Manager;aMgr.boss := anEmp; /语法错误anEmp.boss := aMgr; /可替换性,合法anEmp.boss.boss := anEmp; /语法检查合法,但有潜在问题 (2) 子类中继承属性的类型不能是该类型的超类,即泛化不合法。 Person和Em
10、ployee的类型定义同上,Manager类型定义如下: type Manager supertype Employee is body refine boss : Person; 程序段:var aPerson : Person; anEmp : Employee; aMgr : Manager;anEmp.boss := anEmp; /合法aMgr.boss := anEmp; /可替换性,合法aMgr.boss.boss := anEmp; /语法错误 第三次作业10.11解:略第三次作业12.3解:(1)Polymorph declare member (ListType = ) :
11、 ListType | ElemType bool;define member (t) is var item : ElemType; begin foreach (item in self) if (item = t) return true; return false; end define member; 第三次作业12.3解:(2)Polymorph declare nthmember (ListType = ) : ListType | int ElemType;define nthmember (n) is var i : int; item : ElemType; Beginif
12、 (n self.length | n 1)return null;i := 0;foreach (item in self) begini+;if (i = n) return item; endfor end define nthmember; 第三次作业12.3解:(3)Polymorph declare substitute (ListType = ) : ListType | ElemType, ElemType void; define substitute(old, new) isvar item : ElemType;begin foreach (item in self) b
13、egin if (item = old) begin self.delete(old); self.insert(new); endif endforend define substitute;第三次作业12.3解:(4)Polymorph declare sublist(ListType = ) : ListType | int, int ListType; define sublist(m, n) isvar newlist : ListType; item : ElemType; i : int;begin i := 0; if (i = 1 & i = k & k = m & i e.
14、worksin.mgr.salary;第四次作业14.9解:Retrieve all Managers of the R&D department(s), who supervise Emps located in the Building called “E1”.select mfrom m in Manager, e in EMPwhere m = e.worksin.mgr and m.worksin.name = R&D and e.office.building = E1;第四次作业18.1解:schema C is subsschema F;subsschema G;end schema C;schema E is subsschema H;subsschema I;end schema E;schema A is subsschema B;subsschema C;end schema A;schema B is subsschema D;subsschema E;end schema B;第四次作业18.2解:schema B ispublic Sinterfacetype S is ;implementationtype T is;end schema B;