1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java - objects and methods

  1. Oct 27, 2012 #1

    CAF123

    User Avatar
    Gold Member

    1. The problem statement, all variables and given/known data
    1. The problem statement, all variables and given/known data
    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.

    3. The attempt at a solution
    Code (Text):
    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);
     
    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: Oct 27, 2012
  2. jcsd
  3. Oct 27, 2012 #2

    Mark44

    Staff: Mentor

    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.

    Discussed above.
    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.
    You don't have these methods. The one you have is for Point, not 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.
    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 (Text):

    // 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.
     
     
  4. Oct 27, 2012 #3

    CAF123

    User Avatar
    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?
     
  5. Oct 27, 2012 #4
    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?
     
  6. Oct 27, 2012 #5

    CAF123

    User Avatar
    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?
     
  7. Oct 27, 2012 #6

    gabbagabbahey

    User Avatar
    Homework Helper
    Gold Member

    Sure...
    Code (Text):
    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:

     
  8. Oct 28, 2012 #7

    CAF123

    User Avatar
    Gold Member

    Ok, here is what I have written for the first part;
    Code (Text):
     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 (Text):
     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 (Text):
     public double getX() {
    return aLoc;
    }
    public double getY() {
    return bLoc;
    }
    public double getZ() {
    return rLoc;
    }
    I am not sure about this one.
    For the fourth:
    Code (Text):
     
    //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 (Text):
     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();
    //Sum radii
    double addradii = c1.getZ() + c2.getZ();
    //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.
     
  9. Oct 28, 2012 #8

    gabbagabbahey

    User Avatar
    Homework Helper
    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 (Text):
    public class Circle {
          private double radius; //the radius of the 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 (Text):
    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;
          }
          this.radius=radius;
    }
    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.

    Don't forget to add import statements before your class definition so that the compiler can find Math.PI and Math.pow.
     
  10. Oct 28, 2012 #9

    CAF123

    User Avatar
    Gold Member

    Can I write what you have above as
    Code (Text):
     public Circle (Point centre, double radius) {
    centre = (a,b);
    radius = r;
    I wrote:
    Code (Text):
     public Point getcentre() {
    return centre;
    }
    public double getradius() {
    return radius;
    }
    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.
     
  11. Oct 28, 2012 #10

    Mark44

    Staff: Mentor

    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 (Text):
    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.
     
  12. Oct 28, 2012 #11

    gabbagabbahey

    User Avatar
    Homework Helper
    Gold Member


    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.

    good. :approve: (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)

    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 (Text):
    public void setRadius(double radius) {
          this.radius = 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)
     
  13. Oct 28, 2012 #12

    CAF123

    User Avatar
    Gold Member

    When doing the set method for the centre, do you mean that I should use if/else, testing..
    Code (Text):
     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.

    To find the distance should I do something like double separation = Math.abs(c1.getCentre - c2.gtCentre) and double addradii = c1.getRadius + c2.getRadius
    Does this all seem reasonable?
     
    Last edited: Oct 28, 2012
  14. Oct 28, 2012 #13

    gabbagabbahey

    User Avatar
    Homework Helper
    Gold Member

    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).

    Seems overly complicated. Why not just compare the distance between the centres of the circles to the sum of their radii?
     
  15. Oct 28, 2012 #14

    CAF123

    User Avatar
    Gold Member

    How would I compute the distance between the centres ? (I don't think what I had in my previous post is correct).
     
  16. Oct 28, 2012 #15

    gabbagabbahey

    User Avatar
    Homework Helper
    Gold Member

    Does your Point class have a method for finding the distance between two points?
     
  17. Oct 28, 2012 #16

    CAF123

    User Avatar
    Gold Member

    Is this right;
    Code (Text):
     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.
     
  18. Oct 28, 2012 #17

    gabbagabbahey

    User Avatar
    Homework Helper
    Gold Member

    Looks good, except that you are missing a couple of closing braces } and have an extra opening brace at the end instead.

    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.
     
  19. Oct 28, 2012 #18

    CAF123

    User Avatar
    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 compute the separation using just 'centre'
     
  20. Oct 28, 2012 #19

    gabbagabbahey

    User Avatar
    Homework Helper
    Gold Member

    Can you post the code for the Point class from your earlier examples?
     
  21. Oct 28, 2012 #20

    CAF123

    User Avatar
    Gold Member

    I had
    Code (Text):
    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);
    where getX got the x coord of one centre and getY got the y coord of the centre
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java - objects and methods
  1. JAVA indexOf() method (Replies: 0)

Loading...