Advertisement
KoMeDiAnT

Vector3D Value

Nov 20th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. package calculator.datatypes.vector;
  2.  
  3. import calculator.AbstractValue;
  4. import calculator.DivisionByZeroException;
  5. import calculator.OperationNotSupportedException;
  6.  
  7. public class VectorValue extends AbstractValue {
  8.     private int x;
  9.     private int y;
  10.     private int z;
  11.    
  12.     public VectorValue(int x, int y, int z) {
  13.         super();
  14.        
  15.         this.x = x;
  16.         this.y = y;
  17.         this.z = z;
  18.     }
  19.  
  20.     @Override
  21.     public AbstractValue add(AbstractValue operand) throws OperationNotSupportedException {
  22.         int x = this.x + ((VectorValue) operand).x;
  23.         int y = this.y + ((VectorValue) operand).y;
  24.         int z = this.z + ((VectorValue) operand).z;
  25.        
  26.         return new VectorValue(x, y, z);
  27.     }
  28.  
  29.     @Override
  30.     public AbstractValue sub(AbstractValue operand) throws OperationNotSupportedException {
  31.         int x = this.x - ((VectorValue) operand).x;
  32.         int y = this.y - ((VectorValue) operand).y;
  33.         int z = this.z - ((VectorValue) operand).z;
  34.        
  35.         return new VectorValue(x, y, z);
  36.     }
  37.  
  38.     @Override
  39.     public AbstractValue mul(AbstractValue operand) throws OperationNotSupportedException {
  40.         int i = this.y * ((VectorValue) operand).z - this.z * ((VectorValue) operand).y;
  41.         int j = -(this.x * ((VectorValue) operand).z - this.z * ((VectorValue) operand).x);
  42.         int k = this.x * ((VectorValue) operand).y - this.y * ((VectorValue) operand).x;
  43.        
  44.         return new VectorValue(i, j, k);
  45.     }
  46.  
  47.     @Override
  48.     public AbstractValue div(AbstractValue operand) throws DivisionByZeroException, OperationNotSupportedException {
  49.         throw new OperationNotSupportedException("div");
  50.     }
  51.  
  52.     @Override
  53.     public String toString() {
  54.         StringBuilder builder = new StringBuilder("Vector3D(");
  55.         builder.append(this.x + ", ");
  56.         builder.append(this.y + ", ");
  57.         builder.append(this.z + ")");
  58.  
  59.         return builder.toString();
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement