Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Vector {
- // Variables
- double x;
- double y;
- double z;
- ArrayList <Double> myVector;
- // Constructors
- Vector(){
- x = 0;
- y = 0;
- z = 0;
- myVector = new ArrayList <Double> ();
- myVector.add(x);
- myVector.add(y);
- myVector.add(z);
- }
- Vector(double x, double y, double z){
- this.x = x;
- this.y = y;
- this.z = z;
- myVector = new ArrayList <Double> ();
- myVector.add(x);
- myVector.add(y);
- myVector.add(z);
- }
- // Methods
- // 1st Method
- Vector add(Vector v) {
- // First create the 3 appropriate variables
- double newX = x + v.x;
- double newY = y + v.y;
- double newZ = z + v.z;
- Vector result = new Vector(newX, newY, newZ);
- return result;
- }
- // 2nd Method
- Vector subtract(Vector v) {
- // First create the 3 appropriate variables
- double newX = x - v.x;
- double newY = y - v.y;
- double newZ = z - v.z;
- Vector result = new Vector(newX, newY, newZ);
- return result;
- }
- // 3rd Method
- boolean isEqualTo(Vector v) {
- if(x == v.x && y == v.y && z == v.z && myVector == v.myVector) {
- return true;
- }
- return false;
- }
- // 4th method
- Vector cross(Vector v) {
- double newX = x * v.x;
- double newY = y * v.y;
- double newZ = z * v.z;
- return new Vector(newX, newY, newZ);
- }
- // 5th method
- double product(Vector v) {
- double sum = x * v.x + y * v.y + z * v.z;
- return sum;
- }
- // 6th method
- String convertToString() {
- return ("<" + x + ", " + y + ", " + z + ">");
- }
- // MAIN METHOD
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- // Initializations
- Vector a = new Vector();
- Vector b = new Vector(1, 2, 3);
- Vector c = new Vector(4, 5, 6);
- // Displays
- System.out.println("a = " + a.myVector);
- System.out.println("b = " + b.myVector);
- System.out.println("c = " + c.myVector);
- System.out.println();
- // 1. To check the function "add" - OK
- Vector aPlusB = a.add(b);
- Vector bPlusC = b.add(c);
- System.out.println("1. Checking add");
- System.out.println("a + b = " + aPlusB.myVector);
- System.out.println("b + c = " + bPlusC.myVector);
- System.out.println();
- // 2. To check the function "subtract" - OK
- Vector aMinusB = a.subtract(b);
- Vector bMinusC = b.subtract(c);
- System.out.println("2. Checking subtract");
- System.out.println("a - b = " + aMinusB.myVector);
- System.out.println("b - c = " + bMinusC.myVector);
- System.out.println();
- // 3. To check the function "isEqualTo" - OK
- boolean result1 = a.isEqualTo(b);
- boolean result2 = b.isEqualTo(c);
- boolean result3 = a.isEqualTo(a);
- System.out.println("3. Checking equality or not");
- System.out.println(result1 + " " + result2 + " " + result3);
- System.out.println();
- // 4. To check the function "cross"
- Vector aTimesB = a.cross(b);
- Vector bTimesC = b.cross(c);
- System.out.println("4. Checking cross product");
- System.out.println("a * b = " + aTimesB.myVector);
- System.out.println("b * c = " + bTimesC.myVector);
- System.out.println();
- // 5. To check the function "product"
- System.out.println("Checking product");
- double product1 = a.product(b);
- double product2 = b.product(c);
- System.out.println("a * b = " + product1);
- System.out.println("b * c = " + product2);
- System.out.println();
- // 6. To check the function "convertToString"
- String stringEquivalent1 = a.convertToString();
- String stringEquivalent2 = b.convertToString();
- String stringEquivalent3 = c.convertToString();
- System.out.println("a = " + stringEquivalent1);
- System.out.println("b = " + stringEquivalent2);
- System.out.println("c = " + stringEquivalent3);
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement