Results 1 to 3 of 3
- 09-01-2010, 01:23 AM #1
"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
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).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);
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,
instead ofJava Code:Vector2D result = Vector2D.add(v1, v2, v3);
because the latter code just causes result to become an alias for v1 (changes made to result are also made to v1).Java Code:Vector2D result = v1.add(v2).add(v3);
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:
or like this:Java Code:public Vector2D add(Vector2D v) { this.x += v.x; this.y += v.y; return 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
- 09-01-2010, 02:03 AM #2
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:
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.Java Code:Vector2D x = new Vector2D(5,5); Vector2D y = x.add(new Vector2D(7,7));
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.
- 09-01-2010, 07:47 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,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; }
Jos
Similar Threads
-
Math.random() without a "0"
By FlyNn in forum New To JavaReplies: 4Last Post: 03-02-2010, 01:24 AM -
Java, Military Format using "/" and "%" Operator!!
By sk8rsam77 in forum New To JavaReplies: 11Last Post: 02-26-2010, 03:03 AM -
" shift " Operator ???
By MuslimCoder in forum New To JavaReplies: 5Last Post: 02-09-2009, 05:51 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks