面向对象实验五答案

合集下载

面向对象软件工程 第五章 答案

面向对象软件工程 第五章 答案

Object-Oriented Software Engineering: Using UML, Patterns, and Java: Solutions to Exercises5. Analysis: Solutions5-1Consider a file system with a graphical user interface, such as Macintosh’s Finder, Microsoft’s Windows Explorer, or Linux’s KDE. The following objects were identified from a use case describing how to copy a file from a floppy disk to a hard disk: File, Icon, TrashCan, Folder, Disk, Pointer. Specify which are entity objects, which are boundary objects, and which are control objects.Entity objects: File, Folder, DiskBoundary objects: Icon, Pointer, TrashCanControl objects: none in this example.5–2Assuming the same file system as before, consider a scenario consisting of selecting a file on a floppy, dragging it to Folder and releasing the mouse. Identify and define at least one control object associated with this scenario.The purpose of a control object is to encapsulate the behavior associated with a user level transaction. In this example, we identify a CopyFile control object, which is responsible for remembering the path of the original file, the path of the destination folder, checking if the file can be copied (access control and disk space), and to initiate the file copying.5–3Arrange the objects listed in Exercises 5-1 and 5–2 horizontally on a sequence diagram, the boundary objects to the left, then the control object you identified, and finally, the entity objects. Draw the sequence of interactions resulting from dropping the file into a folder. For now, ignore the exceptional cases.Figure 5-1 depicts a possible solution to this exercise. The names and parameters of the operations may vary. The diagram, however, should at least contain the following elements:•Two boundary objects, one for the file being copied, and one of the destination folder.•At least one control object remembering the source and destination of the copy, and possibly checking for access rights.•Two entity objects, one for the file being copied, and one of the destination folder.In this specific solution, we did not focus on the Disk, Pointer, and TrashCan objects. The Disk object would be added to the sequence when checking if there is available space. The TrashCan object is needed for scenarios in which Files or Folders are deleted.Note that the interaction among boundary objects can be complex, depending on the user interface components that are used. This sequence diagram, however, only describes user level behavior and should not go into such details. As a result, the sequence diagram depicts a high level view of the interactions between these object, not the actual sequence of message sends that occurs in the delivered system.Object-Oriented Software Engineering: Using UML, Patterns, and Java: Solutions to ExercisesFigure 5-2Sample solution for Exercise 5–45–5Identify the attributes of each object that are relevant to this scenario (copying a file from a floppy disk to a hard disk). Also consider the exception cases “There is already a file with that name in the folder” and “There is no more space on disk.”Object-Oriented Software Engineering: Using UML, Patterns, and Java: Solutions to ExercisesThe scenario implies that icons have positions (since they can be moved) and that they can be selected. Each Item, Folder or File, has a size attribute that is checked against the available space in the Disk. The exception “There is already a file with that name in the folder” implies that Item names are unique within a Folder. We represent this by adding a qualifier on the relationship between Item and Folder. Finally, when copying a file, we need to copy its contents, hence we add a contents attribute on the File object.Figure 5-4 A naive model of the Gregorian calendar (UML class diagram).5–7Consider the object model of Figure 5-32 in the book. Using association multiplicity only, can you modify the model such that a developer unfamiliar with the Gregorian calendar could deduce the number of days in each month? Identify additional classes if necessary.The purpose of this exercise is to show the limitation of association multiplicities. There is no complete solution tothis problem. A partial solution indicating the number of days in each month is depicted in Figure 5-5. We createdfour abstract classes for each of the possible month lengths, 11 classes for each of the nonvariable months and twoclasses for the month of February. The part that cannot be resolved with association multiplicities is the definition ofa leap year and that the number of days in February depends on whether the year is leap or not. In practice, thisObject-Oriented Software Engineering: Using UML, Patterns, and Java: Solutions to Exercisesproblem can solved by using informal or OCL constraints, described in more detail in Chapter 9, Object Design: Specifying Interfaces.Figure 5-5Revised class diagram indicating how many days each month includes.5–8Consider a traffic light system at a four-way crossroads (e.g., two roads intersecting at right angles). Assume the simplest algorithm for cycling through the lights (e.g., all traffic on one road is allowed to go through the crossroad while the other traffic is stopped). Identify the states of this system and draw a statechart describing them. Remember that each individual traffic light has three states (i.e. green, yellow, and red).Object-Oriented Software Engineering: Using UML, Patterns, and Java: Solutions to ExercisesWe model this system as two groups of lights, one for each road. Opposing lights have always the same value. Lights from at least one group must always be red for safety reasons. We assume there are no separate lights for left and right turns and that cycles are fixed. Figure 5-6 depicts the startchart diagram for this solution.5–10Consider the addition of a nonfunctional requirement stipulating that the effort needed by Advertisers toobtain exclusive sponsorships should be minimized. Change the AnnounceTournament and the ManageAdvertisement use cases (solution of Exercise 4-12) so that the Advertiser can specify preferences in her pro file so that exclusive sponsorships can be decided automatically by the system.Figure 5-7Sample solution for Exercise 5–5Object-Oriented Software Engineering: Using UML, Patterns, and Java: Solutions to ExercisesA new use case, ManageSponsorships , should be written and included in the ManageAdvertisement use case and de fine the preferences that the Advertiser can specify for automatically accepting an exclusive sponsorship. Figure 5-8 depicts a possible solution for this use case.In the AnnounceTournament use case, steps 6 & 7 are the modi fied as follows:6.The system noti fies the selected sponsors about the upcoming tournament and the flat fee for exclusive sponsorships. For advertisers who selected an automatic sponsorship preferences, the system automatically generates a positive answer if the fee falls within the bounds speci fied by the Advertiser.7.The system communicates their answers to the LeagueOwner .5–11Identify and write de finitions for any additional entity, boundary, and control objects participating in theAnnounceTournament use case that were introduced by realizing the change speci fied in Exercise 5-10.As speci fied above, this exercise should result in the identi fication of two new entity objects:•SponsorshipPreferences stores the time frame during which a tournament can be considered for an automatic sponsor ship and the maximum fee the Advertiser is willing to pay without being noti fied. This object also has associations to the leagues that should be considered during the automatic sponsorship decision.• ExclusiveSponsorship represents an exclusive sponsorship that was awarded to an Advertiser . It is associatedwith the tournament and Advertiser objects involved in the sponsorship. It stores a boolean indicating whether or not the sponsorship was decided automatically.However, the instructor may choose to extend the exercise so that the students also consider the objects participating in the ManageSponsorships use case. In this case, the student should identify a control object for the new use case and two boundary objects for displaying the exclusive sponsorships and the preferences, respectively.5–12Update the class diagrams of Figure 5-29 and Figure 5-31 to include the new objects you identi fied in Exercise5-11.Use case nameManageSponsorships Participating actorsInitiated by Advertiser Flow of events 1.The Advertiser requests the sponsorships area..2. ARENA displays the list of tournaments for which the Advertiserhas exclusive sponsorships and indicates for each tournamentwhether the exclusive sponsorship was decided by the system orthe Advertiser .3.The Advertiser may change the conditions under which atournament becomes a candidate for her exclusive sponsorship,including specifying a set of leagues, a time frame, or a maximumfee that she is willing to pay for an exclusive sponsorship. TheAdvertiser may also select not to use the automatic sponsorshipfeature and be noti fied about such opportunities directly.4.ARENA records the changes of preferences for the automatic sponsorships. Entry condition•The Advertiseris logged into ARENA .Exit conditions •ARENA checks the advertisers automatic sponsorship pro file for any new tournaments created after thecompletion of this use case.Figure 5-8ManageSponsorships use case.Figure 5-9 depicts the objects discussed in Exercise 5-11 and their associations.5–13The Figure 5-1 A UML startchart diagram for AnnounceTournamentControl .Create TournamentWait For Sponsor RepliesDecide on SponsorshipWait for Player Applicationsnotify sponsorship deadline expired notify players Decide on Interest Groupssponsors notify sponsors。

面向对象实验题及答案(全)

面向对象实验题及答案(全)

实验 2 引用与函数重载
2A:引用作为函数的参数
下面的主函数需要调用函数 setdata,用于求 x 数组前 n 个元素的最大值、最小值、平均 值,编写函数 setdata,使得程序可以正确运行。
#include <iostream>
using namespace std;
#define N 20
3A:单文件编程
了解基于类的编程的基本框架。完成以下操作: (1) 启动 VC,新建操作台工程及源程序文件 (2) 将上述代码粘贴到该文件内。 (3) 查看“类视图”和“文件视图”中有哪些内容,查看相应文件夹中的文件名称 (4) 在类声明与 main 之间编写 CMyDate 的各个方法成员的代码 (5) 在类声明的前面添加#include 等项 (6) 运行程序,观察结果 (7) 为程序添加“星期几”的功能,相应地修改相关函数成员 (8) 运行程序,观察结果
添加了哪些内容。检查目前 CMyDate 类中有哪些成员。 (4) 以集成环境辅助方式为 CMyDate 类添加数据成员 m_year,观察“类视图”的变化。 (5) 以手工方式为 CMyDate 类添加数据成员 m_month,观察“类视图”的变化。 (6) 添加数据成员 m_day 和 m_week,方式自选。 (7) 以集成环境辅助方式添加公共的 Set 函数成员及 Display 函数成员,观察“类视图”以
};
void main() {
CMyDate d1(2049,10,1),d2(2001,1,1); d1.Display(); cout <<endl; d2.Display(); cout<<endl; d2.Set(2001,1,1); d2.Display(); cout<<endl; d2=d1; d2.Display(); cout<< endl; }

《C++面向对象程序设计答案》-第五章--谭浩强-清华大学出版社

《C++面向对象程序设计答案》-第五章--谭浩强-清华大学出版社

1:#include<iostream>usingnamespacestd;classStudent{public:voidget_value(){cin>>num>>name>>sex;}voiddisplay(){cout<<"num:"<<num<<endl;cout<<"name:"<<name<<endl;cout<<"sex:"<<sex<<endl;}private:intnum;charname[10];charsex;};classStudent1:publicStudent{public:voidget_value_1(){get_value();cin>>age>>addr;}voiddisplay_1(){cout<<"age:"<<age<<endl;//引用派生类的私有成员,正确。

cout<<"address:"<<addr<<endl;}//引用派生类的私有成员,正确。

private:intage;charaddr[30];};intmain(){Student1stud1;stud1.get_value_1();stud1.display();stud1.display_1();return0;}2:#include<iostream>usingnamespacestd;classStudent{public:voidget_value(){cin>>num>>name>>sex;}voiddisplay(){cout<<"num:"<<num<<endl;cout<<"name:"<<name<<endl;cout<<"sex:"<<sex<<endl;}private:intnum;charname[10];charsex;};classStudent1:privateStudent{public:voidget_value_1(){get_value();cin>>age>>addr;}voiddisplay_1(){display();cout<<"age:"<<age<<endl;//引用派生类的私有成员,正确。

面向对象程序设计实验项目及答案

面向对象程序设计实验项目及答案

面向对象程序设计实验项目及答案实验一 C++面向过程练习(一)(2学时) ? 实验目的和要求:1.掌握C ++程序的基本格式与规范,学会编写简单的C ++程序。

2.理解C ++程序的结构和特点。

3.熟悉C ++程序基本的输入和输出操作。

4.掌握C ++数据类型、常量和变量以及修饰符的使用方法。

5.掌握C ++的各种流程控制语句的使用。

6. 熟悉C ++语言实验环境,能够在VC6.0环境下调试与运行程序。

? 实验内容:1. 求解方程02=++c bx ax 在复数域上的解,a,b,c 由终端输入。

注意分析的全面性。

#include#includeusing namespace std;int main(){double a,b,c,d,e,m,n;cout<<"a=";cin>>a;cout<<"b=";cin>>b;cout<<"c=";cin>>c;while(1){if(a==0){m=-b/c;cout<<"x1=x2="<<m<<endl;< bdsfid="119" p=""></m<<endl;<>}else{d=b*b-4*a*c;if(d>=0){e=sqrt(d);m=(-b+e)/(2*a);n=(-b-e)/(2*a);if(m!=n)cout<<"x1="<<m<<endl<<"x2="<<n<<endl;<bdsfid="131" p=""></m<<endl<<"x2="<<n<<endl;<> elsecout<<"x1=x2="<<m<<endl;< bdsfid="134" p=""></m<<endl;<>}else{e=sqrt(-d);m=-b/(2*a);n=e/(2*a);cout<<"x1="<<m<<"+"<<n<<"i"<<endl;< bdsfid="143" p=""></m<<"+"<<n<<"i"<<endl;<>cout<<"x2="<<m<<"-"<<n<<"i"<<endl;< bdsfid="145" p=""></m<<"-"<<n<<"i"<<endl;<>}}cout<<"a=";cin>>a;cout<<"b=";cin>>b;cout<<"c=";cin>>c;}return 0; }2. 猴子吃桃子问题:一天一只小猴子摘下一堆桃子,当即吃掉一半,还觉得不过瘾,又多吃了一个。

《C++面向对象程序设计答案》-第五章--谭浩强-清华大学出版社之欧阳化创编

《C++面向对象程序设计答案》-第五章--谭浩强-清华大学出版社之欧阳化创编

1:#include <iostream>using namespace std;class Student{public:voidget_value(){cin>>num>>name>>sex;}void display( ){cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl;} private :int num;char name[10];char sex;};class Student1: public Student {public:void get_value_1(){get_value();cin>>age>>addr;}void display_1(){ cout<<"age: "<<age<<endl; //引用派生类的私有成员,正确。

cout<<"address: "<<addr<<endl;} //引用派生类的私有成员,正确。

private:int age;charaddr[30];};int main(){Student1 stud1;stud1.get_value_1();stud1.display();stud1.display_1();return 0;}2:#include <iostream>using namespace std;class Student{public:voidget_value(){cin>>num>>name>>sex;}void display( ){cout<<"num: "<<num<<endl;cout<<"name: "<<name<<endl;cout<<"sex: "<<sex<<endl;}private :int num;char name[10];char sex;};class Student1: private Student{public:void get_value_1(){get_value();cin>>age>>addr;}void display_1(){display();cout<<"age: "<<age<<endl; //引用派生类的私有成员,正确。

《python语言实训》面向对象程序设计实验

《python语言实训》面向对象程序设计实验

《python语言实训》面向对象程序设计实验一、实验目的1、掌握面向对象程序设计的思想。

2、掌握类的定义。

3、掌握对象的创建和使用。

4、掌握属性和方法的访问控制、★△类属性和实例属性、类的方法。

5、掌握继承、多重继承、多态。

6、掌握程序流程控制语句、函数与模块、数据结构等知识内容与面向对象程序设计的综合应用。

二、实验内容1、对象的创建和使用,在Python中,用赋值的方式创建类的实例,创建对象后,通过实例对象访问这个类的属性和方法。

2、从面向过程到面向对象,面向过程程序设计采用自顶向下的方法,分析出解决问题所需要的步骤,将程序分解为若干个功能模块,每个功能模块用函数来实现。

3、定义属性、定义方法、创建实例对象、调用属性和方法。

4、构造方法,在生成对象时调用,可以用来进行一些属性初始化操作,不需要显示去调用,系统会默认去执行。

5、类方法、实例方法和静态方法,方法的访问控制使用。

6、类与对象的应用、属性与方法的应用。

7、类中内置的方法、类方法、实例方法、静态方法。

8、继承与多态的应用、面向对象程序设计案例的应用。

9、Python程序控制结构、数据结构、函数与模块与面向对象程序设计方法的综合案例。

三、主要实验设备及器材1、计算机终端1套(配备Windous10系统或Windows7系统,英特尔i5及以上的处理器,4G以上内存,256G硬盘)。

2、多媒体教学设备1套。

3、网络设备套件1套。

4、Python软件、PyCharm软件、Anaconda软件等Python软件1套。

四、实验步骤(1)演示性实验教师利用多媒体等设备,对本章节的知识在Python语言的相关软件上进行演练。

(2)验证性实验学生指对Python的语言对象有了一定了解,并提出自己的相关问题,然后学生利用实验室的电脑设备解决问题。

(3)设计性实验设计程序以解决金融工程中的计量分析问题。

2、实验步骤(1)Python的Car对象的创建。

(2)在精灵宝可梦游戏中创建小火龙角色,对给出的各个属性进行迭代和私有化。

05b答案

05b答案
c.display();
return 0;
}
executing Point destructor
(2)(3分)程序运行结果为:
executing Circle destructor
executing Point destructor
(3)(3分)答:指针p所指向的对象所占用的存储空间没有释放。
(4)(3分)答:虚函数的作用是允许在派生类中重新定义与基类同名的函数,并且可以通过基类指针或引用来访问基类和派生类中的同名函数。
三、填空题(每空2分,共10分)
解答:(评分标准:每答对一个2分,注意:功能上等价的也正确)
(1)、pHead->Next
(2)、return 1
(3)、p->Next
(4)、2
(5)、X=num
四、阅读下面的程序,写出程ห้องสมุดไป่ตู้运行的结果。(共26分)
1、(评分标准:每答对一个得2分,每错输出一个换行,扣一分。)
课程名称:面向对象程序设计参考答案及评分标准(B卷闭卷)
一、判断题,正确的打√,错误的打×(每小题1分,共计10分)
(评分标准:每答对一个1分)
1、×2、×3、√4、×5、√6、×7、×8、×9、√10、√
二、简答题(每小题8分,共24分)
1、(评分标准:前两个知识点各3分,后一个知识点2分)
答:在调用一个函数的过程中又出现直接或间接地调用该函数本身,称为函数的递归调用。
for(int j=0;j< Column;j++)
{c. ValueM [i][j]=a. ValueM t[i][j]+b. ValueM [i][j];}
return c;

JAVA面向对象程序设计(第二版)袁绍欣第五章答案

JAVA面向对象程序设计(第二版)袁绍欣第五章答案

JAVA⾯向对象程序设计(第⼆版)袁绍欣第五章答案1.⾯向对象的主要特征是什么?封装、继承、多态2.封装是如何实现的?封装通过访问控制符实现的,只有通过授权才能访问数据。

3.对象之间如何相互作⽤?作⽤的条件是什么?通过消息,拥有访问类数据成员或成员⽅法的权限。

4.protected修饰符的特点?只有包内其它类、⾃⼰和⼦类可以访问。

5.Object都有哪些⽅法?Object clone() //克隆对象boolean equals() //判断两个引⽤是否指向同⼀个对象void finalize() //释放对象int hashCode() //获得代表对象特有的 hashcode 整数String toString() //代表对象的字符串void notify() //线程同步中唤醒等待线程void wait()//线程同步中线程等待6.重载的⽅法之间⼀般有什么关系?⽅法名相同,⽽参数不同。

返回类型可以相同也可以不同。

7.⼦类覆盖⽗类⽅法需要什么条件?⼦类中定义与⽗类同名的⽅法⼀定是覆盖吗?⽅法名,参数,返回类型全部相同,内容不同。

不⼀定8.封装、继承和多态在⾯向对象设计中的⽤途是什么?封装可以保护类的数据成员,隐藏信息。

⼦类继承⽗类的属性和⽅法后,不⽤重新编写代码,提⾼了代码复⽤性。

多态最重要的是⼦类的功能可以被⽗类的引⽤调⽤,向后兼容。

9.设计Src和Dis两个类,Src中有⼀个被封装的属性,类型类 int ,每当通过特定⽅法更改Src对象中的这个属性后,Dis对象都能得到通知,并向Src发消息获得此属性值。

public class Src{int num;Dis dis;Src(int n){if(n>0)num = n;}void SetDis(Dis d){dis = d;}void Change(int n){if(n>0&&n!=num){num = n;dis.print();}}int getNum(){return num;}public static void main(String[] args) {Src src = new Src(1);Dis dis = new Dis(src);src.SetDis(dis);for(int i=0;i<5;i++){src.Change(i);}}}public class Dis{Src src;Dis(Src s){src = s;}void print(){System.out.println("num被改变为:"+src.getNum()); }}/*结果:num被改变为:2num被改变为:3num被改变为:4*/。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include <string.h>
class person
{protected:
char name[10];
intage;
public:
person(char n[],int a)
{
strcpy(name,n);
age=a;
}
void disp() {
cout<<"姓名:"<<name<<endl;
4.编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、年龄、班级和成绩,教师数据有编号、姓名、年龄、职称和部门。要求将编号、姓名输入和显示设计成一个类person,并作为学生数据操作类student和教师类数据操作类teacher的基类。
参考程序如下:
#include <iostream.h>
}
double getedge()
{
return edgelength;
}
};
class table
{
double price;
public:
table(double p)
{
price=p;
}
double getprice()
{
return price;
}
};
class squaretable:public square,public table
这主要考查对继承机制的应用。这里方桌类由正方形类和桌子类派生而来,所以采用多重继承编写此题
参考程序如下:
#include<iostream.h>
#include<string.h>
class square
{
double edgelength;
public:
square(double i)
{
edgelength=i;
}
};
class student : public person
{
char sid[12];
char cnum[12];
double score;
public:
student(char *s1,char *s2,int a, char *s3,double i):person(s2,a)
{
strcpy(sid,s1);
{
char color[10];
public:
squaretable(double el,double prc,char *c):square(el)(color,c);
}
void disp()
{
cout<<"颜色:"<<color<<endl
<<"价格:"<<getprice()<<endl
cout<<"年龄:"<<age<<endl;
}
};
class teacher : public person
{
char tid[10];
char dept[20];
char post[10];
public:
teacher(char *s1,char *s2,int i,char *s3,char *s4):person(s2,i)
<<"桌面尺寸:"<<getedge()<<"*"<<getedge()<<endl;
}
};
void main()
{
squaretable st(60,1234,"red");
st.disp();
}
{
strcpy(tid,s1);
strcpy(dept,s3);
strcpy(post,s4);
}
void disptech()
{
cout<<"编号:"<<tid<<endl;
person::disp();
cout<<"工作部门:"<<dept<<endl;
cout<<"职称:"<<post<<endl<<endl;
student s1("2007023","李涛",19,"07001",510);
t1.disptech();
s1.dispstu();
return 0;
}
5.编写一个程序声明一个正方形类、一个桌子类和一个方桌类,方桌类由桌子类和正方形类派生,定义一格方桌类对象,并输出该桌子的颜色,价格,以及桌面尺寸。
strcpy(cnum,s3);
score=i;
}
void dispstu() {
cout<<"学号:"<<sid<<endl;
person::disp();
cout<<"班级:"<<cnum<<endl;
cout<<"成绩:"<<score<<endl;}
};
int main()
{teacher t1("001","张伟",38,"信息学院","教授");
相关文档
最新文档