第九章第九题(几何:正多边形)(Geometry: regular polygons)

本文介绍如何设计一个名为 RegularPolygon 的类,用于表示等边等角的正多边形,并提供了构造方法、数据访问/修改、周长和面积计算。通过实例演示了如何创建并展示不同参数的正多边形对象的属性和计算结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第九章第九题(几何:正多边形)(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 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值