Java黑皮书9.9

题目

(几何:正n边形)

在一个正n边形中,所有边的长度都相同,且所有角的度数都相同(即这个多边形是等边等角的)。设计一个名为RegularPolygon的类,该类包括:

• 一个名为n的int型私有数据域定义多边形的边数,默认值为3。

• 一个名为side的double型私有数据域存储边的长度,默认值为1。

• 一个名为×的double型私有数据域定义多边形中点的x坐标,默认值为0。

• 一个名为y的double型私有数据域定义多边形中点的y坐标,默认值为0。

• 一个创建带默认值的正多边形的无参构造方法。

• 一个能创建带指定边数和边长度、中心在(0, 0)的正多边形的构造方法。

• 一个能创建带指定边数和边长度、中心在(x, y)的正多边形的构造方法。

• 所有数据域的访问器和修改器。

• 一个返回多边形周长的方法getPerimeter()

• 一个返回多边形面积的方法getArea()。计算正多边形面积的公式是:

面积 = n ∗ s 2 4 ∗ t a n ( π n ) 面积=\frac{n * s^2}{4 * tan(\frac{\pi}{n})} 面积=4∗tan(nπ​)n∗s2​

画出该类的UML图并实现这个类。编写一个测试程序,分别使用无参构造方法、RegularPolygon(6, 4)和RegularPolygon(10, 4, 5.6, 7.8)创建三个RegularPolygon对象。显示每个对象的周长和面积。

(Geometry: regular n-sided)
In a regular n-sided polygon, all sides have the same length and all angles have the same degree (i.e. the polygon is equilateral). Design a class called RegularPolygon, which includes:

An int type private data field named n defines the number of sides of a polygon, with a default value of 3.

The length of a double type private data field storage edge named side, with a default value of 1.

A double type private data domain named x defines the x-coordinate of the midpoint of the polygon, with a default value of 0.

A double type private data domain named y defines the y-coordinate of the midpoint of the polygon, with a default value of 0.

A non parametric construction method for creating regular polygons with default values.

A construction method that can create regular polygons with a specified number of edges and edge length, centered at (0,0).

A construction method that can create regular polygons with a specified number of edges and edge length, centered on (x, y).

Access and modifier for all data domains.

A method called getPerimeter that returns the perimeter of a polygon

A method called getArea() that returns the area of a polygon. The formula for calculating the area of a regular polygon is:

Area=n * s 2 4 * t a n (π n) Area=\ frac {n * s ^ 2} {4 * tan (\ frac {\ pi} {n})} Area=4 * tan (n π) n * s2

Draw a UML diagram of the class and implement it. Write a test program that uses the parameter free construction method, RegularPolygon (6, 4), and RegularPolygon (10, 4, 5.6, 7.8) to create three RegularPolygon objects. Display the perimeter and area of each object.

代码

RegularPolygon.java

public 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 n, double side) {
        this.n = n;
        this.side = side;
        x = 0;
        y = 0;
    }

    RegularPolygon(int n, double side, double x, double y) {
        this.n = n;
        this.side = side;
        this.x = x;
        this.y = y;
    }

    int getN() {
        return n;
    }

    void setN(int newN) {
        this.n = newN;
    }

    double getSide() {
        return side;
    }

    void setSide(double newSide) {
        this.side = newSide;
    }

    double getX() {
        return x;
    }

    void setX(double newX) {
        this.x = newX;
    }

    double getY() {
        return y;
    }

    void setY(double newY) {
        this.y = newY;
    }

    double getPerimeter() {
        return side * n;
    }

    double getArea() {
        return (n * side * side) / (4 * Math.tan(Math.PI/n));
    }
}

Test.java

public class Test {
    public static void main(String[] args) {
        RegularPolygon test1 = new RegularPolygon();
        RegularPolygon test2 = new RegularPolygon(6, 4);
        RegularPolygon test3 = new RegularPolygon(10, 4, 5.6, 7.8);
        System.out.println("test1的周长是:" + test1.getPerimeter() + ",面积是" + test1.getArea());
        System.out.println("test1的周长是:" + test2.getPerimeter() + ",面积是" + test2.getArea());
        System.out.println("test1的周长是:" + test3.getPerimeter() + ",面积是" + test3.getArea());
    }
}

UML图

结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值