From 94bd528509989ae1d74b06b8e0890c4f145b14dd Mon Sep 17 00:00:00 2001 From: fefe <hofer.francois.01@gmail.com> Date: Fri, 11 Oct 2019 11:09:51 +0200 Subject: [PATCH] Functions: Add,od,constructors,len --- src/main/java/ch/hepia/numeric/Vector.java | 23 +++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main/java/ch/hepia/numeric/Vector.java b/src/main/java/ch/hepia/numeric/Vector.java index d210742..0a4613a 100644 --- a/src/main/java/ch/hepia/numeric/Vector.java +++ b/src/main/java/ch/hepia/numeric/Vector.java @@ -1,16 +1,21 @@ package ch.hepia.numeric; import java.util.List; +import java.util.ArrayList; import java.util.function.DoubleFunction; import java.util.function.Function; final public class Vector { + private List<Double> values; + private Vector() {} private Vector(Double... elements) { - throw new UnsupportedOperationException("This feature isn't implemented yet"); + List<Double> vec = new ArrayList<>(); + for (double elem : elements){vec.add(elem);} + this.values = vec; } private Vector(List<Double> elements) { - throw new UnsupportedOperationException("This feature isn't implemented yet"); + this.values = elements; } public Transposed t() { @@ -18,18 +23,22 @@ final public class Vector { } public int len() { - throw new UnsupportedOperationException("This feature isn't implemented yet"); + return this.values.size(); } public double get(int i) { - throw new UnsupportedOperationException("This feature isn't implemented yet"); + return this.values.get(i); } void set(int i, double value) { - throw new UnsupportedOperationException("This feature isn't implemented yet"); + this.values.set(i, value); } public Vector add(Vector that) { - throw new UnsupportedOperationException("This feature isn't implemented yet"); + List<Double> newVec = new ArrayList<>(); + for(int i=0; i < this.len();i++){ + newVec.add(this.get(i)+that.get(i)); + } + return new Vector(newVec); } public Vector mul(double m) { @@ -77,7 +86,7 @@ final public class Vector { } public static Vector of(Double... elements) { - throw new UnsupportedOperationException("This feature isn't implemented yet"); + return new Vector(elements); } public static Vector of(List<Double> elements) { -- GitLab