Results 1 to 3 of 3
  1. #1
    rgrant222's Avatar
    rgrant222 is offline Member
    Join Date
    Jul 2010
    Location
    Lawrenceville, Ga
    Posts
    17
    Rep Power
    0

    Post "Best" implementation of math operator methods?

    What do you guys think the "best" implementation for a Vector2D class would be? Basically, the question I have is whether methods like add, scale, normalize, etc., should return a new instance or not. At the moment, my class behaves just like the java.math.BigInteger class. For example, the output of

    Java Code:
    Vector2D v1 = new Vector2D(3, 0);
    Vector2D v2 = new Vector2D(0, 4);
    Vector2D v3 = new Vector2D(1, 1);
    v1.add(v2).add(v3);
    System.console().format("(%.0f, %.0f)", v1.x, v1.y);
    would be "(4, 5)". In other words, the add method (and others) alters the object on which it was called and then returns a self-reference (this).

    I'm using Vector2D objects as instance variables in another class. The problem is that when I want to add vectors together (for example) without affecting either operand I have to call a static version of add. For example,

    Java Code:
    Vector2D result = Vector2D.add(v1, v2, v3);
    instead of

    Java Code:
    Vector2D result = v1.add(v2).add(v3);
    because the latter code just causes result to become an alias for v1 (changes made to result are also made to v1).

    Calling the static methods clutters up my code because in many cases I need to pass altered Vector2D objects to other methods without actually altering the Vector2D objects I'm passing in as arguments.

    What do Java programmer's usually expect (if anything) with math operator methods, or what is the common practice? In other words, would it be "better" to write the add method like this:

    Java Code:
    public Vector2D add(Vector2D v) {
    	this.x += v.x;
    	this.y += v.y;
    	return this;
    }
    or like this:

    Java Code:
    public Vector2D add(Vector2D v) {
    	Vector2D result = new Vector2D();
    	result.x = this.x + v.x;
    	result.y = this.y + v.y;
    	return result;
    }
    ?
    Learn something new... wiser-today.blogspot.com

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You're probably better off using the second method. Having said this, it's personal preference for the coder. Neither is technically right or wrong, simply which one you think will be more useful in the program. However consider this:
    Java Code:
    Vector2D x = new Vector2D(5,5);
    Vector2D y = x.add(new Vector2D(7,7));
    Now both x and y are the same, so what's the point of even having y? If you want to keep those separate, you have to use the second implementation.

    If you want my explicit opinion on what I'd expect, I can't answer that. I'd assume that the programmer working before me provided some documentation, or at the very least provided his source for me to see. That way I can tell for myself what the best way to use it. Never assume.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by rgrant222 View Post
    What do Java programmer's usually expect (if anything) with math operator methods, or what is the common practice? In other words, would it be "better" to write the add method like this:

    Java Code:
    public Vector2D add(Vector2D v) {
    	this.x += v.x;
    	this.y += v.y;
    	return this;
    }
    or like this:

    Java Code:
    public Vector2D add(Vector2D v) {
    	Vector2D result = new Vector2D();
    	result.x = this.x + v.x;
    	result.y = this.y + v.y;
    	return result;
    }
    ?
    Don't change the vectors; imagine what would happen if you did v.add(v).add(v). The result would not be what you expected. Make a copy constructor and operators that change the vector itself. You'll end up with things like this:

    Java Code:
    public Vector2D add(Vector2D that) {
       Vector2D copy= Vector2D(this); // create a copy
       return copy.addThis(that); // do the real addition
    }
    public Vector2D addThis(Vector2D that) {
       this.x+= that.x;
       this.y+= that.y;
       return this;
    }
    kind regards,

    Jos

Similar Threads

  1. Math.random() without a "0"
    By FlyNn in forum New To Java
    Replies: 4
    Last Post: 03-02-2010, 02:24 AM
  2. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 04:03 AM
  3. " shift " Operator ???
    By MuslimCoder in forum New To Java
    Replies: 5
    Last Post: 02-09-2009, 06:51 AM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •