From 760d4a90133dca4601dba92998fc5a614f407014 Mon Sep 17 00:00:00 2001 From: pozut141k <154245225+pozut141k@users.noreply.github.com> Date: Wed, 11 Dec 2024 00:13:44 +0200 Subject: [PATCH 1/4] Create Main.java --- project_by_mister_brazhnyk/src/Main.java | 31 ++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 project_by_mister_brazhnyk/src/Main.java diff --git a/project_by_mister_brazhnyk/src/Main.java b/project_by_mister_brazhnyk/src/Main.java new file mode 100644 index 0000000..8266934 --- /dev/null +++ b/project_by_mister_brazhnyk/src/Main.java @@ -0,0 +1,31 @@ +import java.util.List; +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.println("Choose input mode: 1 for console, 2 for file"); + int mode = scanner.nextInt(); + List coefficients; + + if (mode == 1) { + coefficients = InputHandler.readCoefficientsFromConsole(); + } else { + System.out.println("Enter the file name:"); + String fileName = scanner.next(); + coefficients = InputHandler.readCoefficientsFromFile(fileName); + } + + Polynome p = new Polynome(coefficients); + System.out.println("Polynomial: " + p.toString()); + System.out.println("Polynomial Value at x = 1: " + p.evaluate(new Rational(1, 1)).toString()); + System.out.println("Polynomial Derivative: " + p.derivative().toString()); + System.out.println("Polynomial Integral: " + p.integrate().toString()); + + if (coefficients.size() == 2) { + System.out.println("Linear Polynomial Solution: " + p.solveLinear().toString()); + } else if (coefficients.size() == 3) { + System.out.println("Quadratic Polynomial Solutions: " + p.solveQuadratic()); + } + } +} From 91fd243091ba3f7d134f71e71b66b9c7239bffbb Mon Sep 17 00:00:00 2001 From: pozut141k <154245225+pozut141k@users.noreply.github.com> Date: Wed, 11 Dec 2024 00:17:23 +0200 Subject: [PATCH 2/4] Create coefficients.txt --- project_by_mister_brazhnyk/resources/coefficients.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 project_by_mister_brazhnyk/resources/coefficients.txt diff --git a/project_by_mister_brazhnyk/resources/coefficients.txt b/project_by_mister_brazhnyk/resources/coefficients.txt new file mode 100644 index 0000000..c22ed20 --- /dev/null +++ b/project_by_mister_brazhnyk/resources/coefficients.txt @@ -0,0 +1,3 @@ +2 1 +3 1 +1 1 From 1a271b927e9f46473ec85c064575c12d52b5a0f5 Mon Sep 17 00:00:00 2001 From: pozut141k <154245225+pozut141k@users.noreply.github.com> Date: Wed, 11 Dec 2024 00:21:22 +0200 Subject: [PATCH 3/4] Create MainTest.java --- project_by_mister_brazhnyk/test/MainTest.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 project_by_mister_brazhnyk/test/MainTest.java diff --git a/project_by_mister_brazhnyk/test/MainTest.java b/project_by_mister_brazhnyk/test/MainTest.java new file mode 100644 index 0000000..c4f7cb8 --- /dev/null +++ b/project_by_mister_brazhnyk/test/MainTest.java @@ -0,0 +1,39 @@ +import java.math.BigInteger; +import java.util.List; + +public class MainTest { + public static void main(String[] args) { + Rational r1 = new Rational(3, 4); + Rational r2 = new Rational(2, 3); + System.out.println("Rational Addition: " + r1.add(r2).toString()); + System.out.println("Rational Subtraction: " + r1.subtract(r2).toString()); + System.out.println("Rational Multiplication: " + r1.multiply(r2).toString()); + System.out.println("Rational Division: " + r1.divide(r2).toString()); + + Unsigned u1 = new Unsigned(10); + Unsigned u2 = new Unsigned(15); + System.out.println("Unsigned Addition: " + u1.add(u2).toString()); + System.out.println("Unsigned Subtraction: " + u1.subtract(u2).toString()); + System.out.println("Unsigned Multiplication: " + u1.multiply(u2).toString()); + System.out.println("Unsigned Division: " + u1.divide(u2).toString()); + + BigUnsigned bu1 = new BigUnsigned(BigInteger.valueOf(1000000000000L)); + BigUnsigned bu2 = new BigUnsigned(BigInteger.valueOf(2000000000000L)); + System.out.println("BigUnsigned Addition: " + bu1.add(bu2).toString()); + System.out.println("BigUnsigned Subtraction: " + bu1.subtract(bu2).toString()); + System.out.println("BigUnsigned Multiplication: " + bu1.multiply(bu2).toString()); + System.out.println("BigUnsigned Division: " + bu1.divide(bu2).toString()); + + List coefficients = List.of(new Rational(2, 1), new Rational(3, 1), new Rational(1, 1)); + Polynome p = new Polynome(coefficients); + System.out.println("Polynomial: " + p.toString()); + System.out.println("Polynomial Value at x = 1: " + p.evaluate(new Rational(1, 1)).toString()); + System.out.println("Polynomial Derivative: " + p.derivative().toString()); + System.out.println("Polynomial Integral: " + p.integrate().toString()); + + List quadCoefficients = List.of(new Rational(-3, 1), new Rational(2, 1), new Rational(1, 1)); + Polynome quad = new Polynome(quadCoefficients); + System.out.println("Quadratic Polynomial: " + quad.toString()); + System.out.println("Quadratic Polynomial Solutions: " + quad.solveQuadratic()); + } +} From 3f8497dd5a95562b888dda3e1a6aeb554b28eef7 Mon Sep 17 00:00:00 2001 From: pozut141k <154245225+pozut141k@users.noreply.github.com> Date: Wed, 11 Dec 2024 00:32:04 +0200 Subject: [PATCH 4/4] Add files via upload --- .../src/BigUnsigned.java | 63 +++++++ .../src/InputHandler.java | 37 ++++ .../src/NumberInterface.java | 7 + project_by_mister_brazhnyk/src/Polynome.java | 166 ++++++++++++++++++ project_by_mister_brazhnyk/src/Rational.java | 85 +++++++++ project_by_mister_brazhnyk/src/Unsigned.java | 61 +++++++ 6 files changed, 419 insertions(+) create mode 100644 project_by_mister_brazhnyk/src/BigUnsigned.java create mode 100644 project_by_mister_brazhnyk/src/InputHandler.java create mode 100644 project_by_mister_brazhnyk/src/NumberInterface.java create mode 100644 project_by_mister_brazhnyk/src/Polynome.java create mode 100644 project_by_mister_brazhnyk/src/Rational.java create mode 100644 project_by_mister_brazhnyk/src/Unsigned.java diff --git a/project_by_mister_brazhnyk/src/BigUnsigned.java b/project_by_mister_brazhnyk/src/BigUnsigned.java new file mode 100644 index 0000000..6512957 --- /dev/null +++ b/project_by_mister_brazhnyk/src/BigUnsigned.java @@ -0,0 +1,63 @@ +import java.math.BigInteger; + +public class BigUnsigned implements NumberInterface { + private BigInteger value; + + public BigUnsigned(BigInteger value) { + if (value.compareTo(BigInteger.ZERO) < 0) { + throw new IllegalArgumentException("Value cannot be negative"); + } + this.value = value; + } + + @Override + public NumberInterface add(NumberInterface other) { + if (!(other instanceof BigUnsigned)) { + throw new IllegalArgumentException("Incompatible type"); + } + BigUnsigned o = (BigUnsigned) other; + return new BigUnsigned(this.value.add(o.value)); + } + + @Override + public NumberInterface subtract(NumberInterface other) { + if (!(other instanceof BigUnsigned)) { + throw new IllegalArgumentException("Incompatible type"); + } + BigUnsigned o = (BigUnsigned) other; + if (this.value.compareTo(o.value) < 0) { + throw new IllegalArgumentException("Result cannot be negative"); + } + return new BigUnsigned(this.value.subtract(o.value)); + } + + @Override + public NumberInterface multiply(NumberInterface other) { + if (!(other instanceof BigUnsigned)) { + throw new IllegalArgumentException("Incompatible type"); + } + BigUnsigned o = (BigUnsigned) other; + return new BigUnsigned(this.value.multiply(o.value)); + } + + @Override + public NumberInterface divide(NumberInterface other) { + if (!(other instanceof BigUnsigned)) { + throw new IllegalArgumentException("Incompatible type"); + } + BigUnsigned o = (BigUnsigned) other; + if (o.value.equals(BigInteger.ZERO)) { + throw new IllegalArgumentException("Cannot divide by zero"); + } + return new BigUnsigned(this.value.divide(o.value)); + } + + @Override + public String toString() { + return value.toString(); + } + + public BigInteger getValue() { + return value; + } +} diff --git a/project_by_mister_brazhnyk/src/InputHandler.java b/project_by_mister_brazhnyk/src/InputHandler.java new file mode 100644 index 0000000..13ce7a2 --- /dev/null +++ b/project_by_mister_brazhnyk/src/InputHandler.java @@ -0,0 +1,37 @@ +import java.util.Scanner; +import java.io.File; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.List; + +public class InputHandler { + public static List readCoefficientsFromConsole() { + Scanner scanner = new Scanner(System.in); + List coefficients = new ArrayList<>(); + System.out.println("Enter the number of coefficients:"); + int n = scanner.nextInt(); + for (int i = 0; i < n; i++) { + System.out.println("Enter numerator for coefficient " + i + ":"); + int numerator = scanner.nextInt(); + System.out.println("Enter denominator for coefficient " + i + ":"); + int denominator = scanner.nextInt(); + coefficients.add(new Rational(numerator, denominator)); + } + return coefficients; + } + + public static List readCoefficientsFromFile(String fileName) { + List coefficients = new ArrayList<>(); + try { + Scanner scanner = new Scanner(new File(fileName)); + while (scanner.hasNext()) { + int numerator = scanner.nextInt(); + int denominator = scanner.nextInt(); + coefficients.add(new Rational(numerator, denominator)); + } + } catch (FileNotFoundException e) { + System.out.println("File not found: " + fileName); + } + return coefficients; + } +} diff --git a/project_by_mister_brazhnyk/src/NumberInterface.java b/project_by_mister_brazhnyk/src/NumberInterface.java new file mode 100644 index 0000000..df4abdd --- /dev/null +++ b/project_by_mister_brazhnyk/src/NumberInterface.java @@ -0,0 +1,7 @@ +public interface NumberInterface { + NumberInterface add(NumberInterface other); + NumberInterface subtract(NumberInterface other); + NumberInterface multiply(NumberInterface other); + NumberInterface divide(NumberInterface other); + String toString(); +} diff --git a/project_by_mister_brazhnyk/src/Polynome.java b/project_by_mister_brazhnyk/src/Polynome.java new file mode 100644 index 0000000..bf617ed --- /dev/null +++ b/project_by_mister_brazhnyk/src/Polynome.java @@ -0,0 +1,166 @@ +import java.util.ArrayList; +import java.util.List; + +public class Polynome implements NumberInterface { + private List coefficients; + + public Polynome() { + this.coefficients = new ArrayList<>(); + } + + public Polynome(List coefficients) { + this.coefficients = new ArrayList<>(coefficients); + } + + public Polynome(Polynome other) { + this.coefficients = new ArrayList<>(other.coefficients); + } + + @Override + public NumberInterface add(NumberInterface other) { + if (!(other instanceof Polynome)) { + throw new IllegalArgumentException("Incompatible type"); + } + Polynome otherPolynome = (Polynome) other; + List newCoefficients = new ArrayList<>(); + for (int i = 0; i < Math.max(this.coefficients.size(), otherPolynome.coefficients.size()); i++) { + NumberInterface coeff1 = i < this.coefficients.size() ? this.coefficients.get(i) : new Rational(0, 1); + NumberInterface coeff2 = i < otherPolynome.coefficients.size() ? otherPolynome.coefficients.get(i) : new Rational(0, 1); + newCoefficients.add(coeff1.add(coeff2)); + } + return new Polynome(newCoefficients); + } + + @Override + public NumberInterface subtract(NumberInterface other) { + if (!(other instanceof Polynome)) { + throw new IllegalArgumentException("Incompatible type"); + } + Polynome otherPolynome = (Polynome) other; + List newCoefficients = new ArrayList<>(); + for (int i = 0; i < Math.max(this.coefficients.size(), otherPolynome.coefficients.size()); i++) { + NumberInterface coeff1 = i < this.coefficients.size() ? this.coefficients.get(i) : new Rational(0, 1); + NumberInterface coeff2 = i < otherPolynome.coefficients.size() ? otherPolynome.coefficients.get(i) : new Rational(0, 1); + newCoefficients.add(coeff1.subtract(coeff2)); + } + return new Polynome(newCoefficients); + } + + @Override + public NumberInterface multiply(NumberInterface other) { + if (!(other instanceof Polynome)) { + throw new IllegalArgumentException("Incompatible type"); + } + Polynome otherPolynome = (Polynome) other; + List newCoefficients = new ArrayList<>(); + for (int i = 0; i < this.coefficients.size() + otherPolynome.coefficients.size() - 1; i++) { + newCoefficients.add(new Rational(0, 1)); + } + for (int i = 0; i < this.coefficients.size(); i++) { + for (int j = 0; j < otherPolynome.coefficients.size(); j++) { + newCoefficients.set(i + j, newCoefficients.get(i + j).add(this.coefficients.get(i).multiply(otherPolynome.coefficients.get(j)))); + } + } + return new Polynome(newCoefficients); + } + + @Override + public NumberInterface divide(NumberInterface other) { + throw new UnsupportedOperationException("Polynomial division is not supported yet."); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + for (int i = coefficients.size() - 1; i >= 0; i--) { + if (i < coefficients.size() - 1 && !coefficients.get(i).toString().startsWith("-")) { + sb.append("+"); + } + sb.append(coefficients.get(i).toString()); + if (i > 0) { + sb.append("x^").append(i).append(" "); + } + } + return sb.toString(); + } + + public NumberInterface evaluate(NumberInterface x) { + NumberInterface result = new Rational(0, 1); + for (int i = 0; i < coefficients.size(); i++) { + NumberInterface term = coefficients.get(i); + for (int j = 0; j < i; j++) { + term = term.multiply(x); + } + result = result.add(term); + } + return result; + } + + public Polynome derivative() { + List newCoefficients = new ArrayList<>(); + for (int i = 1; i < coefficients.size(); i++) { + newCoefficients.add(coefficients.get(i).multiply(new Unsigned(i))); + } + return new Polynome(newCoefficients); + } + + public Polynome integrate() { + List newCoefficients = new ArrayList<>(); + newCoefficients.add(new Rational(0, 1)); + for (int i = 0; i < coefficients.size(); i++) { + newCoefficients.add(coefficients.get(i).divide(new Unsigned(i + 1))); + } + return new Polynome(newCoefficients); + } + + public NumberInterface definiteIntegral(NumberInterface a, NumberInterface b) { + Polynome indefiniteIntegral = integrate(); + return indefiniteIntegral.evaluate(b).subtract(indefiniteIntegral.evaluate(a)); + } + + public NumberInterface solveLinear() { + if (coefficients.size() != 2) { + throw new IllegalArgumentException("The polynomial is not linear."); + } + NumberInterface a = coefficients.get(1); + NumberInterface b = coefficients.get(0); + + if (a.toString().equals("0")) { + throw new IllegalArgumentException("The equation is not solvable (a = 0)."); + } + return b.divide(a).multiply(new Rational(-1, 1)); + } + + public List solveQuadratic() { + if (coefficients.size() != 3) { + throw new IllegalArgumentException("The polynomial is not quadratic."); + } + NumberInterface a = coefficients.get(2); + NumberInterface b = coefficients.get(1); + NumberInterface c = coefficients.get(0); + + if (a.toString().equals("0")) { + throw new IllegalArgumentException("The equation is not solvable (a = 0)."); + } + + NumberInterface discriminant = b.multiply(b).subtract(a.multiply(c).multiply(new Rational(4, 1))); + if (discriminant.toString().equals("0")) { + List solutions = new ArrayList<>(); + solutions.add(b.multiply(new Rational(-1, 1)).divide(a.multiply(new Rational(2, 1)))); + return solutions; + } else if (discriminant.toString().charAt(0) == '-') { + throw new IllegalArgumentException("The equation has no real solutions."); + } else { + List solutions = new ArrayList<>(); + NumberInterface sqrtDiscriminant = sqrt(discriminant); + solutions.add(b.multiply(new Rational(-1, 1)).add(sqrtDiscriminant).divide(a.multiply(new Rational(2, 1)))); + solutions.add(b.multiply(new Rational(-1, 1)).subtract(sqrtDiscriminant).divide(a.multiply(new Rational(2, 1)))); + return solutions; + } + } + + private NumberInterface sqrt(NumberInterface value) { + double val = Double.parseDouble(value.toString()); + return new Rational((int) Math.sqrt(val), 1); + } +} diff --git a/project_by_mister_brazhnyk/src/Rational.java b/project_by_mister_brazhnyk/src/Rational.java new file mode 100644 index 0000000..59b2eea --- /dev/null +++ b/project_by_mister_brazhnyk/src/Rational.java @@ -0,0 +1,85 @@ +public class Rational implements NumberInterface { + private int numerator; + private int denominator; + + public Rational(int numerator, int denominator) { + if (denominator == 0) { + throw new IllegalArgumentException("Denominator cannot be zero"); + } + this.numerator = numerator; + this.denominator = denominator; + simplify(); + } + + private void simplify() { + int gcd = gcd(Math.abs(numerator), Math.abs(denominator)); + numerator /= gcd; + denominator /= gcd; + if (denominator < 0) { + numerator = -numerator; + denominator = -denominator; + } + } + + private int gcd(int a, int b) { + while (b != 0) { + int temp = b; + b = a % b; + a = temp; + } + return a; + } + + @Override + public NumberInterface add(NumberInterface other) { + if (!(other instanceof Rational)) { + throw new IllegalArgumentException("Incompatible type"); + } + Rational o = (Rational) other; + int newNumerator = this.numerator * o.denominator + o.numerator * this.denominator; + int newDenominator = this.denominator * o.denominator; + return new Rational(newNumerator, newDenominator); + } + + @Override + public NumberInterface subtract(NumberInterface other) { + if (!(other instanceof Rational)) { + throw new IllegalArgumentException("Incompatible type"); + } + Rational o = (Rational) other; + int newNumerator = this.numerator * o.denominator - o.numerator * this.denominator; + int newDenominator = this.denominator * o.denominator; + return new Rational(newNumerator, newDenominator); + } + + @Override + public NumberInterface multiply(NumberInterface other) { + if (!(other instanceof Rational)) { + throw new IllegalArgumentException("Incompatible type"); + } + Rational o = (Rational) other; + int newNumerator = this.numerator * o.numerator; + int newDenominator = this.denominator * o.denominator; + return new Rational(newNumerator, newDenominator); + } + + @Override + public NumberInterface divide(NumberInterface other) { + if (!(other instanceof Rational)) { + throw new IllegalArgumentException("Incompatible type"); + } + Rational o = (Rational) other; + int newNumerator = this.numerator * o.denominator; + int newDenominator = this.denominator * o.numerator; + return new Rational(newNumerator, newDenominator); + } + + @Override + public String toString() { + return numerator + "/" + denominator; + } + + public double toDouble() { + return (double) numerator / denominator; + } +} diff --git a/project_by_mister_brazhnyk/src/Unsigned.java b/project_by_mister_brazhnyk/src/Unsigned.java new file mode 100644 index 0000000..16b3a87 --- /dev/null +++ b/project_by_mister_brazhnyk/src/Unsigned.java @@ -0,0 +1,61 @@ +public class Unsigned implements NumberInterface { + private int value; + + public Unsigned(int value) { + if (value < 0) { + throw new IllegalArgumentException("Value cannot be negative"); + } + this.value = value; + } + + @Override + public NumberInterface add(NumberInterface other) { + if (!(other instanceof Unsigned)) { + throw new IllegalArgumentException("Incompatible type"); + } + Unsigned o = (Unsigned) other; + return new Unsigned(this.value + o.value); + } + + @Override + public NumberInterface subtract(NumberInterface other) { + if (!(other instanceof Unsigned)) { + throw new IllegalArgumentException("Incompatible type"); + } + Unsigned o = (Unsigned) other; + if (this.value < o.value) { + throw new IllegalArgumentException("Result cannot be negative"); + } + return new Unsigned(this.value - o.value); + } + + @Override + public NumberInterface multiply(NumberInterface other) { + if (!(other instanceof Unsigned)) { + throw new IllegalArgumentException("Incompatible type"); + } + Unsigned o = (Unsigned) other; + return new Unsigned(this.value * o.value); + } + + @Override + public NumberInterface divide(NumberInterface other) { + if (!(other instanceof Unsigned)) { + throw new IllegalArgumentException("Incompatible type"); + } + Unsigned o = (Unsigned) other; + if (o.value == 0) { + throw new IllegalArgumentException("Cannot divide by zero"); + } + return new Unsigned(this.value / o.value); + } + + @Override + public String toString() { + return Integer.toString(value); + } + + public int getValue() { + return value; + } +}