diff --git a/src/com/javarush/module2/lesson02/calc/MathRunner.java b/src/com/javarush/module2/lesson02/calc/MathRunner.java index cda1201..28adcef 100644 --- a/src/com/javarush/module2/lesson02/calc/MathRunner.java +++ b/src/com/javarush/module2/lesson02/calc/MathRunner.java @@ -1,11 +1,38 @@ package com.javarush.module2.lesson02.calc; +import com.javarush.module2.lesson02.calc.types.Var; +import com.javarush.module2.lesson02.calc.types.VariableTypes; + 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) { + 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 ) { + 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; + } + + 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/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/Var.java b/src/com/javarush/module2/lesson02/calc/Var.java deleted file mode 100644 index ca808d4..0000000 --- a/src/com/javarush/module2/lesson02/calc/Var.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.javarush.module2.lesson02.calc; - -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 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 div(Var other) { - throw new CalcException("Operation %s / %s is not allowed".formatted(this,other)); - } -} diff --git a/src/com/javarush/module2/lesson02/calc/Vector.java b/src/com/javarush/module2/lesson02/calc/Vector.java deleted file mode 100644 index 4c45b48..0000000 --- a/src/com/javarush/module2/lesson02/calc/Vector.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.javarush.module2.lesson02.calc; - -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 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]); - } - } - - @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) { - double[] result = this.values.clone(); - for (int i = 0; i < result.length; i++) { - result[i] += vector.values[i]; - } - return new Vector(result); - } - } - return super.add(other); - } - - @Override - public String toString() { - return Arrays.toString(values); - } -} 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/Matrix.java b/src/com/javarush/module2/lesson02/calc/types/Matrix.java new file mode 100644 index 0000000..64efd7f --- /dev/null +++ b/src/com/javarush/module2/lesson02/calc/types/Matrix.java @@ -0,0 +1,218 @@ +package com.javarush.module2.lesson02.calc.types; + +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); + } + + private void checkOtherMatrix(Matrix other) { + if ( rows != other.rows || cols != other.cols ) { + throw new CalcException(new ArithmeticException("Matrix dimensions mismatch")); + } + } + + @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 new file mode 100644 index 0000000..b7ce35d --- /dev/null +++ b/src/com/javarush/module2/lesson02/calc/types/Scalar.java @@ -0,0 +1,136 @@ +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; + } + + @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(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")); + } + return new Scalar(this.value / other.value); + } + + @Override + public String toString() { + return String.valueOf(value); + } +} diff --git a/src/com/javarush/module2/lesson02/calc/types/Var.java b/src/com/javarush/module2/lesson02/calc/types/Var.java new file mode 100644 index 0000000..7073140 --- /dev/null +++ b/src/com/javarush/module2/lesson02/calc/types/Var.java @@ -0,0 +1,27 @@ +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 { + + @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 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)); + } +} 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..753bcf3 --- /dev/null +++ b/src/com/javarush/module2/lesson02/calc/types/VariableTypes.java @@ -0,0 +1,38 @@ +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("^\\[((\\[\\d+(,\\d)*\\])+,*)*\\]$", Matrix.class); + + 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 ( string.matches(var.regexp) ) { + return var.getInstance(string); + } + } + throw new CalcException("No such variable: " + string); + } + +} diff --git a/src/com/javarush/module2/lesson02/calc/types/Vector.java b/src/com/javarush/module2/lesson02/calc/types/Vector.java new file mode 100644 index 0000000..60741a5 --- /dev/null +++ b/src/com/javarush/module2/lesson02/calc/types/Vector.java @@ -0,0 +1,148 @@ +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.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); + } +} +