第九章第九題(幾何:正多邊形)(Geometry: regular polygons)

jxxxh發表於2020-10-30

第九章第九題(幾何:正多邊形)(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

相關文章