"boolean" question! why there is no comma in between two variables?

edited September 2017 in Questions about Code
class Particle {
  float x, y;
  float r;

  Particle() {
    x = random(width);
    y = random(height);
    r= random (4,30);

  }

  Particle(float tempX, float tempY, float tempR) {

    x=tempX;
    y=tempY;
    r=tempR;
  }

  boolean overlaps(Particle other) {
    float d = dist(x,y,other.x,other.y);
    if (d< r+ other.r){
      return true;
    } else {
      return false;
  }
  }
  void display() {
    stroke(255);
    strokeWeight(4);
    noFill();
    ellipse(x,y,r*2,r*2);
  }

In the code above, I don't get the logic behind

boolean overlaps(Particle other) {
  float d= dist(x,y,other.x,other.y);
  if(d<r+other.r){
    return true;
  }else{
    return false;
  }
}

For me it seems like, newly defined function 'overlaps' would take two inputs one of which is Particle and the other is other. And it will execute with the variable float d which represents distance between x&y coordinates of Particle and x&y coordinates of other. if the distance value is smaller than the sum of radius of Particle and other, it will yield the 'true' value.

However,

specifically, for "overlaps(Particle other)", I think there must be a comma between Particle and other as 'Particle' also requires commas among each entity going into its round bracket. like Particle (float tempX, float tempY,float tempZ).

I think I totally don't understand the part from boolean. can you explain what the part below is meaning?

boolean overlaps(Particle other) {
  float d= dist(x,y,other.x,other.y);

I really don't get this part...

Answers

  • edited September 2017 Answer ✓

    When you define a class, you are creating a new type of object. You use various types of objects all the time without realizing it: int is a type of object that represents a number; String is a type of object that represents a bunch of char objects in some order.

    By defining the class Particle, you have created a new type of object: Particle

    Now this class has a function in it, called overlaps(). This function is going to return a boolean, and it also takes one object as input: some other Particle object. You can tell all this from the definition of the function:

    boolean overlaps( Particle other ){
    

    boolean is the returned type. overlaps() is the function name And the input to this function is a Particle called other

    There is no need for a comma between Particle and other, because what that is saying is that there is one input. The type of the input is Particle. And the name of the input is other.

  • @TfGuy44 Thank you so much. Can I ask you one more question? if "Particle other" is just a name of another Particle,

  • if Particle other is just a name of another Particle.

    • other is a parameter of method overlaps() from class Particle.
    • Parameters are functionally exactly the same as local variables.
    • The only diff. is that parameters are initialized by the arguments passed to their method.
    • While regular local variables are declared and initialized within their method's body.
    • Given that parameter other was declared as being of datatype Particle, the argument passed & assigned to it is also of datatype Particle, or a subtype of it.
  • edited September 2017

    As a bonus, method Particle::overlaps(Particle) can be greatly simplified like this: :D

    boolean overlaps(Particle p) {
      return dist(x, y, p.x, p.y) < r + p.r;
    }
    
  • edited September 2017

    int is a type of object that represents a number;

    @TfGuy44, Java's datatype int isn't an object/reference/pointer but simply primitive. L-)

    There are exactly 8 primitive datatypes in Java.
    All of the other types can be called objects, references, pointers or even memory addresses: ~O)

    http://Docs.Oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Sign In or Register to comment.