Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
vtcompsci - 02 Feb 2006 04:27 GMT
/**
* 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);
   }
}
Monique Y. Mudama - 02 Feb 2006 04:58 GMT
Is there a question carefully hidden somewhere in all that code?

I will say that I like all the javadoc comments.

> /**
>  * Vector2d.java
[quoted text clipped - 80 lines]
>     }
> }

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Chris Uppal - 02 Feb 2006 10:44 GMT
> 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
opalpa@gmail.com opalinski from opalpaweb - 02 Feb 2006 15:12 GMT
> public float Y() { return x; }

Cheers.

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
opalpa@gmail.com opalinski from opalpaweb - 02 Feb 2006 15:29 GMT
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
opalpa@gmail.com
http://www.geocities.com/opalpaweb/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.