第九章第九题(几何:正多边形)(Geometry: regular polygons)
-
**9.9(几何:正多边形)在一个正n边形中,所有边的长度都相同,且所有角的度数都相同(即这个多边形是等边等角的)。设计一个名为RegularPolygon的类,该类包括:
- 一个名为n的int类型私有数据域,定义多边形的边数,默认值为3.
- 一个名为side的double类型私有数据域,存储边的长度,默认值为1.
- 一个名为x的double类型私有数据域,定义多边形中点的x坐标,默认值为0.
- 一个 名为y的double类型私有数据域,定义多边形中点的y坐标,默认值为0.
- 一个创建具有默认值的正多边形的无参构造方法
- 一个能创建带指定边数和边长度、中心在(0,0)的正多边形的构造方法。
- 一个能创建带指定边数和边长度、中心在(x,y)的正多边形的构造方法。
- 所有数据域的访问器和修改器。
- 一个返回多边形周长的方法getPerimeter()
- 一个返回多边形面积的方法getArea()。计算正多边形面积的公式是:
画出该类的UML图并实现这个类。编写一个测试程序,分别使用无参构造方法、RegularPolygon(6,4)和RegularPolygon(10,2,5.6,7.8)创建三个RegularPolygon对象。显示每个对象的周长和面积。
**9.9(Geometry: regular polygons)In a regular n-polygon, all edges have the same length, and all angles have the same degree (that is, the polygon is equilateral and equiangular). Design a class named regularpolygon, which includes:- An int type private data field named n, which defines the number of sides of a polygon. The default value is 3
- A double type private data field named side. The length of the storage edge is 1. 0 by default
- A double type private data field named x defines the X coordinate of the midpoint of the polygon. The default value is 0
- A double type private data field named y, which defines the Y coordinate of the midpoint of a polygon. The default value is 0
- A method of constructing regular polygons with default values without parameters
- A construction method that can create a regular polygon with a specified number of edges and edge length, with the center at (0,0).
- A construction method that can create a regular polygon with a specified number of edges and edge length, with the center at (x, y).
- Accessors and modifiers for all data fields.
- Getperimeter() method to return the perimeter of a polygon
- A method getarea() that returns the area of a polygon. The formula for calculating the area of regular polygon is as follows:
Draw the UML diagram of the class and implement the class. Write a test program, and create three regular polygon objects using the nonparametric construction method, regularpolygon (6,4) and regularpolygon (10,2,5.6,7.8). Displays the perimeter and area of each object.
-
参考代码:
package chapter09;
public class Code_09 {
public static void