# Write a class encapsulating the concept of a circle: Help Needed

• March 19th, 2014, 10:48 PM
Knowledge_Feeds_The_Mind
Write a class encapsulating the concept of a circle: Help Needed
Prompt: Write a class encapsulating the concept of a circle, assuming a circle has the following attributes: a Point representing the center of the circle, and the radius of the circle, and integer. Include a constructor, the accessors and mutators, and methods toString and equals. Also include methods returning the perimeter ( 2 × 𝜋 × 𝑟 ) and area ( 𝜋 × 𝑟^2) of the circle. Write a client (application) class to test all the methods in your class.

I started out trying to thing how to do this and I mapped out a certain idea but do not know how to incorporate the point represent the center of the circle. I am not sure how to proceed further.. Please give me your wise guidance:

Code Java:

```  import java.awt.*; public class Circle {   public static void main(String[] args) {   final double PI = 3.14; int x,y, radius = 4; double area; double perimeter;   area = PI * radius * radius; perimeter = 2 * PI * radius;   // Output Results System.out.println("The area of the circle is " + area ); System.out.println("The perimeter of the circle is " + perimeter ); }   int x, y, radius; public Circle(int x, int y, int radius) { this.x = x; this.y = y; this.radius = radius; } }   // For some reason this code has problems. // I tried integrating it into the top code and // That failed, not sure why though.   class public Circle(int x, int y, int radius) { this.x = x; this.y = y; this.radius = radius; }   public Circle() { this.x = 3; this.y = 3; this.radius = 6; }```

I do not know what to do. I have viewed tutorials, but that makes it even more confusing.... I hope you can assist me.
Thank you so much for coping with a noob like me.
Thanks.
• March 19th, 2014, 11:09 PM
dicdic
Re: Write a class encapsulating the concept of a circle: Help Needed
Quote:

but do not know how to incorporate the point represent the center of the circle.
what do you mean by that?
do you need to know where exactly the center of circle located?
if that's the case, then, should your circle be in cartesian plane?
then your circle would have coordinates center(x, y)?

Quote:

// For some reason this code has problems.
// I tried integrating it into the top code and
// That failed, not sure why though.

class public Circle(int x, int y, int radius) {
this.x = x;
this.y = y;
}
well that is obviously a compilation error. that is invalid syntax
should it be public class ....?

So i think you must have another class that has public static void main
something like this:
Code java:

```public class Circle {   int diameter; public Circle(int diameter) { this.diameter = diameter }   // create some methods here }```

then test it with other class:
Code java:

```public class TestCircle {   public static void main(String[] args) { Circle circle = new Circle(20); // do some stuff here } }```

by the way, what is the purpose of having the variable x and y?
• March 19th, 2014, 11:43 PM
Knowledge_Feeds_The_Mind
Re: Write a class encapsulating the concept of a circle: Help Needed
I do not think I need to know exactly, put pretty close to it.
Yes. "A toString method that prints out the location of the circle (the x, y coordinates of

Code Java:

```  public class TestCircle {   public static void main(String[] args) {   final double PI = 3.14;   int x,y, radius = 4;   double area = 24;   double perimeter = 30;   area = PI * radius * radius; perimeter = 2 * PI * radius;   // Output Results System.out.println("The area of the circle is " + area ); System.out.println("The perimeter of the circle is " + perimeter ); } }   public class Circle {   int radius; int x,y; public Circle( int x, int y, int radius) { this.x = 2; this.y = 2; this.radius = 4;   } }```
• March 20th, 2014, 12:49 AM
dicdic
Re: Write a class encapsulating the concept of a circle: Help Needed
Okay I get it now. So the x and y are the coordinates.

Quote:

"A toString method that prints out the location of the circle (the x, y coordinates of
I think the requirement wants you to override the toString method of Object
If you are using netbeans or eclipse, just press Ctrl + spacebar and find the toString and click enter.
that will override the toString method and you must return what you want to print out with that Circle class.
If you are not using any IDE, just create a public method named toString that has return type of String

Quote:

public class TestCircle {

public static void main(String[] args) {

final double PI = 3.14;

double area = 24;

double perimeter = 30;

perimeter = 2 * PI * radius;

// Output Results
System.out.println("The area of the circle is " + area );
System.out.println("The perimeter of the circle is " + perimeter );
}
}

public class Circle {

int x,y;
public Circle( int x, int y, int radius) {
this.x = 2;
this.y = 2;

}
}
I guess you have compilation error with your code?
each java file can contain only one public class. except for nested public class.
it would be better if you put your Circle class in another java file.
• March 20th, 2014, 01:01 PM
Knowledge_Feeds_The_Mind
Re: Write a class encapsulating the concept of a circle: Help Needed
So I reworked the code and now I think I only have to find a way to integrate the second code into the first code

Code Java:

```  //First Code   import java.awt.Point;   public class NewClass {   public static void main(String[] args) {   final double PI = 3.14;   int x = 2; int y = 2; int center = 0; int radius = 4;   double area = 24;   double perimeter = 30;   area = PI * radius * radius; perimeter = 2 * PI * radius;     // Output Results System.out.println("The area of the circle is " + area ); System.out.println("The perimeter of the circle is " + perimeter ); System.out.println("The Circle Center is 0, Radius is 4, coordinates are (2,2)");   } }   // Second Code   class Point { int x; int y;   public Point (int x, int y) { this.x = x; this.y = y; } // Getter/Setters and other methods }   public class Circle {   Point center = null; int radius = 0;   public Circle(int x, int y, int rad) { center = new Point(x, y); radius = rad; } }```

I need help with these things:

A Circle constructor that takes a Point object and a radius value as parameters.

Accessors and mutators (getters and setters) for the Point object and the radius.

A toString method that prints out the location of the circle (the x, y coordinates of

An equals method that returns true if two circles have the same Point location

An application class that creates at least one instance of a Circle object.

Thanks
• March 20th, 2014, 07:26 PM
dicdic
Re: Write a class encapsulating the concept of a circle: Help Needed
Quote:

class Point {
int x;
int y;

public Point (int x, int y) {
this.x = x;
this.y = y;
}
// Getter/Setters and other methods
}
you already have created your own/customized Point class. So why do you have to import java.awt.Point?

Circle class:
You need to provide getters for Point and radius in your Circle class.
Quote:

A Circle constructor that takes a Point object and a radius value as parameters.
I think it would be better if the argument of your constructor would be:
since that what the requirement says.

Point class
And in your Point class, you need to override the toString method of Object.
just create a method public String toString() in Point class. then return the coordinates
For Example:
Code java:

```public String toString() { return "(" + x + ", " + y + ")"; }```
and after, try to create Object Point and print it.
For Example:
Code java:

```Point center = new Point(2,3); System.out.println(center);```
the output of the above code will be (2, 3)
• March 21st, 2014, 01:09 AM
Knowledge_Feeds_The_Mind
Re: Write a class encapsulating the concept of a circle: Help Needed
So I sat down and reran the entire thing.
I thought I had it but nope, did not happen :confused:

Code Java:

```public class NewCircle {   public static void main(int radius, int x, int y) {   final double PI = 3.14; x = 2; y = 2; final point center = null; //Says, "assigned value never used" radius = 4;   double area = 24; double perimeter = 30;   area = PI * radius * radius; perimeter = 2 * PI * radius;   System.out.println("Area of the circle is " + area ); System.out.println("Perimeter of the circle is " + perimeter );   class Point { int x; int y;   public Point (final int x, final int y) { this.x = x; this.y = y;   class Circle{ public String toString() {   center = new Point(2,2); //Cannot assign value to final variable center int radius = 4;   System.out.println(center); System.out.println("The Circle Center: 0, Radius: 4, coordinates: (2,2)"); return " (" + x +", " + y +")"; } } } } } }```
The system seems to contradict itself:
Whenever I assign "final" to center, the system states. "Local variable center is accessed from within inner class; needs to be declared final" and then when I reassign final to center it states, "Cannot assign a value to final variable center."
• March 21st, 2014, 01:17 AM
dicdic
Re: Write a class encapsulating the concept of a circle: Help Needed
Quote:

final point center = null;
you declared the class Point with uppercase P
java is a case sensitive programming language.
then it should be final Point center not final point center.

Quote:

center = new Point(2,2); //Cannot assign value to final variable center
because you made it final. just remove the final keyword,
• March 21st, 2014, 12:40 PM
Knowledge_Feeds_The_Mind
Re: Write a class encapsulating the concept of a circle: Help Needed
I finalized my code, but I cannot run it, the program says that there is no main class?
"public static void main(int radius, int x, int y) {" I thought that this was my main class....
Do you know what may be causing this?

Code Java:

```  import java.awt.Point;   public class Circle {   Point center = null; int radius = 4;   public String toString() { int x = 2; int y = 2; return "(" + x + ", " + y + ")"; }   public static void main(int radius, int x, int y) {   final double PI = 3.14; x = 2; y = 2; Point center = new Point(2,2); radius = 4;   double area = 24; double perimeter = 30;   area = PI * radius * radius; perimeter = 2 * PI * radius;   System.out.println("Area of the circle is " + area ); System.out.println("Perimeter of the circle is " + perimeter ); System.out.println(center); System.out.println("The Circle Center:(2,2), Radius: 4,");   class Point { int x; int y;   public Point (final int x, final int y) { this.x = x; this.y = y;   } } } }```
Many Thanks.
-Vex
• March 23rd, 2014, 09:47 PM
dicdic
Re: Write a class encapsulating the concept of a circle: Help Needed
Quote:

I finalized my code, but I cannot run it, the program says that there is no main class?
sorry for late reply. I don't do anything related to programming during weekends
the reason is you don't have main method in your class.
public static void main(String[] var) is a special method in java. you cannot modify its parameter (except for the variable name of String array)