Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions project_by_mister_brazhnyk/resources/coefficients.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2 1
3 1
1 1
63 changes: 63 additions & 0 deletions project_by_mister_brazhnyk/src/BigUnsigned.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
37 changes: 37 additions & 0 deletions project_by_mister_brazhnyk/src/InputHandler.java
Original file line number Diff line number Diff line change
@@ -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<NumberInterface> readCoefficientsFromConsole() {
Scanner scanner = new Scanner(System.in);
List<NumberInterface> 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<NumberInterface> readCoefficientsFromFile(String fileName) {
List<NumberInterface> 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;
}
}
31 changes: 31 additions & 0 deletions project_by_mister_brazhnyk/src/Main.java
Original file line number Diff line number Diff line change
@@ -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<NumberInterface> 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());
}
}
}
7 changes: 7 additions & 0 deletions project_by_mister_brazhnyk/src/NumberInterface.java
Original file line number Diff line number Diff line change
@@ -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();
}
166 changes: 166 additions & 0 deletions project_by_mister_brazhnyk/src/Polynome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import java.util.ArrayList;
import java.util.List;

public class Polynome implements NumberInterface {
private List<NumberInterface> coefficients;

public Polynome() {
this.coefficients = new ArrayList<>();
}

public Polynome(List<NumberInterface> 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<NumberInterface> 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<NumberInterface> 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<NumberInterface> 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<NumberInterface> 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<NumberInterface> 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<NumberInterface> 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<NumberInterface> 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<NumberInterface> 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);
}
}
85 changes: 85 additions & 0 deletions project_by_mister_brazhnyk/src/Rational.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading