In: Computer Science
COMPLETE JAVA CODE:
public final class Vector2 {
// NOTE: Before you get started with the
constructors, implement the class variables and
// ===== the accessor methods (getX,getY).
The tester for the constructors relies on these
//
methods being implemented. After this, move ahead with the
constructors
// class variables here
COMPLETE JAVA CODE:
/**
* Creates the vector <code>(0.0, 0.0)</code>.
*/
public Vector2() {
COMPLETE JAVA CODE:
}
/**
* Creates the vector <code>(x, y)</code>.
*
* @param x
* the x-component of the vector
* @param y
* the y-component of the vector
*/
public Vector2(double x, double y) {
COMPLETE JAVA CODE:
}
/**
* Creates a vector with the same components as another
vector.
*
* @param other
* a vector to copy the components from
*/
public Vector2(Vector2 other) {
COMPLETE JAVA CODE:
}
/**
* Returns the x component of the vector.
*
* @return the x component of the vector.
*/
public double getX() {
COMPLETE JAVA CODE:
}
/**
* Sets the x component of the vector.
*
* @param x
* the new value of the x component.
*/
public void setX(double x) {
COMPLETE JAVA CODE:
}
/**
* Returns the y component of the vector.
*
* @return the y component of the vector.
*/
public double getY() {
COMPLETE JAVA CODE:
}
/**
* Sets the y component of the vector.
*
* @param y
* the new value of the y component.
*/
public void setY(double y) {
COMPLETE JAVA CODE:
}
/**
* Sets the x and y component of the vector.
*
* @param x
* the new value of the x component.
* @param y
* the new value of the y component.
*/
public void set(double x, double y) {
COMPLETE JAVA CODE:
}
/**
* Add a vector to this vector changing the components of this
vector.
*
* <p>
* Mathematically, if this vector is <code>a</code> and
the other vector is
* <code>b</code> then invoking this method is
equivalent to computing
* <code>a + b</code> and assigning the value back to
<code>a</code>.
*
* @param other
* the vector to add to this vector.
* @return this <code>Vector2D</code> object
*/
public Vector2 add(Vector2 other) {
COMPLETE JAVA CODE:
}
/**
* Subtract a vector from this vector changing the components of
this
* vector.
*
* <p>
* Mathematically, if this vector is <code>a</code> and
the other vector is
* <code>b</code> then invoking this method is
equivalent to computing
* <code>a - b</code> and assigning the value back to
<code>a</code>.
*
* @param other
* the vector to subtract this vector.
* @return this <code>Vector2D</code> object
*/
public Vector2 subtract(Vector2 other) {
COMPLETE JAVA CODE:
}
/**
* Multiply this vector by a scalar amount changing the components
of this
* vector.
*
* <p>
* Mathematically, if this vector is <code>a</code> and
the scalor is
* <code>s</code> then invoking this method is
equivalent to computing
* <code>s * a</code> and assigning the value back to
<code>a</code>.
*
* @param s
* the scalar value to multiply the vector by
* @return this <code>Vector2D</code> object
*/
public Vector2 multiply(double s) {
COMPLETE JAVA CODE:
}
/**
* Returns the magnitude of this vector.
*
* @return the magnitude of this vector.
*/
public double mag() {
COMPLETE JAVA CODE:
}
/**
* Returns a new <code>Vector2D</code> equal to
<code>a + b</code>.
*
* @param a
* a vector
* @param b
* another vector
* @return a new <code>Vector2D</code> equal to
<code>a + b</code>
*/
public static Vector2 add(Vector2 a, Vector2 b) {
COMPLETE JAVA CODE:
}
/**
* Returns a new <code>Vector2D</code> equal to
<code>a - b</code>.
*
* @param a
* a vector
* @param b
* another vector
* @return a new <code>Vector2D</code> equal to
<code>a - b</code>
*/
public static Vector2 subtract(Vector2 a, Vector2 b) {
COMPLETE JAVA CODE:
}
/**
* Returns a new <code>Vector2D</code> equal to
<code>s * a</code>.
*
* @param s
* a scalar
* @param a
* a vector
* @return a new <code>Vector2D</code> equal to
<code>s * a</code>
*/
public static Vector2 multiply(double s, Vector2 a) {
COMPLETE JAVA CODE:
}
/**
* Returns the vector having magnitude one pointing in the
direction
* <code>theta</code> degrees from the x axis.
*
* <p>
* The components of the vector are equal to
* <code>(Math.cos(rad), Math.sin(rad))</code> where
<code>rad</code> is
* <code>theta</code> expressed in radians.
*
* @param theta
* the direction that the vector is pointing in measured in
* degrees from the x axis
* @return the unit vector pointing in the given direction
*/
public static Vector2 dirVector(double theta) {
COMPLETE JAVA CODE:
}
/**
* Returns a string representation of the vector. The string is the
name of
* the vector, followed by the comma separated components of the
vector
* inside parentheses.
*
* @return a string representation of the vector
*/
@Override
public String toString() {
COMPLETE JAVA CODE:
}
/**
* Determines if two vectors are almost equal (similar). Two vectors
are
* similar if the magnitude of their vector difference is smaller
than the
* specified tolerance.
*
* @param other
* the other vector to compare
* @param tol
* the threshold length of the vector difference
* <code>(this - other)</code>
* @return <code>true</code> if the length of
<code>(this - other)</code> is
* less than <code>tol</code>, and
<code>false</code> otherwise
*/
public boolean similarTo(Vector2 other, double tol) {
COMPLETE JAVA CODE:
}
}
I have implement a small Test class to try out the functions,
import java.math.*;
//implemented Vector2 class
class Vector2 {
private double x, y;
public Vector2() {
x = 0;
y = 0;
}
public Vector2(double x, double y) {
this.x = x;
this.y = y;
}
public Vector2(Vector2 other) {
this.x = other.x;
this.y = other.y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public void set(double x, double y) {
this.x = x;
this.y = y;
}
public Vector2 add(Vector2 other) {
this.x += other.x;
this.y += other.y;
return this;
}
public Vector2 subtract(Vector2 other) {
this.x -= other.x;
this.y -= other.y;
return this;
}
public Vector2 multiply(double s) {
this.x *= s;
this.y *= s;
return this;
}
public double mag() {
return Math.sqrt(x * x + y * y);
}
public static Vector2 add(Vector2 a, Vector2 b) {
return new Vector2(a.x + b.x, a.y + b.y);
}
public static Vector2 subtract(Vector2 a, Vector2 b) {
return new Vector2(a.x - b.x, a.y - b.y);
}
public static Vector2 multiply(double s, Vector2 a) {
return new Vector2(s * a.x, s * a.y);
}
public static Vector2 dirVector(double theta) {
return new Vector2(Math.cos(theta), Math.sin(theta));
}
@Override
public String toString() {
return "Vector2_" + Integer.toHexString(System.identityHashCode(this)) + " [x=" + x + ", y=" + y + "]";
}
public boolean similarTo(Vector2 other, double tol) {
Vector2 c = Vector2.subtract(this, other);
double cLength = c.mag();
if (cLength < tol)
return true;
else
return false;
}
}
//example test class code
public class Test {
public static void main(String args[]) {
Vector2 a = new Vector2(3, 4);
Vector2 b = new Vector2(12, 5);
Vector2 c = Vector2.subtract(a, b);
System.out.println(a.mag());
System.out.println(b.mag());
System.out.println(c.mag());
System.out.println(a.similarTo(b, 10));
}
}
s