2 Replies - 7649 Views - Last Post: 14 October 2012 - 03:47 PM Rate Topic: -----

#1 james7346  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-September 12

Java Circle 2D

Posted 14 October 2012 - 02:37 PM

Hello all
I have this book assignment with java programming.

Book Instructions:

Define the Circle2D class that contains:
Two double data fields named x and y that specify the center of the circle with get methods.
A data field radius with a get method.
A no-arg constructor that creates a default circle with (0, 0) for (x, y) and 1 for radius.
A constructor that creates a circle with the specified x, y, and radius.
A method getArea() that returns the area of the circle.
A method getPerimeter() that returns the perimeter of the circle.
A method contains(double x, double y) that returns true if the specified point (x, y)is inside this circle. See Figure 10.14(a).
A method contains(Circle2D circle) that returns true if the specified circle is inside this circle. See Figure 10.14(B).
A method overlaps(Circle2D circle) that returns true if the specified circle overlaps with this circle. See the figure below.



I have made the class but I cant get it to run! need help. anyone that could help me. I would be grateful and thank you.


package Circle2D;

public class Circle2D {

public static void main(String[] args) {
//center point of the circle
private double X,Y;
//radius
private double rad;
private double c;
/*double dist=0.0;*/
public Circle2D()
{
X=0;
Y=0;
rad=1.0;
}
public Circle2D(double CenterX,double CenterY,double radius)
{
X = CenterX;
Y = CenterY;
rad = radius;
}
public double getRadius()
{
return rad;
}
public double getArea()
{
return Math.PI*rad*rad;
}
public double getPerimiter()
{
return 2*Math.PI*rad;
}
public boolean isContain(double x,double y)
{
double dist = Math.sqrt((x-X)*(x-X)-(y-Y)*(y-Y));
if(dist>rad) {
return false;
}
else {
return true;
}
}
public boolean isCircleContains(Circle2D circle, double dist)
{
dist = Math.sqrt((X-circle.X)*(X-circle.X)-(Y-c
if(dist+circle.rad<=rad) {
return true;
}
else {
return false;
}
}
public boolean isCircleOverlaps(Circle2D circle, double dist)
{
dist = Math.sqrt((X-circle.X)*(X-circle.X)-(Y-c
if(dist<(rad+circle.rad)) {
return true;
}
else {
return false;
}
}
}

I am running it in Netbeans IDE for anyone who wants to know.
Thank you again to anyone who could help me

Is This A Good Question/Topic? 0
  • +

Replies To: Java Circle 2D

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Java Circle 2D

Posted 14 October 2012 - 03:11 PM

Please post your code in code tags.

It appears that all constructors and methods are inside your main() method. It could be due to a misplaced or missing close brace, but it's impossible to tell. You should see quite a lot of complaints in Netbeans if what you posted is copied and pasted from there.
Was This Post Helpful? 0
  • +
  • -

#3 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 864
  • Posts: 2,652
  • Joined: 29-July 11

Re: Java Circle 2D

Posted 14 October 2012 - 03:47 PM

No access modifiers for variables in main() like that. You can create a class inside main(), if you like and then make them private

public static void main(String[] args) {
        class Circle2D {
            private int x, y;
            
            public Circle2D(int x, int y) {
                this.x = x;
                this.y = y;
            }
        }
        
        Circle2D c = new Circle2D(100,100);
    }



In any event, you don't want to normally declare local inner classes like in my example. Get out of main().

This also doesn't make sense in your code:

dist = Math.sqrt((X-circle.X)*(X-circle.X)-(Y-c


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1