定义Circle类,要求:包括私有属性_radius,构造函数为半径赋值,构造函数的默认参数值为0,析构函数输出适当信息,普通方法SetRadius用于设置半径,内置方法_gt_用于比较两个圆面积

定义Circle类,要求:包括私有属性_radius,构造函数为半径赋值,构造函数的默认参数值为0,
析构函数输出适当信息,普通方法SetRadius用于设置半径,普通方法Area返回圆面积,内置方法_str_用于输出圆面积,
内置方法_gt_用于比较两个圆面积大小,并创建两个实例分别验证上述功能



class Circle:
    _radius=0
    def __init__(self,radius=0):
        self._radius=radius
    def __del__(self):
        print('该数据已经被清空')
    def SetRadius(self,r):
        self._radius=r
        print('该圆的半径为:%d'%r)
    def Area(self):
        return self._radius**2*3.14
    def __str__(self):
        return '该圆面积为'+str(3.14*self._radius**2)
    def __gt__(self, other):
        return self._radius>other._radius
if __name__=='__main__':
    c1=Circle()
    c2=Circle()
    c1.SetRadius(5)
    c2.SetRadius(3)
    print(c1)
    print(c2)
    print(c1>c2)
    del c1
    del c2
该圆的半径为:5
该圆的半径为:3
该圆面积为78.5
该圆面积为28.26
True
该数据已经被清空
该数据已经被清空
  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include "iostream" #include "math.h" using namespace std; class coord{ private: double coord_x; double coord_y; public: coord() { coord_x = coord_y = 0; cout << "调用构造函数coord()" << endl; system("pause"); } ~coord() { cout << "调用析构函数~coord()" << endl; system("pause"); } void set_coord(double x,double y) { coord_x = x; coord_y = y; } double distance(coord &a,coord &b) { return sqrt((a.coord_x-b.coord_x)*(a.coord_x-b.coord_x)+(a.coord_y-b.coord_y)*(a.coord_y-b.coord_y)); } }; class circle{ private: double r; double c_x; double c_y; public: circle() { r=c_x=c_y=0; cout << "调用构造函数circle()" << endl; system("pause"); } ~circle() { cout << "调用析构函数~circle()" << endl; system("pause"); } void set_circle(double x,double y,double R) { if(r<=0) { cout << "error" << endl; exit(0); } r=R; c_x=x; c_y=y; } void if_intersect(circle& c1 , circle& c2) { double dist_1, dist_2; coord o1,o2; o1.set_coord(c1.c_x,c1.c_y); o2.set_coord(c2.c_x,c2.c_y); dist_1 = distance(o1,o2); dist_2 = c1.r + c2.r; if(dist_1<dist_2) cout << "相交" <<endl; else if(dist_1&gt;dist_2) cout << "不相交" <<endl; else cout << "相切" <<endl; } }; int main() { circle c1,c2; double x1,y1,r1,x2,y2,r2; cout << "input the coord and radius of circle1(double) (x,y,r):"; cin &gt;&gt; x1 &gt;&gt; y1 &gt;&gt; r1; c1.set_circle(x1,y1,r1); cout << "input the coord and radius of circle2(double) (x,y,r):"; cin &gt;&gt; x2 &gt;&gt; y2 &gt;&gt; r2; c2.set_circle(x2,y2,r2); if_intersect(c1,c2); return 0; }
05-12
实验内容: 一、定义一个名为Circle的圆,位于shape包中。要求如下: (1) 该类需要描述圆在二维坐标系中的位置,圆心的横、纵坐标值均取整数;描述圆的半径,取双精度型。 (2) 无参构造方法将圆心坐标设为坐标圆点,半径设为1.0;有参构造方法需要根据参数值设置圆心坐标和半径值。 (3) 提供一个计算圆面积方法double getArea()。 (4) 提供一个判断当前圆对象与参数圆对象是否相交的方法(相交方式见下图): boolean isIntersected(Circle another) (5) 提供一个判断当前圆对象是否包含参数圆对象的方法(包含方式见下图): boolean contains(Circle another) (6) 提供一个判断当前圆对象与参数圆对象的大小关系的方法(判断依据是半径半径之差的绝对值小于0.0001认为相等;返回负数表示当前圆对象小,0表示相等,正数表示当前圆对象) int compareTo(Circle another) (7) 提供返回当前圆的字符串的方法,格式为:[Circle(x,y)-raduis] String toString() 二、定义一个包含main方法TestCircle,位于shape包中。要求如下: (1) main方法中调用以下方法。 (2) 编写静态方法createArray,生成位置和大小均随机的10个圆对象,置于数组中。 (3) 编写静态方法sort对这10个圆对象进行升序排序,并输出。 (4) 编写静态方法computeArea计算这10个圆中互不相交和包含的圆的面积之和。 提交要求:打包为可以执行的JAR文档,其中要包含源程序文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值