2012-02-14 22 views
-1

私はLiangの本から1つの問題を解決しようとしていますが、私はそのほとんどを行っていますが、x座標とy座標を持つ部分を理解していません。私は2つのクラスTestRegularPolygonレギュラーポリゴン。このエリアの公式は現在正しくはありませんが、後でそれを扱います。私はコードをコンパイルして実行している日食を使用している誰も私に感謝する方法を私にいくつかのアイデアを与えることができます! n辺正多角形では、(n辺正多角形形状)は、同じ長さを有し、すべての角度(すなわち、多角形は等辺及び等角両方 ある)同じ程度を有する 全ての側面はx、y座標の難易度

  • デフォルト値でポリゴン の辺の数を定義するn個3.
  • 格納側という名前のプライベートダブル・データ・フィールドという名前のプライベートint型のデータフィールドを: が含まれていることをするRegularPolygonという名前のクラスを設計します デフォルト値を有する辺の長さ1
  • デフォルト値0
  • プライベートダブル・データ・フィールド定義名前Yとポリゴンの中心 のx座標定義プライベートダブル・データ・フィールドの名前X y座標
  • デフォルト値を持つ規則的なポリゴンを作成する引数なしのコンストラクタです。デフォルトの値を持つポリゴンの中心の の値です。
  • 指定された辺の数が で、辺の長さが(0、0)を中心とする正多角形を作成するコンストラクタです。
  • 指定された辺数、 辺の長さ、およびxおよびy座標を持つ正多角形を作成するコンストラクタです。
  • すべてのデータフィールドのアクセサメソッドとミューテータメソッド。
  • ポリゴンの周囲を返すメソッドgetPerimeter()。
  • ポリゴンの領域を返すgetArea()メソッドです。 の式は、正多角形の面積を計算します。

このクラスのUML図を描画します。クラスを実装します。引数なしコンストラクタを使用して作成された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

、このタスク私がやろうとしていますが、そう尋ねる人がいない... – Kiril

+0

'するRegularPolygon(int型nは、ダブルサイド、ダブルのx、ダブルy)は'私はすでにと思いますxyコンストラクタとしての資格があります。 –

+0

私はその質問を理解していません。 'x'と'y'座標はポリゴンの中心を指定します。それ以上はありません。 –

答えて

2
​​

は、なぜあなたはそれをカウントするため、xとyが必要なのですか?

私は周りを管理できますか?

x、yについては、問題はここでは心理的です。あなたは設定と取得のためのツールを用意していますが、実際には使用しません。あなたのクラスであるイメージを後でポリゴンを描画するために使用します。次に、これらのx、yが必要になります。全然

+0

xとyは座標です。 – Kiril

+0

私は見ているとおり、あなたはそれらをOKにしています。何が問題ですか? (私はあなたのためにクローズアップされていないupvoteを入れた) – Gangnus

+0

私はxとyのcoresianteのrepesentationがどのように動作するのかわからない – Kiril

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

} 
関連する問題