need some debugging help... short code inside...

V

vtcompsci

/**
* Vector2d.java
* Represents some operations on 2-D vectors for geometry and basic
* physics computations.
*
*/
class Vector2d {
private float x = 0, y = 0;

/**
* Default Constructor.
*
*/
public Vector2d() { x = 0; y = 0; }

/**
* Parameterized Constructor. Sets the member variables to
* the values passed in.
* @param xv The desired value of x
* @param yv The desired value of y
*/
public Vector2d(float xv, float yv) { x = xv; y = yv; }
public Vector2d(double xv, double yv) { x = (float)xv; y = (float)yv;
}

// Accessor/Settor functions.
public void X(float xv) { x = xv; }
public void Y(float yv) { y = yv; }
public float X() { return x; }
public float Y() { return x; }

/**
* Add two vectors
* @param rhs the right hand side vector to be added
* @return a new vector, the sum of this + rhs.
*/
public Vector2d add(Vector2d rhs) {
return new Vector2d(x + rhs.x, y + rhs.y);
}

/**
* Subtract two vectors.
* @param rhs the right hand side vector
* @return a new vector, this - rhs
*/
public Vector2d sub(Vector2d rhs) {
return new Vector2d(x - rhs.x, y - rhs.y);
}

/**
* Negate a vector
* @return A new vector equal to -this
*/
public Vector2d neg() {
return new Vector2d(-x, -y);
}

/**
* Multiply a vector by a constant.
* @param d the constant by which the vector will be scaled.
* @return a new vector equal to d*this
*/
public Vector2d scale(float d) {
return new Vector2d(x * d, y * d);
}

/**
* Determine the magnitude of a vector.
* @return Return the magnitude of this
*/
public float mag() {
return (float) Math.sqrt(x*x + y*y);
}

/**
* Equality test for vectors.
* @param rhs The vector to compare to
* @return true if the vectors have the same components, false
othewise.
*/
public boolean equals(Vector2d rhs) {
return (x == rhs.x) && (y == rhs.y);
}
}
 
M

Monique Y. Mudama

Is there a question carefully hidden somewhere in all that code?

I will say that I like all the javadoc comments.
 
C

Chris Uppal

vtcompsci said:
class Vector2d {
private float x = 0, y = 0;
[... etc ...]

There are several more-or-less questionable things about this code, but you
don't say what your actual problem is.

-- chris
 
O

opalpa

Like Chris said, there are a number of other questionable things about
your code:
# equals is not the equals that is expected. You use an uncommon
signature.
# you have equals but no hashCode
# the constructor that takes doubles is ... well doubles don't fit
into floats all the time
# you have settors but otherwise you take care with add, sub, neg,
and scale to make class immutable. Get rid of settors and make class
immutable.
# IMO your comments are poor. They do not introduce any information.
Yeah, it's a default constructor, how does it leave Vector2d? Yeah,
add adds vectors, what does that mean? Yeah, scale scales, what does
that mean? etc. Sometimes for scientific code I will give a
reference to a popular text in one spot like "The vector opertions in
code below are defined as in "University Physics" by Freedman and
Young, 9th Edition, pages 11 through 22".


Opalinski
(e-mail address removed)
http://www.geocities.com/opalpaweb/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top