Advertisement
KoMeDiAnT

Vector Value

Nov 19th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 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 final int length;
  9.     private int[] data;    
  10.    
  11.     public VectorValue(int... data) {
  12.         super();
  13.        
  14.         length = data.length;
  15.         this.data = new int[length];
  16.        
  17.         for (int i = 0; i < length; i++) {
  18.             this.data[i] = data[i];
  19.         }
  20.     }
  21.    
  22.     public int length() {
  23.         return length;
  24.     }
  25.  
  26.     @Override
  27.     public AbstractValue add(AbstractValue operand) throws OperationNotSupportedException {
  28.         if (this.length() != ((VectorValue) operand).length()) {
  29.             throw new IllegalArgumentException("Dimesions disagree");
  30.         }
  31.        
  32.         int[] data = new int[length];
  33.        
  34.         for (int i = 0; i < length; i ++) {
  35.             data[i] = this.data[i] + ((VectorValue) operand).data[i];
  36.         }
  37.        
  38.         return new VectorValue(data);
  39.     }
  40.  
  41.     @Override
  42.     public AbstractValue sub(AbstractValue operand) throws OperationNotSupportedException {
  43.         if (this.length() != ((VectorValue) operand).length()) {
  44.             throw new IllegalArgumentException("Dimesions disagree");
  45.         }
  46.        
  47.         int[] data = new int[length];
  48.        
  49.         for (int i = 0; i < length; i ++) {
  50.             data[i] = this.data[i] - ((VectorValue) operand).data[i];
  51.         }
  52.        
  53.         return new VectorValue(data);
  54.     }
  55.  
  56.     @Override
  57.     public AbstractValue mul(AbstractValue operand) throws OperationNotSupportedException {
  58.         throw new OperationNotSupportedException("mul");
  59.     }
  60.  
  61.     @Override
  62.     public AbstractValue div(AbstractValue operand) throws DivisionByZeroException, OperationNotSupportedException {
  63.         throw new OperationNotSupportedException("div");
  64.     }
  65.  
  66.     @Override
  67.     public String toString() {
  68.         StringBuilder builder = new StringBuilder("Vector(");
  69.        
  70.         for (int i = 0; i < length; i++) {
  71.             builder.append(data[i]);
  72.            
  73.             if (i < length - 1) {
  74.                 builder.append(", ");
  75.             }
  76.         }
  77.        
  78.         builder.append(')');
  79.  
  80.         return builder.toString();
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement