第九章第九題(幾何:正多邊形)(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)與形狀(Shape)繪圖
- 不可不知的WPF幾何圖形(Geometry)
- HDU 6055 Regular polygon(幾何)Go
- WPF 反射載入Geometry幾何圖形資料圖示反射
- matlab繪製正多邊形Matlab
- [幾何]計算不規則多邊形的面積、中心、重心
- 第九章 正規表示式
- CAD繪圖工具中的正多邊形命令繪圖
- 第九章
- three.js基礎之幾何體Curve、GeometryJS
- 單一div的正多邊形變換(純CSS)CSS
- SVG <polygon> 多邊形SVGGo
- opencv多邊形逼近OpenCV
- 第三章第二十五題(幾何:交點)(Geometry: intersecting point)
- 第三章第二十九題(幾何:兩個圓)(Geometry: two circles)
- IDL建立泰森多邊形
- 多邊形填充-活動邊表法
- 第九章、物件導向物件
- [CSS LEARN]Border與多邊形CSS
- 【JAVA】多邊形重心計算Java
- 折線(Polyline)、多邊形(Polygon)Go
- CSS繪製各種幾何圖形形狀效果CSS
- Flask教程第九章:分頁Flask
- 【第九章】檔案包含漏洞
- CSS繪製各種幾何形狀CSS
- 《Python程式設計》第九章部分課後練習題Python程式設計
- Multiple View Geometry(多檢視幾何)學習筆記(9)—無窮遠平面&絕對二次曲線View筆記
- C primer plus 第六版 第九章 第九題 程式設計練習答案程式設計
- 第九章%第十章
- 分形、分形幾何、資料視覺化、Python繪圖視覺化Python繪圖
- C語言 第九章 檔案系統 重點 典型題C語言
- 第九章:輸入/輸出流與檔案操作 習題
- 異構幾何問題
- Pptx的形狀轉為WPF的Geometry
- 精通比特幣(第九章)【區塊鏈】比特幣區塊鏈
- CSAPP 第九章 虛擬記憶體APP記憶體
- Java程式設計思想 第九章 介面Java程式設計