Advertisement
makispaiktis

Vector Class

Oct 29th, 2019 (edited)
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Vector {
  4.  
  5.     // Variables
  6.     double x;
  7.     double y;
  8.     double z;
  9.     ArrayList <Double> myVector;
  10.    
  11.     // Constructors
  12.     Vector(){
  13.         x = 0;
  14.         y = 0;
  15.         z = 0;
  16.         myVector = new ArrayList <Double> ();
  17.         myVector.add(x);
  18.         myVector.add(y);
  19.         myVector.add(z);
  20.     }
  21.    
  22.     Vector(double x, double y, double z){
  23.         this.x = x;
  24.         this.y = y;
  25.         this.z = z;
  26.         myVector = new ArrayList <Double> ();
  27.         myVector.add(x);
  28.         myVector.add(y);
  29.         myVector.add(z);
  30.     }
  31.    
  32.    
  33.    
  34.     // Methods
  35.     // 1st Method
  36.     Vector add(Vector v) {
  37.         // First create the 3 appropriate variables
  38.         double newX = x + v.x;
  39.         double newY = y + v.y;
  40.         double newZ = z + v.z;
  41.         Vector result = new Vector(newX, newY, newZ);
  42.         return result;
  43.     }
  44.    
  45.     // 2nd Method
  46.     Vector subtract(Vector v) {
  47.         // First create the 3 appropriate variables
  48.         double newX = x - v.x;
  49.         double newY = y - v.y;
  50.         double newZ = z - v.z;
  51.         Vector result = new Vector(newX, newY, newZ);
  52.         return result;
  53.     }
  54.    
  55.     // 3rd Method
  56.     boolean isEqualTo(Vector v) {
  57.         if(x == v.x && y == v.y && z == v.z && myVector == v.myVector) {
  58.             return true;
  59.         }
  60.         return false;
  61.     }
  62.    
  63.    
  64.     // 4th method
  65.     Vector cross(Vector v) {
  66.         double newX = x * v.x;
  67.         double newY = y * v.y;
  68.         double newZ = z * v.z;
  69.         return new Vector(newX, newY, newZ);
  70.     }
  71.    
  72.     // 5th method
  73.     double product(Vector v) {
  74.         double sum = x * v.x + y * v.y + z * v.z;
  75.         return sum;
  76.     }
  77.    
  78.     // 6th method
  79.     String convertToString() {
  80.         return ("<" + x + ", " + y + ", " + z + ">");
  81.     }
  82.    
  83.    
  84.    
  85.     // MAIN METHOD
  86.     public static void main(String[] args) {
  87.         // TODO Auto-generated method stub
  88.        
  89.         // Initializations
  90.         Vector a = new Vector();
  91.         Vector b = new Vector(1, 2, 3);
  92.         Vector c = new Vector(4, 5, 6);
  93.        
  94.         // Displays
  95.         System.out.println("a = " + a.myVector);
  96.         System.out.println("b = " + b.myVector);
  97.         System.out.println("c = " + c.myVector);
  98.         System.out.println();
  99.        
  100.         // 1. To check the function "add" - OK
  101.         Vector aPlusB = a.add(b);
  102.         Vector bPlusC = b.add(c);
  103.         System.out.println("1. Checking add");
  104.         System.out.println("a + b = " + aPlusB.myVector);
  105.         System.out.println("b + c = " + bPlusC.myVector);
  106.         System.out.println();
  107.        
  108.         // 2. To check the function "subtract" - OK
  109.         Vector aMinusB = a.subtract(b);
  110.         Vector bMinusC = b.subtract(c);
  111.         System.out.println("2. Checking subtract");
  112.         System.out.println("a - b = " + aMinusB.myVector);
  113.         System.out.println("b - c = " + bMinusC.myVector);
  114.         System.out.println();
  115.        
  116.         // 3. To check the function "isEqualTo" - OK
  117.         boolean result1 = a.isEqualTo(b);
  118.         boolean result2 = b.isEqualTo(c);
  119.         boolean result3 = a.isEqualTo(a);
  120.         System.out.println("3. Checking equality or not");
  121.         System.out.println(result1 + "  " + result2 + "  " + result3);
  122.         System.out.println();
  123.        
  124.         // 4. To check the function "cross"
  125.         Vector aTimesB = a.cross(b);
  126.         Vector bTimesC = b.cross(c);
  127.         System.out.println("4. Checking cross product");
  128.         System.out.println("a * b = " + aTimesB.myVector);
  129.         System.out.println("b * c = " + bTimesC.myVector);
  130.         System.out.println();
  131.        
  132.         // 5. To check the function "product"
  133.         System.out.println("Checking product");
  134.         double product1 = a.product(b);
  135.         double product2 = b.product(c);
  136.         System.out.println("a * b = " + product1);
  137.         System.out.println("b * c = " + product2);
  138.         System.out.println();
  139.        
  140.         // 6. To check the function "convertToString"
  141.         String stringEquivalent1 = a.convertToString();
  142.         String stringEquivalent2 = b.convertToString();
  143.         String stringEquivalent3 = c.convertToString();
  144.         System.out.println("a = " + stringEquivalent1);
  145.         System.out.println("b = " + stringEquivalent2);
  146.         System.out.println("c = " + stringEquivalent3);
  147.         System.out.println();
  148.        
  149.     }
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement