简体   繁体   English

x , y 坐标的困难

[英]difficulty with x , y coordinate

I am trying to solve one problem from Liang's book I have done most of it but I do not understand the part with the x and y coordinates.I have two classes TestRegularPolygon which is the driver class for RegularPolygon.我正在尝试解决梁书中的一个问题,我已经完成了大部分,但我不明白 x 和 y 坐标的部分。我有两个类 TestRegularPolygon,它是 RegularPolygon 的驱动程序类。 The formula for the area is not currently correct I will deal with it later.该区域的公式目前不正确,我稍后会处理。 I am using eclipse the code is compiling and running if anyone can give me some idea how to do it I would appreciate it!我正在使用 eclipse 代码正在编译和运行,如果有人能给我一些想法如何做到这一点,我将不胜感激!

(Geometry: n-sided regular polygon) In an n-sided regular polygon all sides have the same length and all angles have the same degree (ie, the polygon is both equilateral and equiangular). (几何:n 边正多边形) 在 n 边正多边形中,所有边都具有相同的长度并且所有角的度数都相同(即,多边形既是等边又是等角的)。 Design a class named RegularPolygon that contains:设计一个名为 RegularPolygon 的类,其中包含:

  • A private int data field named n that defines the number of sides in the polygon with default value 3.一个名为 n 的私有 int 数据字段,用于定义多边形中的边数,默认值为 3。
  • A private double data field named side that stores the length of the side with default value 1.一个名为 side 的私有双数据字段,用于存储边的长度,默认值为 1。
  • A private double data field named x that defines the x-coordinate of the center of the polygon with default value 0.一个名为 x 的私有双精度数据字段,用于定义多边形中心的 x 坐标,默认值为 0。
  • A private double data field named y that defines the y-coordinate of the center of the polygon with default value 0.一个名为 y 的私有双精度数据字段,用于定义多边形中心的 y 坐标,默认值为 0。
  • A no-arg constructor that creates a regular polygon with default values.使用默认值创建正多边形的无参数构造函数。
  • A constructor that creates a regular polygon with the specified number of sides and length of side, centered at (0, 0).创建具有指定边数和边长的正多边形的构造函数,以 (0, 0) 为中心。
  • A constructor that creates a regular polygon with the specified number of sides, length of side, and x-and y-coordinates.创建具有指定边数、边长以及 x 和 y 坐标的正多边形的构造函数。
  • The accessor and mutator methods for all data fields.所有数据字段的访问器和修改器方法。
  • The method getPerimeter() that returns the perimeter of the polygon.方法 getPerimeter() 返回多边形的周长。
  • The method getArea() that returns the area of the polygon.返回多边形面积的方法 getArea()。 The formula for computing the area of a regular polygon is计算正多边形面积的公式是

Draw the UML diagram for the class.为该类绘制 UML 图。 Implement the class.实现类。 Write a test program that creates three RegularPolygon objects, created using the no-arg constructor, using RegularPolygon(6, 4), and using RegularPolygon(10, 4, 5.6, 7.8).编写一个测试程序来创建三个 RegularPolygon 对象,这些对象是使用无参数构造函数、使用 RegularPolygon(6, 4) 和使用 RegularPolygon(10, 4, 5.6, 7.8) 创建的。 For each object, display its perimeter and area.对于每个对象,显示其周长和面积。

public class RegularPolygon 
{
    private int n; //number of sides of the polygon
    private double side; //store the length of the side
    private double x; // x coordinate
    private double y; //y coordinate

    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;
    }

    public void setN(int then)
    {
        n = then;
    }

    public int getN()
    {
        return n;
    }

    public void setSide(double theside)
    {
        side = theside;
    }

    public double getSide()
    {
        return side;
    }

    public void setX(int thex)
    {
        x = thex;
    }

    public double getX()
    {
        return x;
    }

    public void setY(int they)
    {
        y = they;
    }

    public double getY()
    {
        return y;
    }

    public double getPerimeter()
    {
        return n * side;
    }

    public double getArea()
    {
        return (n * side) * 5;
    }
}


public class TestRegularPolygon 
{
    public static void main(String[] args) 
    {
        RegularPolygon mypol = new RegularPolygon(6, 4);
        System.out.println("the area is: " + mypol.getArea() + " the perimeter is " + mypol.getPerimeter());

        RegularPolygon yourpol = new RegularPolygon(10, 4, 5.6, 7.8);
        System.out.println("the area is: " + yourpol.getArea() + " the perimeter is " + yourpol.getPerimeter());
    }
}
Area=n*side*side/4.0*cot(Pi/n);

Why do you need x and y for counting it?为什么需要 x 和 y 来计算它?

I hope, you can manage the perimeter?我希望,你可以管理周边?

As for x, y, the problem is only psychological here.至于x,y,这里只是心理问题。 You have prepared the tools for their setting and getting, but you do not really use them.您已经为它们的设置和获取准备了工具,但您并没有真正使用它们。 Image it as is your class will be used later for... drawing the polygon.将其按原样成像,您的课程稍后将用于...绘制多边形。 Then you'll need these x,y.那么你将需要这些 x,y。

public class Exercise89A {

private int n; //number of sides of the polygon
private double side; //store the length of the side
private double x; // x coordinate
private double y; //y coordinate

public static void main(String[] args) {
    Exercise89A defaultpol = new Exercise89A();
    System.out.println("the area is: " + defaultpol.getArea() + " the perimeter is " + defaultpol.getPerimeter());

    Exercise89A mypol = new Exercise89A(6, 4);
    System.out.println("the area is: " + mypol.getArea() + " the perimeter is " + mypol.getPerimeter());

    Exercise89A yourpol = new Exercise89A(10, 4, 5.6, 7.8);
    System.out.println("the area is: " + yourpol.getArea() + " the perimeter is " + yourpol.getPerimeter());
}

Exercise89A() {
    n = 3;
    side = 1;
    x = 0; 
    y = 0;
}

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

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

public void setN(int newn){
    n = newn;
}

public int getN(){
    return n;
}

public void setSide(double newside){
    side = newside;
}

public double getSide(){
    return side;
}

public void setX(int newx){
    x = newx;
}

public double getX(){
    return x;
}

public void setY(int newy){
    y = newy;
}

public double getY(){
    return y;
}

public double getPerimeter(){
    return n * side;
}

public double getArea(){
    double s2 = side * side;
    double pin = Math.PI/n;
    double tangent = Math.tan(pin);
    return (n*s2)/(4*tangent);
}    

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM