import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class Threevector { private double xcoord, ycoord, zcoord; // Private data members // Default constructor Threevector() { xcoord = 0.0; ycoord = 0.0; zcoord = 0.0; } // Cartesian constructor Threevector(double x, double y, double z) { xcoord = x; ycoord = y; zcoord = z; } // Vector duplication constructor Threevector(Threevector v) { xcoord = v.getX(); ycoord = v.getY(); zcoord = v.getZ(); } // Spherical polar constructor //Example call: Threevector v1 = new Threevector(1,2,3,Type.d); - d for degrees, r for radians Threevector(double r, double theta, double phi, Type type) { switch (type){ case d: xcoord = r*Math.sin(Math.toRadians(theta))*Math.cos(Math.toRadians(phi)); ycoord = r*Math.sin(Math.toRadians(theta))*Math.sin(Math.toRadians(phi)); zcoord = r*Math.cos(Math.toRadians(theta)); case r: xcoord = r*Math.sin(theta)*Math.cos(phi); ycoord = r*Math.sin(theta)*Math.sin(phi); zcoord = r*Math.cos(theta); default: System.out.println("ERROR: Units of angle undefined, please select 'd' for degrees or 'r' for radians."); } } // Methods to print out contents to screen and file public void print() { System.out.println(xcoord + "\t" + ycoord + "\t" + zcoord); } public void printtoFile() { String filename = "output_3vector.txt"; try { PrintWriter out = new PrintWriter(new FileWriter(filename)); out.println(xcoord + "\t" + ycoord + "\t" + zcoord); out.close(); } catch(IOException e) { System.out.println("Unable to open file " + filename); } } //Methods to return the cartesian coordinates of the vector public double getX() { return xcoord; } public double getY() { return ycoord; } public double getZ() { return zcoord; } //Methods to return spherical polar coordinate angles of the vector //Example call: v1.getTheta(Type.d); - d for degrees, r for radians public double getTheta(Type type) { switch (type){ case d: return Math.toDegrees(Math.acos(zcoord/mag3())); case r: return Math.acos(zcoord/mag3()); default: System.out.println("ERROR: Units of angle undefined, please select 'd' for degrees or 'r' for radians."); return 0; } } //Example call: v1.getPhi(Type.d); - d for degrees, r for radians public double getPhi(Type type) { switch (type){ case d: return Math.toDegrees(Math.atan(ycoord/xcoord)); case r: return Math.atan2(ycoord,xcoord); //do not use atan, must take into account range of input variables default: System.out.println("ERROR: Units of angle undefined, please select 'd' for degrees or 'r' for radians."); return 0; } } //Method to return the square of the vector public double square3() { return xcoord*xcoord + ycoord*ycoord + zcoord*zcoord; } //Method to return the magnitude of the vector public double mag3() { return Math.sqrt(square3()); } //Methods to alter cartesian values of coordinates public void setX(double x_new) { xcoord = x_new; } public void setY(double y_new) { ycoord = y_new; } public void setZ(double z_new) { zcoord = z_new; } //Method to calculate the dot product of two vectors public double dot(Threevector v_other) { return v_other.getX()*xcoord + v_other.getY()*ycoord + v_other.getZ()*zcoord; } //Method to calculate the cross product of two vectors public Threevector cross(Threevector v_other) { return new Threevector(ycoord*v_other.getZ() - zcoord*v_other.getY(),zcoord*v_other.getX() - xcoord*v_other.getZ(),xcoord*v_other.getY() - ycoord*v_other.getX()); } //Method to scale a vector public void scale(double scale) { xcoord *= scale; ycoord *= scale; zcoord *= scale; } //Methods to combine vectors public Threevector add(Threevector v_other) { return new Threevector(v_other.getX() + xcoord,v_other.getY() + ycoord,v_other.getZ() + zcoord); } public void increment(Threevector v_other) { xcoord += v_other.getX(); ycoord += v_other.getY(); zcoord += v_other.getZ(); } public Threevector subtract(Threevector v_other) { return new Threevector(xcoord - v_other.getX(),ycoord - v_other.getY(),zcoord - v_other.getZ()); } };