From 78e9b2209a1d4ef4dfa745e1c568ec30e9ee1792 Mon Sep 17 00:00:00 2001 From: CodeMashine Date: Tue, 26 May 2026 19:33:11 +0300 Subject: [PATCH 1/5] div : overrite some scalar methods --- src/com/javarush/module2/lesson02/calc/TypeSwitcher.java | 4 ++++ src/com/javarush/module2/lesson02/calc/VariableTypes.java | 7 +++++++ 2 files changed, 11 insertions(+) create mode 100644 src/com/javarush/module2/lesson02/calc/TypeSwitcher.java create mode 100644 src/com/javarush/module2/lesson02/calc/VariableTypes.java diff --git a/src/com/javarush/module2/lesson02/calc/TypeSwitcher.java b/src/com/javarush/module2/lesson02/calc/TypeSwitcher.java new file mode 100644 index 0000000..1ecc0ff --- /dev/null +++ b/src/com/javarush/module2/lesson02/calc/TypeSwitcher.java @@ -0,0 +1,4 @@ +package com.javarush.module2.lesson02.calc; + +public class TypeSwitcher { +} diff --git a/src/com/javarush/module2/lesson02/calc/VariableTypes.java b/src/com/javarush/module2/lesson02/calc/VariableTypes.java new file mode 100644 index 0000000..7dd6901 --- /dev/null +++ b/src/com/javarush/module2/lesson02/calc/VariableTypes.java @@ -0,0 +1,7 @@ +package com.javarush.module2.lesson02.calc.api; + +public enum VariableTypes { + SCALAR , + VECTOR, + MATRIX, +} From b31f0ccb384fa9e91744b743989ae53dcfa8c451 Mon Sep 17 00:00:00 2001 From: CodeMashine Date: Wed, 27 May 2026 17:33:52 +0300 Subject: [PATCH 2/5] dev: add variableTypes --- .../module2/lesson02/calc/MathRunner.java | 38 +++++++++--- .../module2/lesson02/calc/Operation.java | 10 ---- .../module2/lesson02/calc/Scalar.java | 59 ------------------- .../module2/lesson02/calc/TypeSwitcher.java | 4 -- .../module2/lesson02/calc/VariableTypes.java | 7 --- .../module2/lesson02/calc/api/Add.java | 2 +- .../module2/lesson02/calc/api/Div.java | 2 +- .../module2/lesson02/calc/api/Mul.java | 2 +- .../module2/lesson02/calc/api/Operation.java | 5 ++ .../module2/lesson02/calc/api/Sub.java | 2 +- .../calc/{ => exception}/CalcException.java | 2 +- .../module2/lesson02/calc/types/Scalar.java | 55 +++++++++++++++++ .../lesson02/calc/{ => types}/Var.java | 5 +- .../lesson02/calc/types/VariableTypes.java | 39 ++++++++++++ .../lesson02/calc/{ => types}/Vector.java | 27 +++++---- 15 files changed, 153 insertions(+), 106 deletions(-) delete mode 100644 src/com/javarush/module2/lesson02/calc/Operation.java delete mode 100644 src/com/javarush/module2/lesson02/calc/Scalar.java delete mode 100644 src/com/javarush/module2/lesson02/calc/TypeSwitcher.java delete mode 100644 src/com/javarush/module2/lesson02/calc/VariableTypes.java create mode 100644 src/com/javarush/module2/lesson02/calc/api/Operation.java rename src/com/javarush/module2/lesson02/calc/{ => exception}/CalcException.java (86%) create mode 100644 src/com/javarush/module2/lesson02/calc/types/Scalar.java rename src/com/javarush/module2/lesson02/calc/{ => types}/Var.java (78%) create mode 100644 src/com/javarush/module2/lesson02/calc/types/VariableTypes.java rename src/com/javarush/module2/lesson02/calc/{ => types}/Vector.java (62%) diff --git a/src/com/javarush/module2/lesson02/calc/MathRunner.java b/src/com/javarush/module2/lesson02/calc/MathRunner.java index cda1201..aabe7ed 100644 --- a/src/com/javarush/module2/lesson02/calc/MathRunner.java +++ b/src/com/javarush/module2/lesson02/calc/MathRunner.java @@ -1,11 +1,35 @@ package com.javarush.module2.lesson02.calc; +import com.javarush.module2.lesson02.calc.types.Scalar; +import com.javarush.module2.lesson02.calc.types.Var; +import com.javarush.module2.lesson02.calc.types.VariableTypes; +import com.javarush.module2.lesson02.calc.types.Vector; + public class MathRunner { - public static void main(String[] args) { - Var left=new Scalar(3); - Var right=new Vector("[1,2,3,4]"); -// Var right=new Scalar(2); - Var result = right.add(right); - System.out.println(result); - } + public static void main(String[] args) { + Var left = VariableTypes.getVar(args[ 0 ]); + String operation = args[ 1 ]; + Var right = VariableTypes.getVar(args[ 2 ]); + + Var result = null; + switch ( operation ) { + case "+": + result = left.add(right); + break; + case "-": + result = left.sub(right); + break; + case "*": + result = left.mul(right); + break; + case "/": + result = left.div(right); + break; + default: + System.out.println("Invalid operation"); + break; + } + + System.out.println(args + " = " + result); + } } diff --git a/src/com/javarush/module2/lesson02/calc/Operation.java b/src/com/javarush/module2/lesson02/calc/Operation.java deleted file mode 100644 index c238b82..0000000 --- a/src/com/javarush/module2/lesson02/calc/Operation.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.javarush.module2.lesson02.calc; - -import com.javarush.module2.lesson02.calc.api.Add; -import com.javarush.module2.lesson02.calc.api.Div; -import com.javarush.module2.lesson02.calc.api.Mul; -import com.javarush.module2.lesson02.calc.api.Sub; - -public interface Operation extends Add, Sub, Mul, Div { - -} diff --git a/src/com/javarush/module2/lesson02/calc/Scalar.java b/src/com/javarush/module2/lesson02/calc/Scalar.java deleted file mode 100644 index 8c64ce2..0000000 --- a/src/com/javarush/module2/lesson02/calc/Scalar.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.javarush.module2.lesson02.calc; - -public class Scalar extends Var { - - public double getValue() { - return value; - } - - private double value; - - public Scalar(double value) { - this.value = value; - } - - public Scalar(String value) { - this.value = Double.parseDouble(value); - } - - public Scalar(Scalar other) { - this.value = other.value; - } - - @Override - public Var add(Var other) { - if (other instanceof Scalar scalar) { - return new Scalar(this.value + scalar.value); - } - return other.add(this); - } - - @Override - public Var sub(Var other) { - if (other instanceof Scalar scalar) { - return new Scalar(this.value - scalar.value); - } - return other.sub(this).mul(new Scalar(-1)); - } - - @Override - public Var mul(Var other) { - if (other instanceof Scalar scalar) { - return new Scalar(this.value * scalar.value); - } - return other.mul(this); - } - - @Override - public Var div(Var other) { - if (other instanceof Scalar scalar) { - return new Scalar(this.value / scalar.value); - } - return super.div(other); - } - - @Override - public String toString() { - return String.valueOf(value); - } -} diff --git a/src/com/javarush/module2/lesson02/calc/TypeSwitcher.java b/src/com/javarush/module2/lesson02/calc/TypeSwitcher.java deleted file mode 100644 index 1ecc0ff..0000000 --- a/src/com/javarush/module2/lesson02/calc/TypeSwitcher.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.javarush.module2.lesson02.calc; - -public class TypeSwitcher { -} diff --git a/src/com/javarush/module2/lesson02/calc/VariableTypes.java b/src/com/javarush/module2/lesson02/calc/VariableTypes.java deleted file mode 100644 index 7dd6901..0000000 --- a/src/com/javarush/module2/lesson02/calc/VariableTypes.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.javarush.module2.lesson02.calc.api; - -public enum VariableTypes { - SCALAR , - VECTOR, - MATRIX, -} diff --git a/src/com/javarush/module2/lesson02/calc/api/Add.java b/src/com/javarush/module2/lesson02/calc/api/Add.java index b93706d..28870ee 100644 --- a/src/com/javarush/module2/lesson02/calc/api/Add.java +++ b/src/com/javarush/module2/lesson02/calc/api/Add.java @@ -1,6 +1,6 @@ package com.javarush.module2.lesson02.calc.api; -import com.javarush.module2.lesson02.calc.Var; +import com.javarush.module2.lesson02.calc.types.Var; public interface Add { Var add(Var other); diff --git a/src/com/javarush/module2/lesson02/calc/api/Div.java b/src/com/javarush/module2/lesson02/calc/api/Div.java index 4d4f042..24efc09 100644 --- a/src/com/javarush/module2/lesson02/calc/api/Div.java +++ b/src/com/javarush/module2/lesson02/calc/api/Div.java @@ -1,6 +1,6 @@ package com.javarush.module2.lesson02.calc.api; -import com.javarush.module2.lesson02.calc.Var; +import com.javarush.module2.lesson02.calc.types.Var; public interface Div { Var div(Var other); diff --git a/src/com/javarush/module2/lesson02/calc/api/Mul.java b/src/com/javarush/module2/lesson02/calc/api/Mul.java index 04a585a..b550e35 100644 --- a/src/com/javarush/module2/lesson02/calc/api/Mul.java +++ b/src/com/javarush/module2/lesson02/calc/api/Mul.java @@ -1,6 +1,6 @@ package com.javarush.module2.lesson02.calc.api; -import com.javarush.module2.lesson02.calc.Var; +import com.javarush.module2.lesson02.calc.types.Var; public interface Mul { Var mul(Var other); diff --git a/src/com/javarush/module2/lesson02/calc/api/Operation.java b/src/com/javarush/module2/lesson02/calc/api/Operation.java new file mode 100644 index 0000000..fa6e1b3 --- /dev/null +++ b/src/com/javarush/module2/lesson02/calc/api/Operation.java @@ -0,0 +1,5 @@ +package com.javarush.module2.lesson02.calc.api; + +public interface Operation extends Add, Sub, Mul, Div { + +} diff --git a/src/com/javarush/module2/lesson02/calc/api/Sub.java b/src/com/javarush/module2/lesson02/calc/api/Sub.java index 51672dc..fe29d95 100644 --- a/src/com/javarush/module2/lesson02/calc/api/Sub.java +++ b/src/com/javarush/module2/lesson02/calc/api/Sub.java @@ -1,6 +1,6 @@ package com.javarush.module2.lesson02.calc.api; -import com.javarush.module2.lesson02.calc.Var; +import com.javarush.module2.lesson02.calc.types.Var; public interface Sub { Var sub(Var other); diff --git a/src/com/javarush/module2/lesson02/calc/CalcException.java b/src/com/javarush/module2/lesson02/calc/exception/CalcException.java similarity index 86% rename from src/com/javarush/module2/lesson02/calc/CalcException.java rename to src/com/javarush/module2/lesson02/calc/exception/CalcException.java index 543f16b..fc3c1d6 100644 --- a/src/com/javarush/module2/lesson02/calc/CalcException.java +++ b/src/com/javarush/module2/lesson02/calc/exception/CalcException.java @@ -1,4 +1,4 @@ -package com.javarush.module2.lesson02.calc; +package com.javarush.module2.lesson02.calc.exception; public class CalcException extends RuntimeException { diff --git a/src/com/javarush/module2/lesson02/calc/types/Scalar.java b/src/com/javarush/module2/lesson02/calc/types/Scalar.java new file mode 100644 index 0000000..272d9b3 --- /dev/null +++ b/src/com/javarush/module2/lesson02/calc/types/Scalar.java @@ -0,0 +1,55 @@ +package com.javarush.module2.lesson02.calc.types; + +import com.javarush.module2.lesson02.calc.exception.CalcException; + +public class Scalar extends Var { + + public double getValue() { + return value; + } + + private double value; + + public Scalar(double value) { + this.value = value; + } + + public Scalar(String value) { + this.value = Double.parseDouble(value); + } + + public Scalar(Scalar other) { + this.value = other.value; + } + + + public Var add(Scalar other) { + return new Scalar(this.value + other.value); + } + + public Var add(Vector other) { + return other.add(this); + } + + + public Var sub(Scalar other) { + return new Scalar(this.value - other.value); + } + + + public Var mul(Scalar other) { + return new Scalar(this.value * other.value); + } + + public Var div(Scalar other) { + if ( other.value == 0 || this.value == 0 ) { + throw new CalcException(new ArithmeticException("Division by zero")); + } + return new Scalar(this.value / other.value); + } + + @Override + public String toString() { + return String.valueOf(value); + } +} diff --git a/src/com/javarush/module2/lesson02/calc/Var.java b/src/com/javarush/module2/lesson02/calc/types/Var.java similarity index 78% rename from src/com/javarush/module2/lesson02/calc/Var.java rename to src/com/javarush/module2/lesson02/calc/types/Var.java index ca808d4..c81ff50 100644 --- a/src/com/javarush/module2/lesson02/calc/Var.java +++ b/src/com/javarush/module2/lesson02/calc/types/Var.java @@ -1,4 +1,7 @@ -package com.javarush.module2.lesson02.calc; +package com.javarush.module2.lesson02.calc.types; + +import com.javarush.module2.lesson02.calc.api.Operation; +import com.javarush.module2.lesson02.calc.exception.CalcException; public abstract class Var implements Operation { diff --git a/src/com/javarush/module2/lesson02/calc/types/VariableTypes.java b/src/com/javarush/module2/lesson02/calc/types/VariableTypes.java new file mode 100644 index 0000000..78e1399 --- /dev/null +++ b/src/com/javarush/module2/lesson02/calc/types/VariableTypes.java @@ -0,0 +1,39 @@ +package com.javarush.module2.lesson02.calc.types; + +import com.javarush.module2.lesson02.calc.exception.CalcException; + +import java.lang.reflect.Constructor; + +public enum VariableTypes { + SCALAR("^-?\\d+(\\.\\d+)?$", Scalar.class), + VECTOR("^\\[\\d+(,\\d+)*\\]$", Vector.class), + ; +// MATRIX(M), + + private String regexp; + private Class type; + + private VariableTypes(String regexp, Class clazz) { + this.regexp = regexp; + this.type = clazz; + } + + private Var getInstance(String string) { + try { + Constructor constructor = type.getConstructor(String.class); + return constructor.newInstance(string); + } catch ( Exception e ) { + throw new CalcException(e); + } + } + + public static Var getVar(String string) { + for ( VariableTypes var : VariableTypes.values() ) { + if ( var.regexp.equals(string) ) { + return var.getInstance(string); + } + } + throw new CalcException("No such variable: " + string); + } + +} diff --git a/src/com/javarush/module2/lesson02/calc/Vector.java b/src/com/javarush/module2/lesson02/calc/types/Vector.java similarity index 62% rename from src/com/javarush/module2/lesson02/calc/Vector.java rename to src/com/javarush/module2/lesson02/calc/types/Vector.java index 4c45b48..b65baf1 100644 --- a/src/com/javarush/module2/lesson02/calc/Vector.java +++ b/src/com/javarush/module2/lesson02/calc/types/Vector.java @@ -1,4 +1,6 @@ -package com.javarush.module2.lesson02.calc; +package com.javarush.module2.lesson02.calc.types; + +import com.javarush.module2.lesson02.calc.exception.CalcException; import java.util.Arrays; @@ -25,24 +27,23 @@ public Vector(String strVectorValues) { } } - @Override - public Var add(Var other) { - if (other instanceof Scalar scalar) { - double[] result = this.values.clone(); - for (int i = 0; i < result.length; i++) { - result[i] += scalar.getValue(); - } - return new Vector(result); - } else if (other instanceof Vector vector) { - if (this.values.length == vector.values.length) { + public Var add(Vector other) { + if (this.values.length == other.values.length) { double[] result = this.values.clone(); for (int i = 0; i < result.length; i++) { - result[i] += vector.values[i]; + result[i] += other.values[i]; } return new Vector(result); } + throw new CalcException("Vectors do not have the same length"); + } + + public Var add(Scalar value) { + double[] result = this.values.clone(); + for (int i = 0; i < result.length; i++) { + result[i] += value.getValue(); } - return super.add(other); + return new Vector(result); } @Override From d89150734e71a61a3f608a52721dd1ffc6a8575f Mon Sep 17 00:00:00 2001 From: CodeMashine Date: Wed, 27 May 2026 17:48:33 +0300 Subject: [PATCH 3/5] dev : add Scalar, Vector methods --- .../module2/lesson02/calc/types/Scalar.java | 2 - .../module2/lesson02/calc/types/Vector.java | 139 ++++++++++++------ 2 files changed, 95 insertions(+), 46 deletions(-) diff --git a/src/com/javarush/module2/lesson02/calc/types/Scalar.java b/src/com/javarush/module2/lesson02/calc/types/Scalar.java index 272d9b3..95b9231 100644 --- a/src/com/javarush/module2/lesson02/calc/types/Scalar.java +++ b/src/com/javarush/module2/lesson02/calc/types/Scalar.java @@ -31,12 +31,10 @@ public Var add(Vector other) { return other.add(this); } - public Var sub(Scalar other) { return new Scalar(this.value - other.value); } - public Var mul(Scalar other) { return new Scalar(this.value * other.value); } diff --git a/src/com/javarush/module2/lesson02/calc/types/Vector.java b/src/com/javarush/module2/lesson02/calc/types/Vector.java index b65baf1..953bd4b 100644 --- a/src/com/javarush/module2/lesson02/calc/types/Vector.java +++ b/src/com/javarush/module2/lesson02/calc/types/Vector.java @@ -6,48 +6,99 @@ public class Vector extends Var { - private final double[] values; - - public Vector(double[] values) { - this.values = values.clone(); - } - - public Vector(Vector vector) { - this.values = vector.values.clone(); - } - - public Vector(String strVectorValues) { - String[] parts = strVectorValues.replaceAll("\\s+", "") - .replace("[", "") - .replace("]", "") - .split(","); - values = new double[parts.length]; - for (int i = 0; i < parts.length; i++) { - values[i] = Double.parseDouble(parts[i]); - } - } - - public Var add(Vector other) { - if (this.values.length == other.values.length) { - double[] result = this.values.clone(); - for (int i = 0; i < result.length; i++) { - result[i] += other.values[i]; - } - return new Vector(result); - } - throw new CalcException("Vectors do not have the same length"); - } - - public Var add(Scalar value) { - double[] result = this.values.clone(); - for (int i = 0; i < result.length; i++) { - result[i] += value.getValue(); - } - return new Vector(result); - } - - @Override - public String toString() { - return Arrays.toString(values); - } + private final double[] values; + + public Vector(double[] values) { + this.values = values.clone(); + } + + public Vector(Vector vector) { + this.values = vector.values.clone(); + } + + public Vector(String strVectorValues) { + String[] parts = strVectorValues.replaceAll("\\s+", "") + .replace("[", "") + .replace("]", "") + .split(","); + values = new double[ parts.length ]; + for ( int i = 0; i < parts.length; i++ ) { + values[ i ] = Double.parseDouble(parts[ i ]); + } + } + + public Var add(Vector other) { + checkOtherVector(other); + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] += other.values[ i ]; + } + return new Vector(result); + } + + public Var add(Scalar value) { + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] += value.getValue(); + } + return new Vector(result); + } + + public Var sub(Vector other) { + checkOtherVector(other); + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] -= other.values[ i ]; + } + return new Vector(result); + } + + public Var sub(Scalar value) { + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] -= value.getValue(); + } + return new Vector(result); + } + + public Var mul(Vector other) { + checkOtherVector(other); + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] *= other.values[ i ]; + } + return new Vector(result); + } + + public Var mul(Scalar value) { + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] *= value.getValue(); + } + return new Vector(result); + } + + public Var div(Scalar other) { + if(other.getValue() == 0 ){ + throw new CalcException(new ArithmeticException("Division by zero")); + } + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] /= other.getValue(); + } + return new Vector(result); + } + + + private void checkOtherVector(Vector other) { + if ( this.values.length == other.values.length ) { + throw new CalcException("Vectors do not have the same length"); + } + } + + + @Override + public String toString() { + return Arrays.toString(values); + } } From 619fa3795615cb613247e19aa1bc71b7c664275f Mon Sep 17 00:00:00 2001 From: CodeMashine Date: Wed, 27 May 2026 18:42:50 +0300 Subject: [PATCH 4/5] dev: add VectorMatrixTemplate --- .../module2/lesson02/calc/MathRunner.java | 2 - .../module2/lesson02/calc/types/Matrix.java | 15 +++ .../module2/lesson02/calc/types/Scalar.java | 2 +- .../lesson02/calc/types/VariableTypes.java | 7 +- .../module2/lesson02/calc/types/Vector.java | 101 +--------------- .../calc/types/VectorMatrixTemplate.java | 110 ++++++++++++++++++ .../lesson02/calc/types/VectorOld.java | 104 +++++++++++++++++ 7 files changed, 241 insertions(+), 100 deletions(-) create mode 100644 src/com/javarush/module2/lesson02/calc/types/Matrix.java create mode 100644 src/com/javarush/module2/lesson02/calc/types/VectorMatrixTemplate.java create mode 100644 src/com/javarush/module2/lesson02/calc/types/VectorOld.java diff --git a/src/com/javarush/module2/lesson02/calc/MathRunner.java b/src/com/javarush/module2/lesson02/calc/MathRunner.java index aabe7ed..0fa296a 100644 --- a/src/com/javarush/module2/lesson02/calc/MathRunner.java +++ b/src/com/javarush/module2/lesson02/calc/MathRunner.java @@ -1,9 +1,7 @@ package com.javarush.module2.lesson02.calc; -import com.javarush.module2.lesson02.calc.types.Scalar; import com.javarush.module2.lesson02.calc.types.Var; import com.javarush.module2.lesson02.calc.types.VariableTypes; -import com.javarush.module2.lesson02.calc.types.Vector; public class MathRunner { public static void main(String[] args) { diff --git a/src/com/javarush/module2/lesson02/calc/types/Matrix.java b/src/com/javarush/module2/lesson02/calc/types/Matrix.java new file mode 100644 index 0000000..61abf75 --- /dev/null +++ b/src/com/javarush/module2/lesson02/calc/types/Matrix.java @@ -0,0 +1,15 @@ +package com.javarush.module2.lesson02.calc.types; + +public class Matrix extends VectorMatrixTemplate{ + public Matrix(String strVectorValues) { + super(strVectorValues); + } + + public Matrix(double[] values) { + super(values); + } + + public Matrix(Matrix vector) { + super(vector); + } +} diff --git a/src/com/javarush/module2/lesson02/calc/types/Scalar.java b/src/com/javarush/module2/lesson02/calc/types/Scalar.java index 95b9231..a5951bb 100644 --- a/src/com/javarush/module2/lesson02/calc/types/Scalar.java +++ b/src/com/javarush/module2/lesson02/calc/types/Scalar.java @@ -27,7 +27,7 @@ public Var add(Scalar other) { return new Scalar(this.value + other.value); } - public Var add(Vector other) { + public Var add(VectorOld other) { return other.add(this); } diff --git a/src/com/javarush/module2/lesson02/calc/types/VariableTypes.java b/src/com/javarush/module2/lesson02/calc/types/VariableTypes.java index 78e1399..3f3cc6f 100644 --- a/src/com/javarush/module2/lesson02/calc/types/VariableTypes.java +++ b/src/com/javarush/module2/lesson02/calc/types/VariableTypes.java @@ -5,11 +5,14 @@ import java.lang.reflect.Constructor; public enum VariableTypes { - SCALAR("^-?\\d+(\\.\\d+)?$", Scalar.class), - VECTOR("^\\[\\d+(,\\d+)*\\]$", Vector.class), + SCALAR("-?\\d+(\\.\\d+)?", Scalar.class), + VECTOR("\\[\\d+(,\\d+)*\\]", Vector.class), + MATRIX("\\[+\\d+(,\\d+)*\\]+", Matrix.class), ; // MATRIX(M), +// [[1,2,3],[4,5,6],[7,8,9]] + private String regexp; private Class type; diff --git a/src/com/javarush/module2/lesson02/calc/types/Vector.java b/src/com/javarush/module2/lesson02/calc/types/Vector.java index 953bd4b..5bce2f0 100644 --- a/src/com/javarush/module2/lesson02/calc/types/Vector.java +++ b/src/com/javarush/module2/lesson02/calc/types/Vector.java @@ -1,104 +1,15 @@ package com.javarush.module2.lesson02.calc.types; -import com.javarush.module2.lesson02.calc.exception.CalcException; - -import java.util.Arrays; - -public class Vector extends Var { - - private final double[] values; - - public Vector(double[] values) { - this.values = values.clone(); - } - - public Vector(Vector vector) { - this.values = vector.values.clone(); - } - +public class Vector extends VectorMatrixTemplate { public Vector(String strVectorValues) { - String[] parts = strVectorValues.replaceAll("\\s+", "") - .replace("[", "") - .replace("]", "") - .split(","); - values = new double[ parts.length ]; - for ( int i = 0; i < parts.length; i++ ) { - values[ i ] = Double.parseDouble(parts[ i ]); - } - } - - public Var add(Vector other) { - checkOtherVector(other); - double[] result = this.values.clone(); - for ( int i = 0; i < result.length; i++ ) { - result[ i ] += other.values[ i ]; - } - return new Vector(result); - } - - public Var add(Scalar value) { - double[] result = this.values.clone(); - for ( int i = 0; i < result.length; i++ ) { - result[ i ] += value.getValue(); - } - return new Vector(result); + super(strVectorValues); } - public Var sub(Vector other) { - checkOtherVector(other); - double[] result = this.values.clone(); - for ( int i = 0; i < result.length; i++ ) { - result[ i ] -= other.values[ i ]; - } - return new Vector(result); - } - - public Var sub(Scalar value) { - double[] result = this.values.clone(); - for ( int i = 0; i < result.length; i++ ) { - result[ i ] -= value.getValue(); - } - return new Vector(result); - } - - public Var mul(Vector other) { - checkOtherVector(other); - double[] result = this.values.clone(); - for ( int i = 0; i < result.length; i++ ) { - result[ i ] *= other.values[ i ]; - } - return new Vector(result); - } - - public Var mul(Scalar value) { - double[] result = this.values.clone(); - for ( int i = 0; i < result.length; i++ ) { - result[ i ] *= value.getValue(); - } - return new Vector(result); - } - - public Var div(Scalar other) { - if(other.getValue() == 0 ){ - throw new CalcException(new ArithmeticException("Division by zero")); - } - double[] result = this.values.clone(); - for ( int i = 0; i < result.length; i++ ) { - result[ i ] /= other.getValue(); - } - return new Vector(result); - } - - - private void checkOtherVector(Vector other) { - if ( this.values.length == other.values.length ) { - throw new CalcException("Vectors do not have the same length"); - } + public Vector(double[] values) { + super(values); } - - @Override - public String toString() { - return Arrays.toString(values); + public Vector(Vector vector) { + super(vector); } } diff --git a/src/com/javarush/module2/lesson02/calc/types/VectorMatrixTemplate.java b/src/com/javarush/module2/lesson02/calc/types/VectorMatrixTemplate.java new file mode 100644 index 0000000..9e15d59 --- /dev/null +++ b/src/com/javarush/module2/lesson02/calc/types/VectorMatrixTemplate.java @@ -0,0 +1,110 @@ +package com.javarush.module2.lesson02.calc.types; + +import com.javarush.module2.lesson02.calc.exception.CalcException; + +import java.util.Arrays; + +public class VectorMatrixTemplate extends Var { + + private final double[] values; + + public VectorMatrixTemplate (double[] values) { + this.values = values.clone(); + } + + public VectorMatrixTemplate (T vector) { + this.values = vector.getValues(); + } + + public VectorMatrixTemplate (String strVectorValues) { + String[] parts = strVectorValues.replaceAll("\\s+", "") + .replace("[", "") + .replace("]", "") + .split(","); + values = new double[ parts.length ]; + for ( int i = 0; i < parts.length; i++ ) { + values[ i ] = Double.parseDouble(parts[ i ]); + } + } + + + public double[] getValues() { + return values.clone(); + } + + public Var add(T other) { + checkOtherVectorOrMatrix(other); + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] += other.getValues()[ i ]; + } + return new VectorOld(result); + } + + public Var add(Scalar value) { + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] += value.getValue(); + } + return new VectorOld(result); + } + + public Var sub(T other) { + checkOtherVectorOrMatrix(other); + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] -= other.getValues()[ i ]; + } + return new VectorOld(result); + } + + public Var sub(Scalar value) { + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] -= value.getValue(); + } + return new VectorOld(result); + } + + public Var mul(T other) { + checkOtherVectorOrMatrix(other); + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] *= other.getValues()[ i ]; + } + return new VectorOld(result); + } + + public Var mul(Scalar value) { + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] *= value.getValue(); + } + return new VectorOld(result); + } + + public Var div(Scalar other) { + if(other.getValue() == 0 ){ + throw new CalcException(new ArithmeticException("Division by zero")); + } + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] /= other.getValue(); + } + return new VectorOld(result); + } + + + private void checkOtherVectorOrMatrix(T other) { + if ( this.values.length == other.getValues().length ) { + throw new CalcException("Vectors do not have the same length"); + } + } + + + @Override + public String toString() { + return Arrays.toString(values); + } +} + diff --git a/src/com/javarush/module2/lesson02/calc/types/VectorOld.java b/src/com/javarush/module2/lesson02/calc/types/VectorOld.java new file mode 100644 index 0000000..df41640 --- /dev/null +++ b/src/com/javarush/module2/lesson02/calc/types/VectorOld.java @@ -0,0 +1,104 @@ +package com.javarush.module2.lesson02.calc.types; + +import com.javarush.module2.lesson02.calc.exception.CalcException; + +import java.util.Arrays; + +public class VectorOld extends Var { + + private final double[] values; + + public VectorOld(double[] values) { + this.values = values.clone(); + } + + public VectorOld(VectorOld vector) { + this.values = vector.values.clone(); + } + + public VectorOld(String strVectorValues) { + String[] parts = strVectorValues.replaceAll("\\s+", "") + .replace("[", "") + .replace("]", "") + .split(","); + values = new double[ parts.length ]; + for ( int i = 0; i < parts.length; i++ ) { + values[ i ] = Double.parseDouble(parts[ i ]); + } + } + + public Var add(VectorOld other) { + checkOtherVector(other); + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] += other.values[ i ]; + } + return new VectorOld(result); + } + + public Var add(Scalar value) { + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] += value.getValue(); + } + return new VectorOld(result); + } + + public Var sub(VectorOld other) { + checkOtherVector(other); + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] -= other.values[ i ]; + } + return new VectorOld(result); + } + + public Var sub(Scalar value) { + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] -= value.getValue(); + } + return new VectorOld(result); + } + + public Var mul(VectorOld other) { + checkOtherVector(other); + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] *= other.values[ i ]; + } + return new VectorOld(result); + } + + public Var mul(Scalar value) { + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] *= value.getValue(); + } + return new VectorOld(result); + } + + public Var div(Scalar other) { + if(other.getValue() == 0 ){ + throw new CalcException(new ArithmeticException("Division by zero")); + } + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] /= other.getValue(); + } + return new VectorOld(result); + } + + + private void checkOtherVector(VectorOld other) { + if ( this.values.length == other.values.length ) { + throw new CalcException("Vectors do not have the same length"); + } + } + + + @Override + public String toString() { + return Arrays.toString(values); + } +} From d363f8201b8aafcb411b75d198cea29982a51438 Mon Sep 17 00:00:00 2001 From: CodeMashine Date: Thu, 28 May 2026 21:34:58 +0300 Subject: [PATCH 5/5] dev: finish --- .../module2/lesson02/calc/MathRunner.java | 13 +- .../module2/lesson02/calc/types/Matrix.java | 217 +++++++++++++++++- .../module2/lesson02/calc/types/Scalar.java | 95 +++++++- .../module2/lesson02/calc/types/Var.java | 32 +-- .../lesson02/calc/types/VariableTypes.java | 12 +- .../module2/lesson02/calc/types/Vector.java | 147 +++++++++++- .../calc/types/VectorMatrixTemplate.java | 110 --------- .../lesson02/calc/types/VectorOld.java | 104 --------- 8 files changed, 468 insertions(+), 262 deletions(-) delete mode 100644 src/com/javarush/module2/lesson02/calc/types/VectorMatrixTemplate.java delete mode 100644 src/com/javarush/module2/lesson02/calc/types/VectorOld.java diff --git a/src/com/javarush/module2/lesson02/calc/MathRunner.java b/src/com/javarush/module2/lesson02/calc/MathRunner.java index 0fa296a..28adcef 100644 --- a/src/com/javarush/module2/lesson02/calc/MathRunner.java +++ b/src/com/javarush/module2/lesson02/calc/MathRunner.java @@ -5,9 +5,10 @@ public class MathRunner { public static void main(String[] args) { - Var left = VariableTypes.getVar(args[ 0 ]); - String operation = args[ 1 ]; - Var right = VariableTypes.getVar(args[ 2 ]); + String[] arguments = {"[[1,2,3],[4,5,6]]","*","[1,2,3]"}; + Var left = VariableTypes.getVar(arguments[ 0 ]); + String operation = arguments[ 1 ]; + Var right = VariableTypes.getVar(arguments[ 2 ]); Var result = null; switch ( operation ) { @@ -28,6 +29,10 @@ public static void main(String[] args) { break; } - System.out.println(args + " = " + result); + for ( int i = 0; i < arguments.length; i++ ) { + System.out.print(arguments[i] + " "); + } + + System.out.println("= " + result); } } diff --git a/src/com/javarush/module2/lesson02/calc/types/Matrix.java b/src/com/javarush/module2/lesson02/calc/types/Matrix.java index 61abf75..64efd7f 100644 --- a/src/com/javarush/module2/lesson02/calc/types/Matrix.java +++ b/src/com/javarush/module2/lesson02/calc/types/Matrix.java @@ -1,15 +1,218 @@ package com.javarush.module2.lesson02.calc.types; -public class Matrix extends VectorMatrixTemplate{ - public Matrix(String strVectorValues) { - super(strVectorValues); +import com.javarush.module2.lesson02.calc.exception.CalcException; + +import java.util.Arrays; + +public class Matrix extends Var { + + private final double[][] values; + private int rows; + private int cols; + + public int getCols() { + return cols; + } + + public int getRows() { + return rows; + } + + public Matrix(String strMatrixValues) { + String mat = strMatrixValues; + mat = mat.substring(2, mat.length() - 2); + String[] parts = mat.replaceAll("\\s+", "") + .split("],\\["); + int col = parts[ 0 ].split(",").length; + int row = parts.length; + double[][] matrixValue = new double[ row ][ col ]; + for ( int i = 0; i < row; i++ ) { + String[] rowValue = parts[ i ].split(","); + for ( int j = 0; j < col; j++ ) { + matrixValue[ i ][ j ] = Double.parseDouble(rowValue[ j ]); + } + } + this.rows = row; + this.cols = col; + this.values = matrixValue; + } + + public Matrix(double[][] values) { + this.values = values.clone(); + this.rows = values.length; + this.cols = values[0].length; + } + + public Matrix(Matrix matrix) { + this.values = matrix.getValues(); + } + + public double[][] getValues() { + double[][] clone = new double[ rows ][ cols ]; + for ( int i = 0; i < rows; i++ ) { + for ( int j = 0; j < cols; j++ ) { + clone[ i ][ j ] = values[ i ][ j ]; + } + } + return clone; + } + + @Override + public Var add(Var other) { + if (other instanceof Scalar) { + return this.add((Scalar) other); + } + if (other instanceof Matrix) { + return this.add((Matrix) other); + } + return super.add(other); + } + + public Var add(Matrix other) { + checkOtherMatrix(other); + double[][] result = this.getValues(); + double[][] otherValue = other.getValues(); + for ( int i = 0; i < result.length; i++ ) { + for ( int j = 0; j < result[ i ].length; j++ ) { + result[ i ][ j ] += otherValue[ i ][ j ]; + } + } + return new Matrix(result); + } + + public Var add(Scalar value) { + double[][] result = this.getValues(); + double otherValue = value.getValue(); + for ( int i = 0; i < result.length; i++ ) { + for ( int j = 0; j < result[ i ].length; j++ ) { + result[ i ][ j ] += otherValue; + } + } + return new Matrix(result); + } + + @Override + public Var sub(Var other) { + if (other instanceof Scalar) { + return this.sub((Scalar) other); + } + if (other instanceof Matrix) { + return this.sub((Matrix) other); + } + return super.sub(other); + } + + public Var sub(Matrix other) { + checkOtherMatrix(other); + double[][] result = this.getValues(); + double[][] otherValue = other.getValues(); + for ( int i = 0; i < result.length; i++ ) { + for ( int j = 0; j < result[ i ].length; j++ ) { + result[ i ][ j ] -= otherValue[ i ][ j ]; + } + } + return new Matrix(result); + } + + public Var sub(Scalar value) { + double[][] result = this.values.clone(); + double otherValue = value.getValue(); + for ( int i = 0; i < result.length; i++ ) { + for ( int j = 0; j < result[ i ].length; j++ ) { + result[ i ][ j ] -= otherValue; + } + } + return new Matrix(result); + } + + @Override + public Var mul(Var other) { + if (other instanceof Scalar) { + return this.mul((Scalar) other); + } + if (other instanceof Matrix) { + return this.mul((Matrix) other); + } + if (other instanceof Vector) { + return this.mul((Vector) other); + } + return super.mul(other); + } + + public Var mul(Vector other) { + if (this.cols != other.getValues().length ) { + throw new CalcException(new IllegalArgumentException("vector not equal to matrix side")); + } + double[] vectorValue = other.getValues(); + double[][] result = this.getValues(); + for ( int i = 0; i < rows; i++ ) { + for ( int j = 0; j < cols; j++ ) { + result[i][j] *= vectorValue[j]; + } + } + return new Matrix(result); + } + + public Var mul(Matrix other) { + checkOtherMatrix(other); + double[][] result = this.getValues(); + double[][] otherValues = other.getValues(); + for ( int i = 0; i < result.length; i++ ) { + for ( int j = 0; j < result[ i ].length; j++ ) { + result[ i ][ j ] *= otherValues[ i ][ j ]; + } + } + return new Matrix(result); + } + + public Var mul(Scalar value) { + double[][] result = this.getValues(); + double otherValue = value.getValue(); + for ( int i = 0; i < result.length; i++ ) { + for ( int j = 0; j < result[ i ].length; j++ ) { + result[ i ][ j ] *= otherValue; + } + } + return new Matrix(result); + } + + @Override + public Var div(Var other) { + if (other instanceof Scalar) { + return this.div((Scalar) other); + } + return super.div(other); + } + + public Var div(Scalar other) { + double otherValue = other.getValue(); + if ( otherValue == 0 ) { + throw new CalcException(new ArithmeticException("Division by zero")); + } + double[][] result = this.getValues(); + for ( int i = 0; i < result.length; i++ ) { + for ( int j = 0; j < result[ i ].length; j++ ) { + result[ i ][ j ] /= other.getValue(); + } + } + return new Matrix(result); } - public Matrix(double[] values) { - super(values); + private void checkOtherMatrix(Matrix other) { + if ( rows != other.rows || cols != other.cols ) { + throw new CalcException(new ArithmeticException("Matrix dimensions mismatch")); + } } - public Matrix(Matrix vector) { - super(vector); + @Override + public String toString() { + StringBuilder out = new StringBuilder(); + out.append( "[ "); + for ( int i = 0; i < rows; i++ ) { + out.append( Arrays.toString( values[ i ] ) ); + out.append( " " ); + } + out.append( "]" ); + return out.toString(); } } diff --git a/src/com/javarush/module2/lesson02/calc/types/Scalar.java b/src/com/javarush/module2/lesson02/calc/types/Scalar.java index a5951bb..b7ce35d 100644 --- a/src/com/javarush/module2/lesson02/calc/types/Scalar.java +++ b/src/com/javarush/module2/lesson02/calc/types/Scalar.java @@ -3,42 +3,125 @@ import com.javarush.module2.lesson02.calc.exception.CalcException; public class Scalar extends Var { - public double getValue() { return value; } - private double value; - public Scalar(double value) { this.value = value; } - public Scalar(String value) { this.value = Double.parseDouble(value); } - public Scalar(Scalar other) { this.value = other.value; } + @Override + public Var add(Var other) { + if (other instanceof Scalar) { + return this.add((Scalar) other); + } + if (other instanceof Vector) { + return this.add((Vector) other); + } + if(other instanceof Matrix) { + return this.add((Matrix) other); + } + + return super.add(other); + } public Var add(Scalar other) { return new Scalar(this.value + other.value); } - public Var add(VectorOld other) { + public Var add(Vector other) { return other.add(this); } + public Var add(Matrix other) { + return other.add(this); + } + + @Override + public Var sub(Var other) { + if (other instanceof Scalar) { + return this.sub((Scalar) other); + } + if (other instanceof Vector) { + return this.sub((Vector) other); + } + if(other instanceof Matrix) { + return this.sub((Matrix) other); + } + return super.sub(other); + } + public Var sub(Scalar other) { return new Scalar(this.value - other.value); } + public Var sub(Vector other) { + double[] vectorValues = other.getValues(); + for (int i = 0; i < vectorValues.length; i++) { + vectorValues[i] = -vectorValues[i]; + } + Vector reverseVector = new Vector(vectorValues); + return reverseVector.add(this); + } + + public Var sub(Matrix other) { + double[][] matrixValues = other.getValues(); + double[][] result = new double[other.getRows()][other.getCols()]; + for (int i = 0; i < matrixValues.length; i++) { + Vector vector = new Vector(matrixValues[i]); + Vector matrixLine = (Vector)this.sub(vector); + result[i] = matrixLine.getValues(); + } + return new Matrix(result); + } + + @Override + public Var mul(Var other) { + if (other instanceof Scalar) { + return this.mul((Scalar) other); + } + if (other instanceof Vector) { + return this.mul((Vector) other); + } + if(other instanceof Matrix) { + return this.mul((Matrix) other); + } + return super.sub(other); + } + public Var mul(Scalar other) { return new Scalar(this.value * other.value); } + public Var mul(Vector other) { + return other.mul(this); + } + + public Var mul(Matrix other) { + double[][] matrixValues = other.getValues(); + for (int i = 0; i < matrixValues.length; i++) { + Vector vector = new Vector(matrixValues[i]); + Vector matrixLine = (Vector)this.mul(vector); + matrixValues[i] = matrixLine.getValues(); + } + return new Matrix(matrixValues); + } + + @Override + public Var div(Var other) { + if (other instanceof Scalar) { + return this.div((Scalar) other); + } + return super.sub(other); + } + public Var div(Scalar other) { if ( other.value == 0 || this.value == 0 ) { throw new CalcException(new ArithmeticException("Division by zero")); diff --git a/src/com/javarush/module2/lesson02/calc/types/Var.java b/src/com/javarush/module2/lesson02/calc/types/Var.java index c81ff50..7073140 100644 --- a/src/com/javarush/module2/lesson02/calc/types/Var.java +++ b/src/com/javarush/module2/lesson02/calc/types/Var.java @@ -5,23 +5,23 @@ public abstract class Var implements Operation { - @Override - public Var add(Var other) { - throw new CalcException("Operation %s + %s is not allowed".formatted(this,other)); - } + @Override + public Var add(Var other) { + throw new CalcException("Operation %s + %s is not allowed".formatted(this, other)); + } - @Override - public Var sub(Var other) { - throw new CalcException("Operation %s - %s is not allowed".formatted(this,other)); - } + @Override + public Var sub(Var other) { + throw new CalcException("Operation %s - %s is not allowed".formatted(this, other)); + } - @Override - public Var mul(Var other) { - throw new CalcException("Operation %s * %s is not allowed".formatted(this,other)); - } + @Override + public Var mul(Var other) { + throw new CalcException("Operation %s * %s is not allowed".formatted(this, other)); + } - @Override - public Var div(Var other) { - throw new CalcException("Operation %s / %s is not allowed".formatted(this,other)); - } + @Override + public Var div(Var other) { + throw new CalcException("Operation %s / %s is not allowed".formatted(this, other)); + } } diff --git a/src/com/javarush/module2/lesson02/calc/types/VariableTypes.java b/src/com/javarush/module2/lesson02/calc/types/VariableTypes.java index 3f3cc6f..753bcf3 100644 --- a/src/com/javarush/module2/lesson02/calc/types/VariableTypes.java +++ b/src/com/javarush/module2/lesson02/calc/types/VariableTypes.java @@ -5,13 +5,9 @@ import java.lang.reflect.Constructor; public enum VariableTypes { - SCALAR("-?\\d+(\\.\\d+)?", Scalar.class), - VECTOR("\\[\\d+(,\\d+)*\\]", Vector.class), - MATRIX("\\[+\\d+(,\\d+)*\\]+", Matrix.class), - ; -// MATRIX(M), - -// [[1,2,3],[4,5,6],[7,8,9]] + SCALAR("^-?\\d+(\\.\\d+)?$", Scalar.class), + VECTOR("^\\[\\d+(,\\d+)*\\]$", Vector.class), + MATRIX("^\\[((\\[\\d+(,\\d)*\\])+,*)*\\]$", Matrix.class); private String regexp; private Class type; @@ -32,7 +28,7 @@ private Var getInstance(String string) { public static Var getVar(String string) { for ( VariableTypes var : VariableTypes.values() ) { - if ( var.regexp.equals(string) ) { + if ( string.matches(var.regexp) ) { return var.getInstance(string); } } diff --git a/src/com/javarush/module2/lesson02/calc/types/Vector.java b/src/com/javarush/module2/lesson02/calc/types/Vector.java index 5bce2f0..60741a5 100644 --- a/src/com/javarush/module2/lesson02/calc/types/Vector.java +++ b/src/com/javarush/module2/lesson02/calc/types/Vector.java @@ -1,15 +1,148 @@ package com.javarush.module2.lesson02.calc.types; -public class Vector extends VectorMatrixTemplate { - public Vector(String strVectorValues) { - super(strVectorValues); - } +import com.javarush.module2.lesson02.calc.exception.CalcException; + +import java.util.Arrays; + +public class Vector extends Var { - public Vector(double[] values) { - super(values); + private final double[] values; + + public Vector (double[] values) { + this.values = values.clone(); } public Vector(Vector vector) { - super(vector); + this.values = vector.getValues(); + } + + public Vector (String strVectorValues) { + String[] parts = strVectorValues.replaceAll("\\s+", "") + .replace("[", "") + .replace("]", "") + .split(","); + values = new double[ parts.length ]; + for ( int i = 0; i < parts.length; i++ ) { + values[ i ] = Double.parseDouble(parts[ i ]); + } + } + + public double[] getValues() { + return values.clone(); + } + + @Override + public Var add(Var other) { + if (other instanceof Scalar) { + return this.add((Scalar) other); + } + if (other instanceof Vector) { + return this.add((Vector) other); + } + return super.add(other); + } + + public Var add(Vector other) { + checkOtherVector(other); + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] += other.getValues()[ i ]; + } + return new Vector(result); + } + + public Var add(Scalar value) { + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] += value.getValue(); + } + return new Vector(result); + } + + @Override + public Var sub(Var other) { + if (other instanceof Scalar) { + return this.sub((Scalar) other); + } + if (other instanceof Vector) { + return this.sub((Vector) other); + } + return super.add(other); + } + + public Var sub(Vector other) { + checkOtherVector(other); + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] -= other.getValues()[ i ]; + } + return new Vector(result); + } + + public Var sub(Scalar value) { + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] -= value.getValue(); + } + return new Vector(result); + } + + @Override + public Var mul(Var other) { + if (other instanceof Scalar) { + return this.mul((Scalar) other); + } + if (other instanceof Vector) { + return this.mul((Vector) other); + } + return super.mul(other); + } + + public Var mul(Vector other) { + checkOtherVector(other); + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] *= other.getValues()[ i ]; + } + return new Vector(result); + } + + public Var mul(Scalar value) { + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] *= value.getValue(); + } + return new Vector(result); + } + + @Override + public Var div(Var other) { + if (other instanceof Scalar) { + return this.div((Scalar) other); + } + return super.div(other); + } + + public Var div(Scalar other) { + if(other.getValue() == 0 ){ + throw new CalcException(new ArithmeticException("Division by zero")); + } + double[] result = this.values.clone(); + for ( int i = 0; i < result.length; i++ ) { + result[ i ] /= other.getValue(); + } + return new Vector(result); + } + + private void checkOtherVector(Vector other) { + if ( this.values.length == other.getValues().length ) { + throw new CalcException("Vectors do not have the same length"); + } + } + + @Override + public String toString() { + return Arrays.toString(values); } } + diff --git a/src/com/javarush/module2/lesson02/calc/types/VectorMatrixTemplate.java b/src/com/javarush/module2/lesson02/calc/types/VectorMatrixTemplate.java deleted file mode 100644 index 9e15d59..0000000 --- a/src/com/javarush/module2/lesson02/calc/types/VectorMatrixTemplate.java +++ /dev/null @@ -1,110 +0,0 @@ -package com.javarush.module2.lesson02.calc.types; - -import com.javarush.module2.lesson02.calc.exception.CalcException; - -import java.util.Arrays; - -public class VectorMatrixTemplate extends Var { - - private final double[] values; - - public VectorMatrixTemplate (double[] values) { - this.values = values.clone(); - } - - public VectorMatrixTemplate (T vector) { - this.values = vector.getValues(); - } - - public VectorMatrixTemplate (String strVectorValues) { - String[] parts = strVectorValues.replaceAll("\\s+", "") - .replace("[", "") - .replace("]", "") - .split(","); - values = new double[ parts.length ]; - for ( int i = 0; i < parts.length; i++ ) { - values[ i ] = Double.parseDouble(parts[ i ]); - } - } - - - public double[] getValues() { - return values.clone(); - } - - public Var add(T other) { - checkOtherVectorOrMatrix(other); - double[] result = this.values.clone(); - for ( int i = 0; i < result.length; i++ ) { - result[ i ] += other.getValues()[ i ]; - } - return new VectorOld(result); - } - - public Var add(Scalar value) { - double[] result = this.values.clone(); - for ( int i = 0; i < result.length; i++ ) { - result[ i ] += value.getValue(); - } - return new VectorOld(result); - } - - public Var sub(T other) { - checkOtherVectorOrMatrix(other); - double[] result = this.values.clone(); - for ( int i = 0; i < result.length; i++ ) { - result[ i ] -= other.getValues()[ i ]; - } - return new VectorOld(result); - } - - public Var sub(Scalar value) { - double[] result = this.values.clone(); - for ( int i = 0; i < result.length; i++ ) { - result[ i ] -= value.getValue(); - } - return new VectorOld(result); - } - - public Var mul(T other) { - checkOtherVectorOrMatrix(other); - double[] result = this.values.clone(); - for ( int i = 0; i < result.length; i++ ) { - result[ i ] *= other.getValues()[ i ]; - } - return new VectorOld(result); - } - - public Var mul(Scalar value) { - double[] result = this.values.clone(); - for ( int i = 0; i < result.length; i++ ) { - result[ i ] *= value.getValue(); - } - return new VectorOld(result); - } - - public Var div(Scalar other) { - if(other.getValue() == 0 ){ - throw new CalcException(new ArithmeticException("Division by zero")); - } - double[] result = this.values.clone(); - for ( int i = 0; i < result.length; i++ ) { - result[ i ] /= other.getValue(); - } - return new VectorOld(result); - } - - - private void checkOtherVectorOrMatrix(T other) { - if ( this.values.length == other.getValues().length ) { - throw new CalcException("Vectors do not have the same length"); - } - } - - - @Override - public String toString() { - return Arrays.toString(values); - } -} - diff --git a/src/com/javarush/module2/lesson02/calc/types/VectorOld.java b/src/com/javarush/module2/lesson02/calc/types/VectorOld.java deleted file mode 100644 index df41640..0000000 --- a/src/com/javarush/module2/lesson02/calc/types/VectorOld.java +++ /dev/null @@ -1,104 +0,0 @@ -package com.javarush.module2.lesson02.calc.types; - -import com.javarush.module2.lesson02.calc.exception.CalcException; - -import java.util.Arrays; - -public class VectorOld extends Var { - - private final double[] values; - - public VectorOld(double[] values) { - this.values = values.clone(); - } - - public VectorOld(VectorOld vector) { - this.values = vector.values.clone(); - } - - public VectorOld(String strVectorValues) { - String[] parts = strVectorValues.replaceAll("\\s+", "") - .replace("[", "") - .replace("]", "") - .split(","); - values = new double[ parts.length ]; - for ( int i = 0; i < parts.length; i++ ) { - values[ i ] = Double.parseDouble(parts[ i ]); - } - } - - public Var add(VectorOld other) { - checkOtherVector(other); - double[] result = this.values.clone(); - for ( int i = 0; i < result.length; i++ ) { - result[ i ] += other.values[ i ]; - } - return new VectorOld(result); - } - - public Var add(Scalar value) { - double[] result = this.values.clone(); - for ( int i = 0; i < result.length; i++ ) { - result[ i ] += value.getValue(); - } - return new VectorOld(result); - } - - public Var sub(VectorOld other) { - checkOtherVector(other); - double[] result = this.values.clone(); - for ( int i = 0; i < result.length; i++ ) { - result[ i ] -= other.values[ i ]; - } - return new VectorOld(result); - } - - public Var sub(Scalar value) { - double[] result = this.values.clone(); - for ( int i = 0; i < result.length; i++ ) { - result[ i ] -= value.getValue(); - } - return new VectorOld(result); - } - - public Var mul(VectorOld other) { - checkOtherVector(other); - double[] result = this.values.clone(); - for ( int i = 0; i < result.length; i++ ) { - result[ i ] *= other.values[ i ]; - } - return new VectorOld(result); - } - - public Var mul(Scalar value) { - double[] result = this.values.clone(); - for ( int i = 0; i < result.length; i++ ) { - result[ i ] *= value.getValue(); - } - return new VectorOld(result); - } - - public Var div(Scalar other) { - if(other.getValue() == 0 ){ - throw new CalcException(new ArithmeticException("Division by zero")); - } - double[] result = this.values.clone(); - for ( int i = 0; i < result.length; i++ ) { - result[ i ] /= other.getValue(); - } - return new VectorOld(result); - } - - - private void checkOtherVector(VectorOld other) { - if ( this.values.length == other.values.length ) { - throw new CalcException("Vectors do not have the same length"); - } - } - - - @Override - public String toString() { - return Arrays.toString(values); - } -}