第九章第九题(几何:正多边形)(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

  • 10
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值