第九章第九題(幾何:正多邊形)(Geometry: regular polygons)
第九章第九題(幾何:正多邊形)(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 main(String[] args) {
RegularPolygon regularPolygon1 = new RegularPolygon();
RegularPolygon regularPolygon2 = new RegularPolygon(6,4);
RegularPolygon regularPolygon3 = new RegularPolygon(10,4,5.6,7.8);
System.out.println("regularPolygon1's perimeter is " + regularPolygon1.getPerimeter() + " and regularPolygon1's area is " + regularPolygon1.getArea());
System.out.println("regularPolygon2's perimeter is " + regularPolygon2.getPerimeter() + " and regularPolygon2's area is " + regularPolygon2.getArea());
System.out.println("regularPolygon3's perimeter is " + regularPolygon3.getPerimeter() + " and regularPolygon3's area is " + regularPolygon3.getArea());
}
}
class RegularPolygon{
private int n;
private double side;
private double x;
private double y;
RegularPolygon(){
n = 3;
side = 1;
x = 0;
y = 0;
}
RegularPolygon(int newN,double newSide){
this.n = newN;
this.side = newSide;
this.x = 0;
this.y = 0;
}
RegularPolygon(int newN,double newSide,double newX,double newY){
this.n = newN;
this.side = newSide;
this.x = newX;
this.y = newY;
}
public int getN(){
return n;
}
public void setN(int newN){
n = newN;
}
public double getSide(){
return side;
}
public void setSide(double newSide){
side= newSide;
}
public double getX(){
return x;
}
public void setX(double newX){
x = newX;
}
public double getY(){
return y;
}
public void setY(double newY){
y = newY;
}
public double getPerimeter(){
return n * side;
}
public double getArea(){
return n * side * side / (4 * Math.tan(Math.PI / n));
}
}
- 結果顯示:
regularPolygon1's perimeter is 3.0 and regularPolygon1's area is 0.43301270189221946
regularPolygon2's perimeter is 24.0 and regularPolygon2's area is 41.569219381653056
regularPolygon3's perimeter is 40.0 and regularPolygon3's area is 123.10734148701015
Process finished with exit code 0
相關文章
- 第九章第十二題(幾何:交點)(Geometry: Intersections)
- 【計算幾何】多邊形交集
- 【計算幾何】多邊形點集排序排序
- 【計算幾何】點在多邊形內部
- 不可不知的WPF幾何圖形(Geometry)
- WPF繪圖(一):幾何(Geometry)與形狀(Shape)繪圖
- WPF 反射載入Geometry幾何圖形資料圖示反射
- HDU 6055 Regular polygon(幾何)Go
- 【計算幾何】點定位(線段,三角形,多邊形)
- [幾何]計算不規則多邊形的面積、中心、重心
- matlab繪製正多邊形Matlab
- 第九章 正規表示式
- Facebook 面試題 | 凸多邊形面試題
- 【D3.js學習總結】22、D3幾何-泰森多邊形JS
- three.js基礎之幾何體Curve、GeometryJS
- 單一div的正多邊形變換(純CSS)CSS
- CAD繪圖工具中的正多邊形命令繪圖
- POJ 1408-Fishnet(計算幾何-根據交點求多邊形面積)
- 第九章 介面
- opencv多邊形逼近OpenCV
- SVG <polygon> 多邊形SVGGo
- 多邊形填充-活動邊表法
- 第三章第二十五題(幾何:交點)(Geometry: intersecting point)
- 第三章第二十九題(幾何:兩個圓)(Geometry: two circles)
- 第九章、物件導向物件
- [CSS LEARN]Border與多邊形CSS
- 【JAVA】多邊形重心計算Java
- 正規表示式(regular expression)Express
- CSS繪製各種幾何圖形形狀效果CSS
- CSS繪製各種幾何形狀CSS
- WebGL不同幾何圖形的渲染方式Web
- 【第九章】檔案包含漏洞
- Flask教程第九章:分頁Flask
- 第九章專案程式碼
- 達芬奇密碼 第九章密碼
- Multiple View Geometry(多檢視幾何)學習筆記(9)—無窮遠平面&絕對二次曲線View筆記
- 微信小程式-測試遊戲生成六邊多邊形微信小程式遊戲
- Oracle正規表示式(regular expression)OracleExpress