//Code by Grey Hugentobler, March 31st, 2011 through April 3rd, 2011 package GravityApplet; public class Vector { public static void main(String[] Args) { } private float xCoordinate; private float yCoordinate; // create the Vector public Vector(float x, float y) { this.xCoordinate = x; this.yCoordinate = y; } //toStringMethod public String toString(){ return "(" + this.xCoordinate + ", " + this.yCoordinate + ")"; } // get the x Coordinate of the Vector public float x() { return this.xCoordinate; } // get the y Coordinate of the Vector public float y() { return this.yCoordinate; } //get the length of the Vector public float length() { return Math.round(Math.sqrt(this.xCoordinate*this.xCoordinate + this.yCoordinate*this.yCoordinate)); } //get the direction of the Vector public float angle() { return Math.round(Math.atan2(this.yCoordinate, this.xCoordinate)); } // add two Vectors public Vector add(Vector that) { return new Vector(this.xCoordinate + that.xCoordinate, this.yCoordinate + that.yCoordinate); } // get the negative of a vector public Vector negative() { return new Vector(-this.xCoordinate, -this.yCoordinate); } // subtract one vector from another public Vector subtract(Vector that) { return this.add(that.negative()); } // get the dot product of two Vectors public float dot(Vector that) { return this.xCoordinate * that.xCoordinate + this.yCoordinate * that.yCoordinate; } // Multiply a Vector by a scalar public Vector multiply(float that) { return new Vector(that * this.xCoordinate, that * this.yCoordinate); } // return the distance between the endpoints of two Vectors public float distance(Vector that) { return Math.round(Math.sqrt((this.subtract(that)).dot(this.subtract(that)))); } }