# Java - objects and methods

• Comp Sci
Gold Member

## 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();
//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);
Questions: the above is my initial attempt at the class. I think what I have written assumes that r is like a z coordinate (my understanding is that the Point class defines a point in 3d space) - so how else would i define r?
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:

Related Engineering and Comp Sci Homework Help News on Phys.org
Mark44
Mentor

## 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();
//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);
Questions: the above is my initial attempt at the class. I think what I have written assumes that r is like a z coordinate (my understanding is that the Point class defines a point in 3d space) - so how else would i define r?
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.
It looks to me like you are heading down the wrong path. You have defined a class for Point, but I don't see a class for Circle, which is what the problem asks for.

In response to your questions - the radius is a number, so you should define it as a private member of the Circle class, of type double. You could define the center of the circle as two private members, also of type double. I would dispense with the Point class and focus on the immediate problem.

CAF123 said:
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.
Discussed above.
CAF123 said:
-Constructors to create circle objects with nothing supplied, with just a radius value supplied and with a radius and centre supplied
The only constructor I see is for the Point class. You need three Circle constructors: a default constructor (no parameters), a constructor with a radius parameter, and a constructor with radius and center parameters. The latter constructor could be written with three parameters - one for the radius and two more for the x- and y-coordinates of the center.
CAF123 said:
-Public instance methods that allow the radius and centre coordinates to be set and retrieved(via get/set methods)
You don't have these methods. The one you have is for Point, not Circle.
CAF123 said:
-Public instance methods that return the area and circumference of the circle
Your area method won't work because rLoc is a member of the Point class, not the Circle class. Also, you need to rethink this name - you don't need the location of the radius, whatever that means, just its value.
CAF123 said:
-A public class method to test whether two circles overlap.
This method should return a Boolean value, not a double. It should return true if the circles overlap, and false if they don't. It should take a Circle parameter. The name of the method should be something like overlaps, not separation.

It would be used like this:
Code:
// Initialize circle1 and circle2 - not shown.

result = circle1.overlaps(circle2);

// result will be true if there is overlap of the two circles, and false if not.

Gold Member
Is the Point class just an example of a class? It was used extensively in the notes given and I thought it might be applicable here. Do I have to use the point class at all here?

Is the Point class just an example of a class? It was used extensively in the notes given and I thought it might be applicable here. Do I have to use the point class at all here?
Things you want to do with points should be in the point class. You don't want to have point-like code distributed around a bunch of related classes because it increases code size and makes the code harder to understand, maintain, debug and modify. The OO style is all about localizing functions and properties (of an object) in one place so that it can be reused by the rest of the program. The identification of objects and how they interact is part of software engineering using the OO approach and factoring is an important attribute of making solutions tractable.

What sort of properties does your circle have? I'd say a radius and a centre identified by a point.

If your circle did not use the point class, would it be harder to change the program in the future if you were to modify it to 3d? What if you had squares, rectangles, rhomubs, circle, ellipse, etc. would it be harder to modify if they all had their own point code?

Gold Member
Thanks, I'll have a go with the changes tomorrow. For now, can you give me any hints on how I should actually start this class?

gabbagabbahey
Homework Helper
Gold Member
For now, can you give me any hints on how I should actually start this class?
Sure...
Code:
public class Circle {
...
}
As for hints on what to put in the class, try following the very explicit step-by-step instructions you were given:

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

Gold Member
Ok, here is what I have written for the first part;
Code:
 Public class Circle {
private double aLoc, bLoc, rLoc//the centre of the Circle coords (a,b) and radius r stored as private doubles.
is this ok for the 1st part?
For the second bullet point:
Code:
 public Circle () { //with nothing supplied
//set the internal variables
aLoc = 0;
bLoc = 0;
rLoc = 0;
}
public Circle (double r) { // with only radius supplied
aLoc = 0;
bLoc = 0;
rLoc = r;
}
public Circle (double a, double b, double r) { //with centre and radius supplied
aLoc = a;
bLoc = b;
rLoc = r;
}
Correct for the second bullet?
For the third:
Code:
 public double getX() {
return aLoc;
}
public double getY() {
return bLoc;
}
public double getZ() {
return rLoc;
}
For the fourth:
Code:
//Public instance methods to return circumference and area of circle
public double circumference() {
return (2.0)*(Math.PI)*(r);
}
public double area() {
return (Math.PI)*(Math.pow(r,2));
}
Yes?
For the final bullet:
Code:
 public static boolean overlaps(Circle c1, Circle c2) {
//Get the distance between the centres of the two circles
double xDelta = c1.getX() - c2.getX();
double yDelta = c2.getY() - c2.getY();
//Compute separation
return Math.sqrt(xDelta*xDelta + yDelta*yDelta);
I am not sure where to go from here. Possibly use an if statement(if separation > addradii then no overlap, else overlap?)
Many thanks.

gabbagabbahey
Homework Helper
Gold Member
Ok, here is what I have written for the first part;
Code:
 Public class Circle {
private double aLoc, bLoc, rLoc//the centre of the Circle coords (a,b) and radius r stored as private doubles.
is this ok for the 1st part?
The centre of the circle is a point, so why not use the Point class to define it as such? Also, why call the radius rLoc (the radius is a scalar value, not a location, so this name seems illogical to me) instead of something more descriptive like radius:

Code:
public class Circle {
private Point centre;   //the centre of the circle
...
}
For the second bullet point:
Code:
 public Circle () { //with nothing supplied
//set the internal variables
aLoc = 0;
bLoc = 0;
rLoc = 0;
}
public Circle (double r) { // with only radius supplied
aLoc = 0;
bLoc = 0;
rLoc = r;
}
public Circle (double a, double b, double r) { //with centre and radius supplied
aLoc = a;
bLoc = b;
rLoc = r;
}
Correct for the second bullet?
Using the origin (0,0) for the centre of the circle by default makes sense, but why use zero as the default radius? I would think that a unit circle (radius = 1) would make a more appropriate default. And again, I would make use of the Point
class for the centre:

Code:
public Circle (Point centre, double radius) { //with centre and radius supplied
/*If the supplied centre is a null object, use the origin as the centre,
otherwise use the supplied object*/
if(centre==null) {
this.centre=new Point(0,0); // new Point object representing the origin
} else {
this.centre=centre;
}
}
For the third:
Code:
 public double getX() {
return aLoc;
}
public double getY() {
return bLoc;
}
public double getZ() {
return rLoc;
}
Usually you want to make variable and method names as descriptive as possible, without being overwhelmingly long. Naming a method to return the radius of the circle "getZ" is not very descriptive. Try to think of a better name. And again, I would make use of the Point class and have a getCentre method instead of the getX and getY methods. You've also neglected to define set methods for setting/changing the radius and centre.

For the fourth:
Code:
//Public instance methods to return circumference and area of circle
public double circumference() {
return (2.0)*(Math.PI)*(r);
}
public double area() {
return (Math.PI)*(Math.pow(r,2));
}
Yes?
Don't forget to add import statements before your class definition so that the compiler can find Math.PI and Math.pow.

Gold Member
The centre of the circle is a point, so why not use the Point class to define it as such? Also, why call the radius rLoc (the radius is a scalar value, not a location, so this name seems illogical to me) instead of something more descriptive like radius:

Code:
public class Circle {
private Point centre;   //the centre of the circle
...
}

Using the origin (0,0) for the centre of the circle by default makes sense, but why use zero as the default radius? I would think that a unit circle (radius = 1) would make a more appropriate default. And again, I would make use of the Point
class for the centre:

Code:
public Circle (Point centre, double radius) { //with centre and radius supplied
/*If the supplied centre is a null object, use the origin as the centre,
otherwise use the supplied object*/
if(centre==null) {
this.centre=new Point(0,0); // new Point object representing the origin
} else {
this.centre=centre;
}
}
Can I write what you have above as
Code:
 public Circle (Point centre, double radius) {
centre = (a,b);
radius = r;
Usually you want to make variable and method names as descriptive as possible, without being overwhelmingly long. Naming a method to return the radius of the circle "getZ" is not very descriptive. Try to think of a better name. And again, I would make use of the Point class and have a getCentre method instead of the getX and getY methods. You've also neglected to define set methods for setting/changing the radius and centre.
I wrote:
Code:
 public Point getcentre() {
return centre;
}
}
I am not familiar with the set methods. They are not in my notes either - how would you write the code for the set part?

Also, to determine if the two circles overlap, I am not sure really how to begin this. I thought an if,else statement would be appropriate somewhere but I am not sure how to start.
Thanks again.

Mark44
Mentor
For the final bullet:
Code:
 public static boolean overlaps(Circle c1, Circle c2) {
//Get the distance between the centres of the two circles
double xDelta = c1.getX() - c2.getX();
double yDelta = c2.getY() - c2.getY();
//Compute separation
return Math.sqrt(xDelta*xDelta + yDelta*yDelta);
I am not sure where to go from here. Possibly use an if statement(if separation > addradii then no overlap, else overlap?)
Many thanks.
Your overlaps method is returning a double when it should be returning a boolean. Yes, you will need to use an if .. else ... block to determine whether the circles overlap. The logic will have to take into account several possibilities:
• One circle completely inside the other.
• One circle partially inside the other.
• Both circles completely separated.
It would be helpful to draw some pictures of these situations, with centers labelled and radii identified. It would be a good idea to use these as test cases to check whether your code is producing the correct results.

Here is a skeleton of what your overlaps method could look like.
Code:
public static boolean overlaps(Circle c1, Circle c2)
{
if (...)
{
// One circle inside the other
return true;
}
else if ( ... )
{
// One circle partially inside the other
return true;
}
else
{
return 0;
}
}
Note that each of the first two clauses needs to check two possibilities. For example, c1 inside c2 or c2 inside c1.

gabbagabbahey
Homework Helper
Gold Member
Can I write what you have above as
Code:
 public Circle (Point centre, double radius) {
centre = (a,b);
radius = r;

No. For starters, the method parameters are still named centre and radius, so a, b, and r are not declared anywhere. Secondly, (a,b) is not an object of the class Point, in fact, it even isn't syntactically allowed in Java (the compiler won't know how to interpret (a,b)).

If you are confused about the syntax I used; this.centre=centre, you should know that it is fairly common. Both the class variable for radius, and the method parameter have the same name in my example (radius), so to set the class variable radius equal to the method parameter radius, you need to qualify the class variable by the keyword this.

this.centre tells the compiler that you are referring to the class variable rather than the method parameter of the same name. You could avoid having to use the this keyword by using a different name for the method parameter, but it is usually considered best practise to just keep the same name (other programmers will disagree).

If you are confused about passing a Point object as a parameter instead of two numbers, consider that this is the crux of object-oriented programming. The centre of a circle is a point, so it is advantageous to have a Point class, where objects of the Point class represent specific points, like the centre of a circle. You can then use Point objects in code wherever you are dealing with Points, rather than having to rewrite code each time you want to do something like test if two points are the same, or find the distance between two points.

I wrote:
Code:
 public Point getcentre() {
return centre;
}
}
good. (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 getCentre instead of getcentre, and likewise for your second method)

I am not familiar with the set methods. They are not in my notes either - how would you write the code for the set part?
A set method is used to set/change the value of a class variable to that of a parameter which is passed into the method. So, for example, to set the radius of the circle you could have:

Code:
public void setRadius(double radius) {
}
You will need a similar method for the centre of the circle (you will probably want to test to make sure the parameter passed to that method is not null)

Gold Member
When doing the set method for the centre, do you mean that I should use if/else, testing..
Code:
 public void setCentre(Point centre) {
if centre==null {
this.centre = new Point(0,0);
}else {
this.centre=centre;
}
}
I take it this goes before the get methods?

In terms of testing for circle overlap, I have :
If distance between centres is less than smaller radius (say c2) and radius of c2<c1 then c2 in c1.
If distance between centres is less than smaller radius (say c1) and radius of c1<c2 then c1 in c2. (that's for one circle inside the other)
Else if distance between centres of the circles is less than or equal to the sum of the radii then intersect.
Else they are disjoint.

Does this all seem reasonable?

Last edited:
gabbagabbahey
Homework Helper
Gold Member
When doing the set method for the centre, do you mean that I should use if/else, testing..
Code:
 if centre==null {
this.centre = new Point(0,0);
}else {
this.centre=centre;
}
I take it this goes before the get methods?
I'm not sure exactly what you are asking here. Before you can call the get methods of an object externally, the object must be instantiated (you must create an instance of the object), so one of the constructor methods will certainly be called before any of the get methods. Each variation of your constructor methods sets centre equal to a specific (non-null) Point object, so there should be no problem returning a sensible value ( a non-null Point object) for the centre of your circle in your getCentre method, unless you have a setCentre method which allows setting centre equal to null (which is why you will want to test for that in your setCentre, the same way I did in the example constructor method I gave you).

In terms of testing for circle overlap, I have :
If distance between centres is less than smaller radius (say c2) and radius of c2<c1 then c2 in c1.
If distance between centres is less than smaller radius (say c1) and radius of c1<c2 then c1 in c2. (that's for one circle inside the other)
Else if distance between centres of the circles is less than or equal to the sum of the radii then intersect.
Else they are disjoint.

Does this all seem reasonable?
Seems overly complicated. Why not just compare the distance between the centres of the circles to the sum of their radii?

Gold Member
Seems overly complicated. Why not just compare the distance between the centres of the circles to the sum of their radii?
How would I compute the distance between the centres ? (I don't think what I had in my previous post is correct).

gabbagabbahey
Homework Helper
Gold Member
How would I compute the distance between the centres ? (I don't think what I had in my previous post is correct).
Does your Point class have a method for finding the distance between two points?

Gold Member
unless you have a setCentre method which allows setting centre equal to null (which is why you will want to test for that in your setCentre, the same way I did in the example constructor method I gave you).
Is this right;
Code:
 public void setCentre(Point centre) {
if centre==null {
this.centre = new Point(0,0);
}else {
this.centre = centre;
{
Sorry, what I meant was should I write in the code for the set method in the program before the get methods? Or is it the case that they are meshed together? (I.e setRadius... getRadius... and then setCentre... getCentre...)

I have a lab session tomorrow so I will try all your suggestions then - thanks a lot.

gabbagabbahey
Homework Helper
Gold Member
Is this right;
Code:
 public void setCentre(Point centre) {
if centre==null {
this.centre = new Point(0,0);
}else {
this.centre = centre;
{
Looks good, except that you are missing a couple of closing braces } and have an extra opening brace at the end instead.

Sorry, what I meant was should I write in the code for the set method in the program before the get methods?
It doesn't matter which order you define them in. You could even define them before you define your class variables, and the compiler will still sort everything out.

Gold Member
Does your Point class have a method for finding the distance between two points?
I think this is what I had in my very first post, when I computed xDelta and yDelta etc... but instead of defining the actual points (as i did initially) I have just defined centre now, so I am not sure how to compute the separation using just 'centre'

gabbagabbahey
Homework Helper
Gold Member
I think this is what I had in my very first post, when I computed xDelta and yDelta etc... but instead of defining the actual points (as i did initially) I have just defined centre now, so I am not sure how to how this.
Can you post the code for the Point class from your earlier examples?

Gold Member
Code:
double xDelta = p1.getX() - p2.getX();
double yDelta = p1.getY() - p2.getY();
//Compute separation
return Math.sqrt(xDelta*xDelta + yDelta*yDelta);
where getX got the x coord of one centre and getY got the y coord of the centre

gabbagabbahey
Homework Helper
Gold Member
Code:
double xDelta = p1.getX() - p2.getX();
double yDelta = p1.getY() - p2.getY();
//Compute separation
return Math.sqrt(xDelta*xDelta + yDelta*yDelta);
where getX got the x coord of one centre and getY got the y coord of the centre
I meant the version of the Point class that you said was in your notes and used in previous examples. Can you post that code? That's the version you will want to use to define centre in your circle class. Objects of the Point class should be used to represent points. Objects of the Circle class should be used to represent circles.

Gold Member
I meant the version of the Point class that you said was in your notes and used in previous examples. Can you post that code? That's the version you will want to use to define centre in your circle class. Objects of the Point class should be used to represent points. Objects of the Circle class should be used to represent circles.
Ah,ok. So in the Point class I should have variables that represent a point (I.e aLoc = a, bLoc =b etc..) and then use these to compute separation/add radii.
Does this mean I need to add set/get methods for these too?
EDIT: the code in my notes is more or less what I used in my first attempt at the program.

gabbagabbahey
Homework Helper
Gold Member
Ah,ok. So in the Point class I should have variables that represent a point (I.e aLoc = a, bLoc =b etc..) and then use these to compute separation/add radii.
Does this mean I need to add set/get methods for these too?
EDIT: the code in my notes is more or less what I used in my first attempt at the program.
Sort of. I would say that your Point class should have attributes (private class instance variables) for the x- and y-coordinates of the point, a constructor(s) to set these variables when an new Point object is created (instantiated), as well as set & get methods for each variable, and some utility methods for doing stuff with/to points, like testing if two points are equal (override the equals method that all Objects have - if you haven't covered doing that in class yet, just don't worry about it for now) and computing the distance between two points (a static method that takes two Point objects as parameters and returns the distance between them).

Something like:

Code:
import static java.lang.Math.sqrt;
import static java.lang.Math.pow;

public class Point {

private double xCoord;
private double yCoord;

//constructor with no arguments creates the point (0,0)
public Point() {
this.xCoord = 0;
this.yCoord = 0;
}

//overload constructor to create a point with given coordinates
public Point(double xCoord, double yCoord) {
this.xCoord = xCoord;
this.yCoord = yCoord;
}

public void setX(double xCoord) {
this.xCoord = xCoord;
}

public void setY(double yCoord) {
this.yCoord = yCoord;
}

public double getX() {
return this.xCoord;
}

public double getY() {
return this.yCoord;
}

/*override the equals method such that two points are equal iff
both their x- & y-coordinates are identical */
@Override
public boolean equals(Object o) {
Point point;
if(o instanceof Point) {
point = (Point)o;
} else{
return false;
}
if(this.xCoord==point.xCoord && this.yCoord==point.yCoord) {
return true;
} else {
return false;
}
}

//static method to compute the distance between two points a & b
public static double distanceBetween(Point a, Point b) {
return sqrt( pow(a.getX() - b.getX(), 2) + pow(a.getY() - b.getY(), 2) );
}
}
In your Circle class, you would then use the Point.distanceBetween method to find the distance between the centers of the circles (since they are points)

Mark44
Mentor
I think this is what I had in my very first post, when I computed xDelta and yDelta etc... but instead of defining the actual points (as i did initially) I have just defined centre now, so I am not sure how to compute the separation using just 'centre'
The reason I suggested that you abandon your attempt with the Point class was that you seemed completely overwhelmed by having to think about two different classes (Point and Circle). You were writing a class for Point and weren't taking care of business with the Circle class; i.e., you weren't writing the various members of the Circle class that this problem asks for.