我正在尝试解决Liang的书中我已经完成了大部分工作的一个问题,但是我不理解带有x和y坐标的部分。我有两个类TestRegularPolygon,它是RegularPolygon的驱动程序类。该区域的公式目前不正确我将在稍后处理。我正在使用eclipse代码正在编译和运行,如果有人能给我一些想法怎么做我会很感激!
(几何:n边正多边形)在n边正多边形的所有边 具有相同的长度并且所有角度具有相同的度数(即,多边形是 等边和等边)。设计一个名为RegularPolygon的类 包含:
绘制类的UML图。实现类。写一个测试程序 创建三个使用no-arg构造函数创建的RegularPolygon对象, 使用RegularPolygon(6,4),并使用RegularPolygon(10,4,5.6, 7.8)。对于每个对象,显示其周长和区域。
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());
}
}
答案 0 :(得分:2)
Area=n*side*side/4.0*cot(Pi/n);
为什么需要x和y来计算它?
我希望,你可以管理周边?
至于x,y,这里的问题只是心理上的问题。您已经为他们的设置和获取准备了工具,但您并没有真正使用它们。将它想象成你的类将在以后用于...绘制多边形。然后你需要这些x,y。
答案 1 :(得分:0)
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);
}
}