如下给出抽象图形类(Graph)和坐标点类Point。 1)设计其抽象子类二维图形(TwoDGraph),添加计算面积calArea()和周长calPerimeter()两个抽象方法; 2)从TwoDGraph类派生出圆形Circle,增加圆心centerPoint和半径radius属性,增加构造方法; 3)在Circle中重写calArea、calPerimeter和toString()方法 4)从TwoDGraph类派生出长方形Rectangle,增加左上角坐标leftPoint、长length和宽width属性,增加构造方法; 5)在Rectangle中重写calArea、calPerimeter和toString()方法 6)在Rectangle新增输出4个顶点坐标方法outputPoints() 在main方法中,测试Circle和Rectangle类中的方法。

时间: 2024-02-12 08:07:59 浏览: 25
好的,我明白了。以下是代码实现: ```java abstract class Graph { abstract void draw(); } class Point { int x; int y; public Point(int x, int y) { this.x = x; this.y = y; } } abstract class TwoDGraph extends Graph { abstract double calArea(); abstract double calPerimeter(); } class Circle extends TwoDGraph { Point centerPoint; double radius; public Circle(Point centerPoint, double radius) { this.centerPoint = centerPoint; this.radius = radius; } @Override void draw() { System.out.println("Drawing Circle..."); } @Override double calArea() { return Math.PI * radius * radius; } @Override double calPerimeter() { return 2 * Math.PI * radius; } @Override public String toString() { return "Circle with center at (" + centerPoint.x + ", " + centerPoint.y + ") and radius " + radius; } } class Rectangle extends TwoDGraph { Point leftPoint; int length; int width; public Rectangle(Point leftPoint, int length, int width) { this.leftPoint = leftPoint; this.length = length; this.width = width; } @Override void draw() { System.out.println("Drawing Rectangle..."); } @Override double calArea() { return length * width; } @Override double calPerimeter() { return 2 * (length + width); } @Override public String toString() { return "Rectangle with left point at (" + leftPoint.x + ", " + leftPoint.y + "), length " + length + ", and width " + width; } public void outputPoints() { Point rightPoint = new Point(leftPoint.x + length, leftPoint.y); Point bottomPoint = new Point(leftPoint.x, leftPoint.y + width); Point rightBottomPoint = new Point(rightPoint.x, bottomPoint.y); System.out.println("Top left point: (" + leftPoint.x + ", " + leftPoint.y + ")"); System.out.println("Top right point: (" + rightPoint.x + ", " + rightPoint.y + ")"); System.out.println("Bottom left point: (" + bottomPoint.x + ", " + bottomPoint.y + ")"); System.out.println("Bottom right point: (" + rightBottomPoint.x + ", " + rightBottomPoint.y + ")"); } } public class Main { public static void main(String[] args) { Circle c = new Circle(new Point(0, 0), 5); System.out.println(c); System.out.println("Area: " + c.calArea()); System.out.println("Perimeter: " + c.calPerimeter()); Rectangle r = new Rectangle(new Point(0, 0), 3, 4); System.out.println(r); System.out.println("Area: " + r.calArea()); System.out.println("Perimeter: " + r.calPerimeter()); r.outputPoints(); } } ``` 输出结果如下: ``` Circle with center at (0, 0) and radius 5.0 Area: 78.53981633974483 Perimeter: 31.41592653589793 Rectangle with left point at (0, 0), length 3, and width 4 Area: 12.0 Perimeter: 14.0 Top left point: (0, 0) Top right point: (3, 0) Bottom left point: (0, 4) Bottom right point: (3, 4) ```

相关推荐

最新推荐

recommend-type

Tensorflow中的图(tf.Graph)和会话(tf.Session)的实现

主要介绍了Tensorflow中的图(tf.Graph)和会话(tf.Session)的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

tensorflow通过模型文件,使用tensorboard查看其模型图Graph方式

所以我们可以简单的写几行Pyhton,加载Graph,只在logdir里,输出Graph结构数据,并可以查看其图结构。 执行下述代码,将数据流图保存为图片,在目录F:/tensorflow/graph下生成文件events.out.tfevents.1508420019....
recommend-type

janusgraph部署开发.docx

JanusGraph over HBase支持全局顶点和边缘迭代。但是,请注意,所有这些顶点和/或边都将加载到内存中,这可能会导致OutOfMemoryException。使用JanusGraph和TinkerPop的Hadoop-Gremlin有效地遍历大型图中的所有顶点...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB图像处理算法宝典:从理论到实战

![MATLAB图像处理算法宝典:从理论到实战](https://img-blog.csdnimg.cn/20200717112736401.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2d1emhhbzk5MDE=,size_16,color_FFFFFF,t_70) # 1. MATLAB图像处理基础理论 MATLAB图像处理是一种利用MATLAB编程语言进行图像处理的强大工具。它提供了丰富的函数和工具箱,用于图像获取、增强、分
recommend-type

matlab中1/x的非线性规划

在MATLAB中,可以使用非线性规划函数(`fmincon`)来优化一个包含1/x的非线性目标函数。下面是一个简单的例子: ```matlab % 定义目标函数 fun = @(x) 1/x; % 定义约束函数(这里没有约束) nonlcon = []; % 定义初始点 x0 = 1; % 定义优化选项 options = optimoptions('fmincon', 'Display', 'iter'); % 进行非线性规划 [x, fval] = fmincon(fun, x0, [], [], [], [], [], [], nonlcon, options); ``` 在
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

MATLAB数据可视化黑科技:洞悉数据背后的秘密

![MATLAB数据可视化黑科技:洞悉数据背后的秘密](https://img-blog.csdnimg.cn/img_convert/1a36558cefc0339f7836cca7680c0aef.png) # 1. MATLAB数据可视化的基本原理 MATLAB数据可视化是一种将数据转化为图形表示形式的技术,以帮助人们理解和分析数据。其基本原理包括: - **数据表示:**将原始数据转换为图形元素,如点、线和条形。 - **图形布局:**确定图形元素在绘图区域中的位置和大小。 - **图形美化:**添加标题、标签、颜色和纹理等元素,以增强图形的可读性和美观性。 - **交互性:**允