CAF123
Gold Member
- 2,918
- 87
Homework Statement
Homework Statement
I need to write a class that represents a circle object and includes the following;
-Private Class variables that store the radius and centre coordinates of the object.
-Constructors to create circle objects with nothing supplied, with just a radius value supplied and with a radius and centre supplied
-Public instance methods that allow the radius and centre coordinates to be set and retrieved(via get/set methods)
-Public instance methods that return the area and circumference of the circle
-A public class method to test whether two circles overlap.
The Attempt at a Solution
Code:
Public class Point {
private double aLoc, bLoc;
private double rLoc;
public Point (double a, double b) {
aLoc = a;
bLoc = b;
}
//First question: how to define r?
//Define a public instance method to retrieve a,b,r
//for a
public double getX() {
return aLoc;
}
//similar code for b and r.
//Public instance methods to return circumference and area
public double circumference() {
return (2.0)*(Math.PI)*(rLoc);
}
public double area() {
return (Math.PI)*(Math.pow(rLoc,2));
}
//Determine if two circles overlap (intersect)
public static double separation (Point p1, Point p2) {
double xDelta = p1.getX() - p2.getX();
double yDelta = p1.getY() - p2.getY();
//sum of radii
double addradii = p1.getZ() + p2getZ();
//Compute separation
return Math.sqrt(xDelta*xDelta + yDelta*yDelta);
//do I have to determine if they overlap here, or can I, in the main program, use an if else statement?
//want to prompt user for centre and radius implies use a string
public string toString() {
return String.format (''(%g,%g,%g)'', aLoc, bLoc, rLoc);
Does the rest of the class look reasonable - anything that I may have missed out?
Many thanks
Edit: I am new to objects and methods, so apologies if what I have written does not make sense.
Last edited:
(Although it is usually considered best practise to name methods such that the first letter of each word is capitalized, with the exception of the first word, in order to clearly mark boundaries between words. So best practise would be to name your first method