2016-10-07 11 views
-2

私は、サークルが互いに重なっているかどうかをチェックするサークルクラスプログラムを作成しています。最初に与えられた6つの方法がある:getter methods for x,y, and radius and setter methods for x,y, and radius(6)。 2つの追加のメソッドがあります:getArea method(領域の値を返します)とdoesOverLap method(どのサークルが重なっているか重複していないかを返します)。そここのソースフォルダ内の多くのデモがありますが、以下は、バルク(言い訳インデント)である:メソッド付きサークルクラス

public class MyCircle 
{ 


    private double x; 
    private double y; 
    private double radius; 

    private double other_x; //keeps user input for other_x 
    private double other_y; //keeps user input for other_y 
    private double other_radius; //keeps user input for other_radius 

    private double third_x; //keeps user input for third_x 
    private double third_y; //keeps user input for third_y 
    private double third_radius; //keeps user input for third_radius 


    /* 
    The setX method stores a value in the x field. 
    @ param value The value to store in x. 
    */ 
    public void setX(double value) 
    { 
     x = value; 
    } 

    /* 
    The setY method stores a value in the y field. 
    @ param value The value to store in y. 
    */ 
    public void setY(double value) 
    { 
     y = value; 
    } 

    /* 
    The setRadius method stores a value in the radius field. 
    @param value The value to store in radius. 
    */ 
    public void setRadius(double value) 
    { 
     radius = value; 
    } 

     /* 
     The getX method returns x's value. 
     @return The value in the x field. 
     */ 
     public double getX() 
     { 
      return x; //returns value input for x 
     } 

     /* 
     The getY method return y's value. 
     @return The value in the y field. 
     */ 
     public double getY() 
     { 
      return y; //returns value input for y 
     } 

     /* 
     The getRadius method returns radius's value. 
     @return The value in the radius field. 
     */ 
     public double getRadius() 
     { 
      return radius; //returns value input for radius 
     } 

     /* 
     The getArea method returns the circle object's area. 
     @return The product of 3.14159 * radius * radius (area = 3.14159(pie) * radius * radius). 
     */ 
     public double getArea() 
     {  
      return radius * radius * 3.14159; 
     } 

     public boolean doesOverlap(MyCircleTest OtherCircleTest) 
     { 
      double distance; 

      distance = Math.sqrt(Math.pow(other_x - x, 2) + Math.pow(other_y - y, 2)); 
      return distance < other_radius + radius; 


      if(distance < other_radius + radius) 
      { 
       System.out.println("My other circle overlaps my circle"); 
      } 
      else 
      { 
       System.out.println("My other circle does not overlap my circle"); 
      } 
     } 



      /* 
      The setOtherX method stores a value in the x field. 
      @ param value The value to store in x. 
      */ 
      public void setOtherX(double value) 
      { 
       other_x = value; 
      } 

      /* 
      The setOtherY method stores a value in the y field. 
      @ param value The value to store in y. 
      */ 
      public void setOtherY(double value) 
      { 
       other_y = value; 
      } 

      /* 
      The setOtherRadius method stores a value in the radius field. 
      @param value The value to store in radius. 
      */ 
      public void setOtherRadius(double value) 
      { 
       other_radius = value; 
      } 

      /* 
      The getOtherX method returns x's value. 
      @return The value in the x field. 
      */ 
      public double getOtherX() 
      { 
       return other_x; //returns value input for x 
      } 

      /* 
      The getY method return y's value. 
      @return The value in the y field. 
      */ 
      public double getOtherY() 
      { 
       return other_y; //returns value input for y 
      } 

      /* 
      The getRadius method returns radius's value. 
      @return The value in the radius field. 
      */ 
      public double getOtherRadius() 
      { 
       return other_radius; //returns value input for radius 
      } 

      /* 
      The getArea method returns the circle object's area. 
      @return The product of 3.14159 * radius * radius (area = 3.14159(pie) * radius * radius). 
      */ 
      public double getOtherArea() 
      {  
       return other_radius * other_radius * 3.14159; 
      } 

      public boolean doesOverlap(MyCircleTest OtherCircleTest) 
      { 

       //Equation to see whether circles overlap 

       double distance_2; 

       distance_2 = Math.sqrt(Math.pow(other_x - third_x, 2) + Math.pow(other_y - third_y, 2)); 
       return distance < other_radius + third_radius; 


       if(distance_2 < other_radius + third_radius) 
       { 
        System.out.println("My other circle overlaps my third circle"); 
       } 
       else 
       { 
        System.out.println("My other circle does not overlap third circle"); 
       } 

      } 

       public void setThirdX(double value) 
       { 
        third_x = value; 
       } 

       /* 
       The setThirdY method stores a value in the third y field. 
       @ param value The value to store in third y. 
       */ 

       public void setThirdY(double value) 
       { 
        third_y = value; 
       } 

       /* 
       The setThirdRadius method stores a value in the third radius field. 
       @param value The value to store in third radius. 
       */ 

       public void setThirdRadius(double value) 
       { 
        third_radius = value; 
       } 

        /* 
        The getThirdX method returns third x's value. 
        @return The value in the third x field. 
        */ 
        public double getThirdX() 
        { 
         return third_x; //returns value input for third x 
        } 

        /* 
        The getY method return third y's value. 
        @return The value in the third y field. 
        */ 

        public double getThirdY() 
        { 
         return third_y; //returns value input for third y 
        } 

        /* 
        The getThirdRadius method returns third radius's value. 
        @return The value in the third radius field. 
        */ 

        public double getThirdRadius() 
        { 
         return third_radius; //returns value input for third radius 
        } 

        /* 
        The getArea method returns the circle object's area. 
        @return The product of 3.14159 * radius * radius (area = 3.14159(pie) * radius * radius). 
        */ 
        public double getThirdArea() 
        {  
         return third_radius * third_radius * 3.14159; 
        } 


        public boolean doesOverlap (MyCircleTest ThirdCircleTest) 
        { 

         //Equation to see whether circles overlap 

         double distance_3; 

         distance_3 = Math.sqrt(Math.pow(third_x - x, 2) + Math.pow(third_y - y, 2)); 
         return distance_3 < third_radius + radius; 


         if(distance_3 < third_radius + radius) 
         { 
          System.out.println("My third circle overlaps circle"); 
         } 
         else 
         { 
          System.out.println("My third circle does not overlap circle"); 
         } 

        } 
} 

私はプログラムの全体的な結果を述べるべきで、最終的な方法、doesOverLap方法、上の問題を抱えています。 2つの円が重なっているかどうかを示す必要があります。また、私は自分のコードではない各円の面積値を表示することになっています。

要件は次のとおりです。 サークルオブジェクトが3つあります。そのうち2つは重複する必要があり、2つは削除しないでください。 3つの円の領域が表示されます。 doesOverLapメソッドは、どのサークルがラップを超えているか、どのサークルはラップしていないかを示す必要があります。どんな助けでも大歓迎です。

ヒント:半径 'の合計が中心間の距離より大きい場合、2つの円が重複します。

+4

あなたのコードはオブジェクト指向ではありません。あなたの3つのサークルは同じクラスのオブジェクトでなければなりません。あなたのコードでは、サークルクラスはすべてのサークルの位置を保存します。さらに、いくつかのdoesOverlapメソッドがありますが、そうではありません。 –

答えて

1

まず、@JF Meierがコメントで指摘したように、これはOOのアプローチではありません。 Circleクラスには、1つのサークルにのみ関連するデータとロジックが含まれている必要があります。次に、Circleクラスの複数のインスタンスを作成して作業する必要があります。
また、私はこのコードテンプレートがあなた自身で提供されたか作成されたかわかりませんが、最初のデータはセッターだけでなくのコンストラクタに渡す必要があります。私の意見では、円は不変であるべきであるので、意味があります。例えば

:今、これらの方法のそれぞれ単独で1円オブジェクトに「属している」ので、これにより

public class Circle { 
    private final double x; 
    private final double y; 
    private final double radius; 

    public Circle(double x, double y, double radius) { 
     this.x = x; 
     this.y = y; 
     this.radius = radius; 
    } 
} 

は、あなただけの1つのgetArea()の変形やdoesOverlap(Circle other)を必要としています。

これで、複数の円の新しいインスタンスを作成し、それらのメソッドを使用することができます。

ご連絡先:doesOverlap(Circle other)のような方法のいずれかにパラメータがある場合は、実際にotherにしてください。

関連する問題