2012-02-14 144 views
-1

我想從梁的書中解決一個問題我已經完成了大部分工作,但我不明白帶有x和y座標的部分。我有兩個類TestRegularPolygon,它是找到RegularPolygon。該地區的公式目前不正確,我將在稍後處理。我正在使用eclipse的代碼正在編譯和運行,如果任何人都可以給我一些想法如何做到這一點,我將不勝感激!x,y座標難度

(幾何結構:n邊規則多邊形)在一個n邊規則多邊形所有側面 具有相同的長度和所有角度具有相同的程度(即,該多邊形是 兩個等邊和等角的)。設計一個名爲RegularPolygon類是 包含:

  • 一個名爲N的私人int數據字段的默認值定義多邊形 邊數3
  • 私人雙數據字段命名一側存儲側的長度與 默認值1
  • 私有雙倍數據字段命名爲x定義多邊形的中心與 默認值0。
  • 私有雙倍數據字段命名ÿ定義的x座標y座標默認值爲0的多邊形的中心 。
  • 創建具有默認值的正多邊形的無參數構造函數。
  • 一個構造函數,它創建一個正方形多邊形,其邊長爲 ,長度爲(0,0)。
  • 構造函數,用於創建具有指定邊數的正多邊形,邊長爲 ,以及x和y座標。
  • 所有數據字段的訪問器和增變器方法。
  • getPerimeter()方法返回多邊形的周長。
  • getArea()方法返回多邊形的面積。計算正多邊形面積的公式爲 爲

繪製該類的UML圖。實施課程。編寫一個測試程序 ,該程序創建使用Regular參數(6,4)和使用RegularPolygon(10,4,5.6, 7.8)使用no-arg構造函數創建的三個RegularPolygon對象。對於每個對象,顯示其周長和麪積。

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

根本不是,這個我想這樣做,但有沒有人問這樣... – Kiril 2012-02-14 21:22:42

+0

找到RegularPolygon'(INT N,雙面,雙X,雙Y)任務'我認爲已經具有xy構造函數的資格。 – 2012-02-14 21:23:43

+0

我不明白這個問題。 'x'和'y'座標指定了多邊形的中心,沒有更多。 – 2012-02-14 21:24:47

回答

2
​​

爲什麼你需要計數它x和y?

我希望你可以管理周界?

至於x,y,問題只是心理而已。你已經爲他們的設置和獲取準備了工具,但你並沒有真正使用它們。像以前一樣將圖像用於繪製多邊形。那麼你需要這些x,y。

+0

x和y代表座標 – Kiril 2012-02-14 21:34:52

+0

如你所見,你擁有它們。問題是什麼? (我給你upvote因爲不被關閉) – Gangnus 2012-02-14 21:37:29

+0

我不知道x和y coordiante的代表如何工作 – Kiril 2012-02-14 21:39:10

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

}