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, an integer. Include a constructor, the accessors and mutators, and methods toString and equals. Also include methods returning the perimeter (2*PI*radius) and area (PI*radius^2) of the circle.
import java.awt.*;
public class Circle {
private int radius;
public Circle() {
radius = 1;
}
public Circle(int x, int y, int r) {
super(x, y, c);
radius = r;
}
public int getRadius() {
return radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
public double getPerimeter() {
return 2 * Math.PI * radius;
}
}
I could get this far, I'm just a little confused on the adding the point constructor, accessor and mutator to my class.
Would it look something like this?
protected int x, y;
public point() {
setPoint(0,0);
}
public point(int coordx, int coordy) {
setPoint(coordx,coordy);
}
public void setPoint(int coordx, int coordy) {
x = coordx;
y = coordy;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public String toPrint() {
return "[" + x + "," + y + "]";
}
}
Is it possible to combine the both in 1 Class? I tried it and every line in it had an error saying Circle had no return type. Any insight would be a returnable favor. Thanks again guys.
You're talking about having constructors for multiple objects in a single class. That can't be done. Java thinks that public point() is a method that doesn't have a return type and so is syntactically incorrect.
You don't need to make a class for point. Java provides java.awt.Point already. Just add a class level field for Point to your Circle class and you're good.
Circle would then look something like this:
public class Circle {
private int radius;
private Point point;
public Circle() {
point = new Point(0, 0);
radius = 1;
}
public Circle(int x, int y, int r) {
point = new Point(x, y);
radius = r;
}
public int getRadius() {
return radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
public double getPerimeter() {
return 2 * Math.PI * radius;
}
}
Here some code, a Circle is an extension of a point adding a radius to it
public class Point {
public Point(int x, int y) {
// .. set x and Y coord
}
// Getters and Setters
public int getX() {
return x;
}
public int getY() {
return y;
}
public String toPrint() {
return "[" + x + "," + y + "]";
}
// Your other Point methods...
private int x = 0;
private int y = 0;
}
public class Circle extends Point {
int rad;
public Circle (int x, int y, int radius) {
super(x, y);
rad = radius;
}
// Your other Circle methods
}
As promised: another way with no extension could be:
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 centre = null;
int radius = 0;
public Circle(int x, int y, int rad) {
centre = new Point(x, y);
radius = rad;
}
// Your other Circle methods
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With