Difference between revisions of "Vector class lab assignment"

From WLCS
Line 27: Line 27:
 
* Vector add(Vector v)
 
* Vector add(Vector v)
 
** returns a new Vector after calculating the Vector addition of itself and Vector v
 
** returns a new Vector after calculating the Vector addition of itself and Vector v
 +
** Note: if you need to use arc tangent, then use the [https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#atan2(double,%20double) Math.atan2() method]
 
* Vector sub(Vector v)
 
* Vector sub(Vector v)
 
** returns a new Vector after calculating the Vector subtraction of itself and Vector v
 
** returns a new Vector after calculating the Vector subtraction of itself and Vector v
 +
** Note: if you need to use arc tangent, then use the [https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#atan2(double,%20double) Math.atan2() method]
  
 
== Testing Vector class ==
 
== Testing Vector class ==

Revision as of 16:13, 23 October 2017

Resources

Vector class

  • Create a Vector class using the following specifications (HINT: Use any of your notes or other classes as resources):

BE SURE TO COMMENT YOUR CODE

Attributes (private):

  • double magnitude (default: 0)
  • double direction (default: 0) - in radians

Methods (public)

  • Vector() - default constructor
  • Vector(double r, double theta) - specific constructor
  • void setMagnitude(double newMag) - sets the magnitude
  • void setDirection(double newDir) - sets the direction (in radians)
  • double getMagnitude() - returns the magnitude
  • double getDirection() - returns the direction (in radians)
  • double getDirectionDegrees() - returns the direction (in degrees)
  • double getX()
    • returns the Cartesian x-coordinate of the Vector (look at Resources to convert Polar->Cartesian)
  • double getY()
    • returns the Cartesian y-coordinate of the Vector (look at Resources to convert Polar->Cartesian)
  • String toString() - returns a String representation of the Vector
  • Vector add(Vector v)
    • returns a new Vector after calculating the Vector addition of itself and Vector v
    • Note: if you need to use arc tangent, then use the Math.atan2() method
  • Vector sub(Vector v)
    • returns a new Vector after calculating the Vector subtraction of itself and Vector v
    • Note: if you need to use arc tangent, then use the Math.atan2() method

Testing Vector class

  • Write your own main method to test out all the methods in the Vector class
//Example (incomplete -- add more testing)
Vector v = new Vector();
Vector v2 = new Vector(4, Math.PI/2);

v.setMagnitude(3);

System.out.println(v);
System.out.println(v2);

Vector v3 = v.add(v2);

System.out.println(v3);