Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package calculator.datatypes.vector;
- import calculator.AbstractValue;
- import calculator.DivisionByZeroException;
- import calculator.OperationNotSupportedException;
- public class VectorValue extends AbstractValue {
- private final int length;
- private int[] data;
- public VectorValue(int... data) {
- super();
- length = data.length;
- this.data = new int[length];
- for (int i = 0; i < length; i++) {
- this.data[i] = data[i];
- }
- }
- public int length() {
- return length;
- }
- @Override
- public AbstractValue add(AbstractValue operand) throws OperationNotSupportedException {
- if (this.length() != ((VectorValue) operand).length()) {
- throw new IllegalArgumentException("Dimesions disagree");
- }
- int[] data = new int[length];
- for (int i = 0; i < length; i ++) {
- data[i] = this.data[i] + ((VectorValue) operand).data[i];
- }
- return new VectorValue(data);
- }
- @Override
- public AbstractValue sub(AbstractValue operand) throws OperationNotSupportedException {
- if (this.length() != ((VectorValue) operand).length()) {
- throw new IllegalArgumentException("Dimesions disagree");
- }
- int[] data = new int[length];
- for (int i = 0; i < length; i ++) {
- data[i] = this.data[i] - ((VectorValue) operand).data[i];
- }
- return new VectorValue(data);
- }
- @Override
- public AbstractValue mul(AbstractValue operand) throws OperationNotSupportedException {
- throw new OperationNotSupportedException("mul");
- }
- @Override
- public AbstractValue div(AbstractValue operand) throws DivisionByZeroException, OperationNotSupportedException {
- throw new OperationNotSupportedException("div");
- }
- @Override
- public String toString() {
- StringBuilder builder = new StringBuilder("Vector(");
- for (int i = 0; i < length; i++) {
- builder.append(data[i]);
- if (i < length - 1) {
- builder.append(", ");
- }
- }
- builder.append(')');
- return builder.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement