面向对象程序设计上机作业

合集下载

XXX14秋学期《面向对象程序设计》在线作业答案

XXX14秋学期《面向对象程序设计》在线作业答案

XXX14秋学期《面向对象程序设计》在线作业答案面向对象程序设计14秋学期《面向对象程序设计》在线作业一,单选题1.下列关于this指针的说法,错误的是()。

A。

this指针指向当前正在操作的对象B。

this指针只存在于非静态成员函数中C.在成员函数中对非静态数据成员的访问是通过this指针完成的D.静态成员函数中可以通过this指针访问非静态成员正确答案:D2.已知“int a=10,*p;int *&r=p。

r=*p=20;cout<<a;”,则输出为()。

A。

10B。

20C.不确定值D.程序有错误正确答案:B3.有关重载函数的描述正确的是()。

A.函数名相同的函数就是重载函数B.函数名相同但返回值类型不同的函数就是重载函数C.函数名相同但参数个数或类型不同的函数就是重载函数D.函数功用相同的函数就是重载函数正确答案:C4.在表达式x+y*z中,+是作为成员函数重载的运算符,*是作为非成员函数重载的运算符。

以下叙述中正确的选项是()。

A。

operator+有两个参数,operator*有两个参数B。

operator+有两个参数,operator*有一个参数C。

operator+有一个参数,operator*有两个参数D。

operator+有一个参数,operator*有一个参数正确谜底:C5.以成员函数方式重载运算符“>”,则obj1>obj2被C++编译器解释为()。

A。

operator>(obj1,obj2)B。

(obj1,obj2)C。

obj1.operator>(obj2)D。

obj2.operator>(obj1)正确谜底:C6.下列运算符中,()在C++中不能重载。

A。

:B。

+C。

-D。

<=正确答案:A7.运行以下程序时,如果由键盘输入6514,则输出结果是()。

#includeusingnamespace std。

int main() { int m。

中大实践考核面向对象(C)程序设计试题和答案 (上机考试)

中大实践考核面向对象(C)程序设计试题和答案 (上机考试)

面向对象(C++)程序设计 (上机考试)样题1.下列Shape类是一个表示形状的抽象类,Area()为求图形面积的函数,Total()则是一个通用的用以求不同形状的图形面积总和函数。

请从Shape类派生三角形类(triangle)、矩形类(rectangle),并给出具体的求面积函数。

编写程序验证求面积函数的正确性。

Shape、total的定义如下所示。

Class shape{Pubilc:Virtual float area()=0};float total (shape *s[ ], int n){float sum=0.0;for(int i=0; i<n; i++)sum += s [ I ] ->area ();return sum;}解答: #include <iostream.h>class shape{public:virtual float area()=0;};float total(shape *s[], int n){float sum=0;for(int i=0; i<n; i++)sum += s[i] -> area();return sum;}class triangle : public shape{protected:float H, W;public:triangle(float h, float w) { H=h; W=w;}float area() { return H*W*0.5;}};class rectangle : public triangle{public:rectangle(float h, float w) : triangle(h, w) {}float area() { return H*W;}};void main(){shape *s[4];s[0] = new triangle( 3.0, 4.0 );s[1] = new rectangle( 2.0, 4.0 );s[2] = new triangle( 5.0, 8.0 );s[3] = new rectangle( 6.0, 8.0 );float sum = total(s,4);cout << "The total area is:" << sum << endl;}样题2. 以面向对象的概念设计一个类,此类包括3个私有数据,unlead(无铅汽油), lead有铅汽油, total (当天总收入)。

面向对象系统分析与设计上机作业及其格式规范资料

面向对象系统分析与设计上机作业及其格式规范资料

《面向对象系统分析与设计》课程的上机作业要求:(1)根据教师上课的进度,分别在每周的上机课程中练习下面的题目;(2)每道题目所涉及的图形都应采用Rational XDE完成;(3)小作业的格式按照本文档最后一页的形式给出;(4)每一章的小作业都在这一章授课完成之后的一两周上机课中上交,具体上交时间由教师指定。

(5)上交的小作业不用包含本文档的前9页。

第2章(1)绘制某学校网上选课系统的用例图管理员通过系统管理界面进入系统,建立本学期要开设的各种课程,将课程信息保存到系统中,并可以对课程进行改动和删除。

学生通过客户机浏览器进入系统,选择课程:可以查询课程,选择课程,支付课程费用。

(2)为下图的增加课程用例编写用例描述(3)绘制宾馆客房业务管理用例图,并为每一用例编写用例描述宾馆客房业务管理提供客房预订、预订变更、客房入住、退房结帐、旅客信息查询几个方面的功能。

(4)对图书馆的图书借阅进行用例分析①确定图书管理的参与者;②参与者所看到的图书管理功能;③把这些功能分解为用例;④确定用例之间的关系;⑤画用例图;⑥优化用例图;⑦完成用例描述。

(5)对宾馆客房管理进行用例分析。

①确定宾馆客房管理的参与者;②参与者所看到的客房管理功能;③把这些功能分解为用例;④确定用例之间的关系;⑤画用例图;⑥完成用例描述。

(6)完成电梯系统的用例图及每个用例的描述(7)给出在线拍卖系统的用例图及每个用例的描述(8)书P116试题7.1,7.2(9)书P126试题8.1~8.3(10)请仔细阅读下图,描述该图的基本含义(11)根据下面的陈述,请你分析出参与者和用例,绘制出用例图,并给出每个用例的描述在医生的办公室里接待员、护士和医生使用病人记录和计划安排系统。

当病人第一次来这里看病时,接待员使用该系统来输入病人信息,并且他们安排所有的预约。

护士使用系统来跟踪病人每次看病的结果并输入护理病人的信息,如医疗和诊断。

护士也可以访问这些信息以打印病人诊断结果或病人看病历史。

面向对象程序设计上机作业

面向对象程序设计上机作业

1.编一个程序,程序中包含以下内容:(1) 一个圆类(Circle),包含:属性:园半径radius;常量:PI。

方法:构造方法;求面积方法area();求周长方法:perimeter();显示半径、面积和周长的方法show()。

(2) 主方法main(),在主方法中创建圆类的对象c1和c2并初始化,c1的半径为100,c1的半径为200,然后分别显示两个圆的半径、面积和周长。

#include<iostream>using namespace std;class Circle{public:Circle(double r);double area();double perimeter();void show();private:double radius;const double PI;};Circle::Circle(double r):PI(3.14){radius=r;}double Circle::area(){return PI*radius*radius;}double Circle::perimeter(){return 2*PI*radius;}void Circle::show(){cout<<radius<<' '<<area()<<' '<<perimeter()<<endl;}int main(){Circle c1(100),c2(200);c1.show();c2.show();return 0;}2.编一个程序,程序中包含以下内容:(1)一个学生类(Student),包含:属性:学号s_No,姓名s_Name,性别s_Sex,年龄s_Age,成绩s_Score。

方法:构造方法;显示学号、姓名、性别、年龄及成绩的方法show(),修改成绩的方法modifyScore()。

面向对象程序设计编程实验上机

面向对象程序设计编程实验上机
{Pen myPen=new Pen();
Pen hisPen=new Pen( "GREEN,"8);
hisPen.SetPrice();
Console .WriteLine( "The price is{0}" ,hisPen.GetPrice());
}
}
( 3)使用重载方法求5和5.65的平方。
public int GetPrice()
{return Price;}
public void SetColor( string newColor)
{Color=newColor;}
public string GetColor()
{return Color;}
}
class Test
{
public static void Main()
Console .WriteLine( "The Color is{0}",myPen.Color);
}
}
}
(2)调试构造函数重载Pen()例题
using System;
class Pen
{
public string Color;
private int Price;
public Pen()
{Color="BLACK"; Price= 5;}
public double app1( double y)
{return y*y;}
}
static void Main( string[]args)
{app x=new app();
Console .WriteLine("5的平方是{0}" ,x.app1(5));

面向对象程序设计编程实验上机

面向对象程序设计编程实验上机

面向对象程序设计编程上机作业题目:(1)调试构造函数Pen( ) 例题using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication2{class Pen{public string Color;private int Price;public Pen(){ Color="BLACK";Price=5;}public void SetPrice(int newPrice){ Price=newPrice;}public int GetPrice(){ return Price; }public void SetColor(string newColor)public string GetColor(){ return Color; }}class Test{ public static void Main(){ Pen myPen=new Pen() ;Console.WriteLine("The price is {0}",myPen.GetPrice());Console.WriteLine("The Color is {0}",myPen.Color);}}}(2)调试构造函数重载Pen( ) 例题using System;class Pen{public string Color;private int Price;public Pen(){ Color = "BLACK"; Price = 5;}public Pen(string newColor, int newPrice){ Color = newColor;Price = newPrice;}public void SetPrice(int newPrice){ Price = newPrice; }public void SetPrice(){ Price = 5; }public int GetPrice(){ return Price; }public void SetColor(string newColor){ Color = newColor; }public string GetColor(){ return Color; }}class Test{public static void Main(){ Pen myPen = new Pen();Pen hisPen = new Pen("GREEN", 8);Console.WriteLine("The price is {0}", myPen.GetPrice());Console.WriteLine("The Color is {0}", myPen.Color);Console.WriteLine("The price is {0}", hisPen.GetPrice());Console.WriteLine("The Color is {0}", hisPen.GetColor()); hisPen.SetPrice();Console.WriteLine("The price is {0}", hisPen.GetPrice()); }}( 3) 使用重载方法求5和5.65的平方。

面向对象第3章上机作业参考

面向对象第3章上机作业参考

public class RationalNumber implements Cloneable,Comparable { private int numerator;private int denominator;private int greatestCommonFactor (int n1, int n2) //求最大公约数{……} private int leastCommonMultiple (int n1, int n2) ; //求最小公倍数{……}private void reductionOfFraction(); //约分{……}public RationalNumber() { ……}public RationalNumber(int n, int d) { ……}public void set RationalNumber(int n, int d) { ……}public RationalNumber getRationalNumber() { ……}public int getNumerator() { ……}public int getDenominator () { ……}public RationalNumber add(RationalNumber r) {……}public RationalNumber sub(RationalNumber r) {……}public RationalNumber mul(RationalNumber r) {……}public RationalNumber div(RationalNumber r) {……}public boolean equals(Object otherObject) {if (this == otherObject) return true;if (otherObject == null) return false;if (getClass() != otherObject.getClass()) return false;RationalNumber r = (RationalNumber)otherObject;// 通分,比较是否相等……}public int compareTo(Object otherObject) {……}public RationalNumber clone() throws CloneNotSupportedException { return (RationalNumber)super.clone();}public String toString() { return “……..”; }}public class Course implements Cloneable {private int No;private Strng name;private int classHour;private int classCharacter;private String teacher;private Material[] materials;public Course() {……}public Course(String name, int classHour,int classCharacter, String teacher, Material[] materials) {……}public int getNo(){……}public String getName(){……}public int getClassHour(){……}public int get ClassCharacter(){……}public String getTeacher(){……}public Material[] getMaterials(){……}public void setNo(int No){……}public void setName(String name){……}public void setClassHour(int classHour){……}public void set ClassCharacter(int character){……}public void setTeacher(String teacher){……}public void setMaterials(Material[] materials){……}public boolean equals(Object otherObject) {if (this == otherObject) return true;if (otherObject == null) return false;if (getClass() != otherObject.getClass()) return false;Course c = (Course)otherObject;//比较是否相等……}public Course clone() throws CloneNotSupportedException { Course c= (Course) super.clone(); = (String)name.clone();c.teacher = (String)teacher.clone();c.materials = (Material[])materials.clone();return c;}public String toString() { return “……..”; }}。

大三《面向对象技术》上机作业

大三《面向对象技术》上机作业

《面向对象技术》上机作业一、设计目的和时间安排●掌握面向对象程序设计的基本思路和方法。

●利用所学的基本知识和技能,解决简单的面向对象程序设计问题。

二、内容及要求自学《教材》前14章,利用所学到的Java编程知识和编程技巧,设计一个实际的应用软件,初步认识软件设计的基本方法,提高进行工程设计的基本技能及分析、解决实际问题的能力,为后续课程和以后的工程实践打下良好的基础。

三、考核方式请在考试前将代码打包,命名为“学号_姓名.rar”,发送到guokehua@,注意,千万不能发错,否则后果自负。

四、时间、地点安排见 csu_xxxy_jsj@ 邮箱。

五、题目(根据你的学号尾数除以5的余数,选1题。

)0. 人事信息管理系统:主要功能如下:1.添加功能:添加一个职员的基本信息,包括姓名,身份证号码,家庭住址,联系电话,薪水,性别;2.显示功能:显示所有职员的详细信息;3.查找功能1:根据你键入的职员姓名,显示其详细信息;4.查找功能2:输入性别(男或女),显示该性别所有职员的详细信息。

1.通讯录:主要功能如下:1.添加功能:添加一个联系人的基本信息,包括姓名,电话号码,身份证号码,家庭住址,性别;2.查找功能:根据你键入的联系人姓名,显示其详细信息;4.修改功能:对联系人的基本信息进行修改。

5.删除功能:根据你键入的联系人姓名,可以对其进行删除操作。

2.学生成绩管理系统:主要功能如下:1.添加学生信息功能:添加一个学生的基本信息,包括学号、姓名,性别;2.添加课程信息功能:添加一个课程的基本信息,包括课程号、课程名称;3. 添加考试成绩功能:添加考试成绩:输入一个学生的学号,输入一个课程号,输入分数;4.统计功能:输入姓名,能显示某学生各门科目的课程号和分数。

3.客房管理系统主要功能如下:1.添加客房信息功能:添加一个客房的基本信息,包括房号、价格;2.添加住宿信息:添加顾客住宿信息,包括顾客姓名、身份证号、房号,天数;3. 查询顾客应付款项:输入顾客身份证号,显示其需要付款的数量。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1.编一个程序,程序中包含以下内容:(1) 一个圆类(Circle),包含:属性:园半径radius;常量:PI。

方法:构造方法;求面积方法area();求周长方法:perimeter();显示半径、面积和周长的方法show()。

(2) 主方法main(),在主方法中创建圆类的对象c1和c2并初始化,c1的半径为100,c1的半径为200,然后分别显示两个圆的半径、面积和周长。

#include<iostream>using namespace std;class Circle{public:Circle(double r);double area();double perimeter();void show();private:double radius;const double PI;};Circle::Circle(double r):PI(3.14){radius=r;}double Circle::area(){return PI*radius*radius;}double Circle::perimeter(){return 2*PI*radius;}void Circle::show(){cout<<radius<<' '<<area()<<' '<<perimeter()<<endl;}int main(){Circle c1(100),c2(200);c1.show();c2.show();return 0;}2.编一个程序,程序中包含以下内容:(1)一个学生类(Student),包含:属性:学号s_No,姓名s_Name,性别s_Sex,年龄s_Age,成绩s_Score。

方法:构造方法;显示学号、姓名、性别、年龄及成绩的方法show(),修改成绩的方法modifyScore()。

(2)普通方法int max(Student* pStu),求出所有学生的最高分。

(3)主方法main(),在其中创建5个学生的对象数组stu,然后调用max()方法求出这5个学生的最高分并输出其学号和成绩。

#include<iostream>#include<string>using namespace std;class Student{public:Student(string no,string name,string sex,int age,int score);void show();void modifyScore(int newScore);string s_No;string s_Name;string s_Sex;int s_Age;int s_Score;};Student::Student(string no,string name,string sex,int age,int score){s_No=no;s_Name=name;s_Sex=sex;s_Age=age;s_Score=score;}int max(Student* pStu,int n){int ms=pStu[0].s_Score;for(int i=1;i<n;i++){if(ms<pStu[i].s_Score)ms=pStu[i].s_Score;}return ms;}void Student::show(){cout<<s_No<<' '<<s_Name<<' '<<s_Sex<<' '<<s_Age<<' '<<s_Score<<endl;}void Student::modifyScore(int newScore){s_Score=newScore;}int main(){Student stu[5]={Student("100A","Zhangsan","Male",18,85),Student("100A","Zhangsan","Male",18,85),Student("100A","Zhangsan","Male",18,95),Student("100A","Zhangsan","Male",18,85), Student("100A","Zhangsan","Male",18,85)};cout<<max(stu,5)<<endl;return 0;}1.编写一个人类Person,其中包含姓名、性别和年龄的属性,包含构造方法以及显示姓名、性别和年龄的方法。

再编写一个学生类Student,它继承Person类,其中包含学号属性,包含构造方法以及显示学号的方法。

最后编写main()函数,在main()函数中定义两个学生s1和s2并给他们赋值,最后显示他们的学号、姓名、性别以及年龄。

#include<iostream>#include<string>using namespace std;class Person{public:Person(string n,string s,int a){name=n;sex=s;age=a;}void show(){cout<<name<<'\t'<<sex<<'\t'<<age<<endl;}private:string name;string sex;int age;};class Student:public Person{public:Student(string n,string s,int a,string id);void show();private:string stuID;};Student::Student(string n,string s,int a,string id):Person(n,s,a){stuID=id;}void Student::show(){Person::show();cout<<stuID<<endl;}int main(){Student s1("zhangsan","male",21,"123456");s1.show();return 0;}2.编一个程序,包含以下内容:(1)Point类,该类放在两个文件中(Point.h, Point.cpp),该类包含x和y两个坐标,构造方法。

(2)Circle类,该类放在两个文件中(Circle.h, Circle.cpp)该类继承Point类,其中包含:属性:圆半径radius。

方法:构造方法;求面积方法area();求周长方法perimeter()。

(3)圆柱体类(Cylinder),继承圆类,其中包含:属性:园柱体高度height。

方法:构造方法;求表面积方法area();求体积方法volume();显示圆心点坐标,底圆面积和周长以及圆柱体表面积和体积的方法show()。

(4)主方法main(),在主方法中创建两个圆柱体类的对象cy1和cy2,具体尺寸自己确定,然后分别显示圆柱体cy1和cy2圆心点坐标,底圆面积和周长以及圆柱体表面积和体积。

#include<iostream>using namespace std;class Point{public:Point(int xx,int yy);void show();private:int x;int y;};Point::Point(int xx,int yy){x=xx;y=yy;}void Point::show(){cout<<x<<' '<<y<<endl;}class Circle:public Point{public:Circle(int xx,int yy,int r);double area();double perimeter();void show();private:int radius;Circle::Circle(int xx,int yy,int r):Point(xx,yy){radius=r;}double Circle::area(){return 3.14*radius*radius;}double Circle::perimeter(){return 2*3.14*radius;}void Circle::show(){Point::show();cout<<radius<<' '<<area()<<' '<<perimeter()<<endl; }class Cylinder:public Circle{public:Cylinder(int xx,int yy,int r,int h);double area();double volume();void show();private:int height;};Cylinder::Cylinder(int xx,int yy,int r,int h):Circle(xx,yy,r){ height=h;}double Cylinder::area(){return 2*Circle::area()+Circle::perimeter()*height;}double Cylinder::volume(){return Circle::area()*height;}void Cylinder::show(){Circle::show();cout<<area()<<' '<<volume()<<endl;}int main(){Cylinder cy1(10,20,100,50),cy2(100,200,1000,500);cy1.show();cy2.show();return 0;1.编写一个复数类Complex,其中包含私有整形变量real和imag,构造函数以及显示属性的函数show(),实现两个复数进行+、-、*、/的运算符重载友元函数,实现前缀++和—的成员函数。

相关文档
最新文档