学以致用——Java源码——使用多态输出平面及立体几何图形的面积和体积(Project: Shape Hierarchy)
程序功能:
使用继承和多态的面向编程思想,动态的判断几何形状,打印平面图形面积及立体几何图形的面积和体积。
这个习题让我从无到有创建了共10个类才完成,虽然简单,但是作为继承和多态的入门练习还是不错的!
测试结果:
逐个处理: circle's area:201.06 square's area:64.00 triangle's area:24.00 sphere's area:804.25 sphere's volume:2144.66 cube's area:384.00 cube's volume:512.00 tetrahedron's area:110.85 tetrahedron's volume:60.34 多态处理: 图形 0是一个圆 面积:201.06 图形 1是一个正方形 面积:64.00 图形 2是一个三角形 面积:24.00 图形 3是一个球 面积:804.25 体积:2144.66 图形 4是一个立方体 面积:384.00 体积:512.00 图形 5是一个圆 面积:110.85 体积:60.34 |
代码:
1. 测试类
//Java How to Program, Exercise 10.13: Project: Shape Hierarchy
//by pandenghuang@163.com
/*** 10.13 (Project: Shape Hierarchy) Implement the Shape hierarchy shown in Fig.* 9.3. Each Two-DimensionalShape should contain method getArea to calculate the* area of the two-dimensional shape. Each ThreeDimensionalShape should have* methods getArea and getVolume to calculate the surface area and volume,* respectively, of the three-dimensional shape. Create a program that uses an* array of Shape references to objects of each concrete class in the hierarchy.* The program should print a text description of the object to which each array* element refers. Also, in the loop that processes all the shapes in the array,* determine whether each shape is a TwoDimensionalShape or a* ThreeDimensionalShape. If it’s a TwoDimensionalShape, display its area. If* it’s a ThreeDimensionalShape, display its area and volume.* * @author Pandenghuang@163.com* @Date Jan 7, 2019, 9:13:12 AM**/
public class ShapeHierarchyTest {public static void main(String[] args) {Circle circle = new Circle(8);Square square = new Square(8);Triangle triangle = new Triangle (6,8,10);Sphere sphere = new Sphere(8);Cube cube = new Cube(8);Tetrahedron tetrahedron = new Tetrahedron(8);System.out.printf("逐个处理:%n%n");System.out.printf("circle's area:%.2f%n", circle.getArea());System.out.printf("square's area:%.2f%n", square.getArea());System.out.printf("triangle's area:%.2f%n", triangle.getArea());System.out.printf("sphere's area:%.2f%n", sphere.getArea());System.out.printf("sphere's volume:%.2f%n", sphere.getVolume());System.out.printf("cube's area:%.2f%n", cube.getArea());System.out.printf("cube's volume:%.2f%n", cube.getVolume());System.out.printf("tetrahedron's area:%.2f%n", tetrahedron.getArea());System.out.printf("tetrahedron's volume:%.2f%n", tetrahedron.getVolume());Shape[] shapes = new Shape[6];shapes[0] = circle;shapes[1] = square;shapes[2] = triangle;shapes[3] = sphere;shapes[4] = cube;shapes[5] = tetrahedron;System.out.printf("%n%n多态处理:%n%n");// get type name of each object in employees arrayfor (int j = 0; j < shapes.length; j++) {String[] classInfo =shapes[j].getClass().getName().split("\\.");String className = classInfo[classInfo.length-1];String classNameCN = "圆";switch (className) {case "Circle":classNameCN = "圆";break;case "Square":classNameCN = "正方形";break;case "Triangle":classNameCN = "三角形";break;case "Sphere":classNameCN = "球";break;case "Cube":classNameCN = "立方体";break;case "Terahedron":classNameCN = "正四面体";break;}System.out.printf("图形 %d是一个%s%n", j, classNameCN); //打印对象的类型(类名)if (shapes[j] instanceof TwoDimensionalShape) System.out.printf("面积:%.2f%n%n", shapes[j].getArea()); //如果是二维图形,打印面积else if (shapes[j] instanceof ThreeDimensionalShape) {ThreeDimensionalShape threeDimensionalShape = (ThreeDimensionalShape)shapes[j]; //如果是三维图形,打印面积和体积System.out.printf("面积:%.2f%n", threeDimensionalShape.getArea());System.out.printf("体积:%.2f%n%n", threeDimensionalShape.getVolume());}}}}
2. 实体类
1)Shape类(基类,图形)
/*** * @author Pandenghuang@163.com* @Date Jan 7, 2019, 1:44:02 AM**/
public abstract class Shape {public abstract double getArea();
}
2)TwoDimensionalShape类 (平面图形)
public abstract class TwoDimensionalShape extends Shape {}
3)ThreeDimensionalShape类 (立体图形)
public abstract class ThreeDimensionalShape extends Shape{public abstract double getVolume(); }
4)Circle类 (圆)
public class Circle extends TwoDimensionalShape {private double radius;Circle(double radius){this.radius = radius;}@Overridepublic double getArea() {return Math.PI*Math.pow(radius,2);}}
5)Square类 (正方形)
public class Square extends TwoDimensionalShape{private double sideLength;Square(double sideLength){this.sideLength = sideLength;}@Overridepublic double getArea() {return Math.pow(sideLength,2);}
}
6)Triangle类 (三角形)
public class Triangle extends TwoDimensionalShape {private double a;private double b;private double c;public Triangle (double a, double b, double c) {this.a = a;this.b = b;this.c = c;}@Overridepublic double getArea() {double p = (a + b + c)/2.0;return Math.sqrt(p*(p-a)*(p-b)*(p-c));}}
7) Sphere类 (球)
public class Sphere extends ThreeDimensionalShape {private double radius;Sphere(double radius){this.radius = radius;}@Overridepublic double getArea() {return 4*Math.PI*Math.pow(radius,2);}@Overridepublic double getVolume() {return 4.0/3.0*Math.PI*Math.pow(radius,3);}
}
8)Cube类 (立方体)
public class Cube extends ThreeDimensionalShape {private double length;Cube(double length){this.length = length;}@Overridepublic double getArea() {return 6*Math.pow(length,2);}@Overridepublic double getVolume() {return Math.pow(length,3);}
}
9)Tetrahedron类(正四面体)
public class Tetrahedron extends ThreeDimensionalShape {private double length;Tetrahedron(double length){this.length = length;}@Overridepublic double getArea() {return Math.sqrt(3)*Math.pow(length,2);}@Overridepublic double getVolume() {return Math.sqrt(2)/12*Math.pow(length,3);}
}
学以致用——Java源码——使用多态输出平面及立体几何图形的面积和体积(Project: Shape Hierarchy)相关推荐
- 学以致用——Java源码——抛双骰游戏图形界面版(GUI-Based Craps Game)
游戏简介: 1. 抛双骰游戏的Swing界面版(CLI命令行版本见:学以致用--Java源码--抛双骰儿游戏改进版(Craps Game Modification with wagering),htt ...
- 学以致用——Java源码——使用随机几何图形制作屏保程序(Screen Saver with Shapes Using the Java 2D API)
程序功能: 使用随机输出的几何图形作为屏保程序,用户可随时指定屏幕上要显示的图形元素的数量. 运行示例: 源码: 1. 实体类 import java.awt.Graphics; import jav ...
- 学以致用——Java源码——使用Graphics2D类draw方法绘制立方体(Drawing Cubes)
程序功能: 使用Graphics2D类draw方法绘制立方体 运行示例: 源码: 1. 实体类 import java.awt.Graphics2D; import java.awt.Polygon; ...
- 学以致用——Java源码——使用Graphics类drawRect方法绘制表格(Grid Using Method drawRect)
程序功能: 使用Graphics类drawRect方法绘制10*10表格. 运行结果: 源码: 1. 实体类 //Creating JFrame to display DrawPanel. impor ...
- 学以致用——Java源码——员工薪酬系统功能增强(Payroll System Modification)
程序功能: 1. 基类:Emplyee(普通员工)(姓名.生日.身份证号) 2. 直接子类:SalariedEmployee(固定工资员工),HourlyEmployee(小时工).Commissio ...
- 学以致用——Java源码——抛硬币(Coin Tossing)
十年一晃而过,十年前写的代码,依然可以帮助我前进! package exercises.ch6Methods;import java.util.*;//JHTP Exercise 6.29 (Coin ...
- 学以致用——Java源码——键盘事件演示程序(Keystroke Events Demo Program)
程序功能 捕捉用户在键盘上的按键,按键分为三种类型: 1. 操作键(Action Key)(箭头.Home.End.翻页键.功能键(F1-F12).INSERT键.PRINT SCREEN键.CAPS ...
- Java源码详解四:String源码分析--openjdk java 11源码
文章目录 注释 类的继承 数据的存储 构造函数 charAt函数 equals函数 hashCode函数 indexOf函数 intern函数 本系列是Java详解,专栏地址:Java源码分析 Str ...
- Java源码详解零:HashMap介绍
文章目录 Java详解(0):HashMap介绍,HashMap的迭代,HashMap的线程安全问题 HashMap介绍 HashMap的迭代 HashMap的线程安全问题 Java详解(0):Has ...
最新文章
- mac pro下安装gdb和delve调试器
- 富士通打印机调整位置_打印机为什么卡纸 打印机四种卡纸原因及解决办法【介绍】...
- 电子商务(电销)平台中用户模块(User)数据库设计明细
- python在txt中的替换数据清洗_数据清洗过程中常见的排序和去重操作
- 12 | 套路篇:CPU 性能优化的几个思路
- C++ vector的释放
- SQLite数据库---ListView控件之商品展示案例
- 【JZOJ3885】【长郡NOIP2014模拟10.22】搞笑的代码
- 路由交换基础——NAT(网络地址转换)
- OracleBulkCopy的批量数据导入
- Powershell - 获取OS版本信息和catpion信息
- 腾讯云服务器登录宝塔面板
- Android 杂记 - 存货盘点用的客户端
- gmail如何设置邮箱别名
- 颜色恒常性 传统算法(AWB)
- 【转贴】英语如此简单
- 跟主页劫持的浏览器再见了,被恶意劫持修改方法!
- 步进电机与伺服电机对比
- 7-7 六度空间 (30分) 【最短路径(Floyd)】
- Apple Http Live Stream
热门文章
- POJ_2031 Building a Space Station
- macos 共享屏幕_如何在macOS上仅使用Private Me卡共享某些联系方式
- oracle-19c超详细安装过程-windows11系统
- 第5章第21节:实现Widget对应的完整应用中的功能 [SwiftUI快速入门到实战]
- Photoshop文字特效——栅格层叠效果文字
- 吉大计算机在线作业一,吉大18春学期《计算机接口技术》在线作业一(参考答案)...
- Spark登录错误Unable to verify certificate和Certificate hostname verification failed
- 认识几种三坐标测量机的结构形式
- 计算机毕业设计java+ssm水果蔬菜销售系统(源码+系统+mysql数据库+Lw文档)
- 3D项目离线部署技术分享, 可视化 全景 三维建模 ThingJS