常规多边形代码中心

时间:2018-04-26 23:59:29

标签: java

我正在尝试编写一个遵循关于正多边形的某些条件并找到多边形的周长和面积的代码。我必须编写三个不同的类Point2D,RegularPolygon和Driver。我可以获得所有必要的标准,除了使用实例变量中心以使中心坐标为x和y之外。在RegularPolygon代码中有两个使用中心的东西,我无法使它们工作。我需要一个Point2D实例变量中心,它存储多边形中心的x和y坐标,Point2D对象的x和y分量的默认值为0。我还需要一个构造函数,它创建一个带有指定边数,边长的正多边形,以及一个表示多边形中心的Point2D对象。我还需要一个方法public String toString(),它返回包含此多边形的边数,边长和中心坐标的多边形的String表示。作为此方法的一部分,调用Point2Dcenter上的toString()方法。但是,当我在Point2D中调用toString方法时,它会出现错误。如果有人能帮我弄清楚为什么我不能让它在我的代码中工作,我会非常感激。

// Point2D的代码

 public class Point2D
 {
//Instance Variables
private int x;
private int y;

/**
 * Constructor for objects of class Point2D, this will initialize
 * the instance variables
 */
public Point2D(int xVariable, int yVariable)
{
    //Initializes Instance Variables
    x = xVariable;
    y = yVariable;
}

//Accessor Method for X variable
public int getX() {
    return x;
}

//Accessor Method for X variable
public int getY() {
    return y;
}

//ToString Method
public String toString() {
    return "<" + x + "," + y + ">";
}

//Method that tests if the values of x and y are equal
public boolean equal(Object o) {
    if (o instanceof Point2D) {
        Point2D c = (Point2D)o;
    }
    return false;
}

}

// RegularPolygon的代码

  public class RegularPolygon
  {
// instance variables
private int n; //Number of sides
private double side; //Length of sides
private double x; //Value of X-Coordinate
private double y; //Value of y-Coordinate
private double center; 

/**
 * No argument constuctor that creates a regular polygon with 
 * default values
 */
public RegularPolygon()
{
    //Intilializes Instance Variables
    n=3;
    side =1;
    x=0;
    y=0;

}

/** 
 * Constructor that creates a regular polygon with a specific number
 * of sides, length of side, and a Point2D object that represents the
 * center of the polygon.
 */
public RegularPolygon(int n, double side, double x, double y) {
    this.n = n;
    this.side= side;
    this.x = 0;
    this.y = 0;
}

//Mututator Methods
public void setN(int nValue) {
    n = nValue;
}
public void setSide(double sideValue) {
    side = sideValue;
}
public void setX(double xValue) {
    x = xValue;
}
public void setY(double yValue) {
    y = yValue;
}

//Accessor Methods
public int getN() {
    return n;
}
public double getSide() {
    return side;
}
public double getX() {
    return x;
}
public double getY() {
    return y;
}

//Calculates the Perimeter of the regular polygon
public double getPerimeter() {
    return n * side;
}

//Calculates the Area of the regular polygon
public double getArea() {
    double area = (n * (Math.pow(side,2))) / (4 * (Math.tan(Math.PI/n)));
    return area;
}

//To String Method
public String toString() {
    return "Number of sides: " + n + ", Side Length: " + side + ", Center Coordinates: "+Point2D.toString(x,y);
}

}

1 个答案:

答案 0 :(得分:0)

Point2D中的toString方法不会将变量作为参数,但您可以使用最后带参数的RegularPolygon调用它。但是你调用Point2D的方式使用它的toString方法就像静态方法一样,尽管它不是。所以你要么必须实例化Point2D,要么让Point2D的toString方法静态化。但问题是,你必须在Point2D中使你的x和y也是静态的。使toString()方法静态的另一个问题是你不能覆盖Object的toString方法。所以我建议您执行以下操作:

    public String toString() {
        return "Number of sides: " + n + ", Side Length: " + side + ", Center Coordinates: "+new Point2D((int)x, (int)y).toString();
    }