diff --git a/src/main/java/fr/insa/gei/angles/Angle.java b/src/main/java/fr/insa/gei/angles/Angle.java new file mode 100644 index 0000000..e1edb2d --- /dev/null +++ b/src/main/java/fr/insa/gei/angles/Angle.java @@ -0,0 +1,76 @@ +package fr.insa.gei.angles; + +public class Angle implements IAngle { + + private double radians; + + protected Angle(double radians) { + this.radians = radians; + } + + @Override + public double toDegrees() { + return Math.toDegrees(this.radians); + } + + @Override + public double toRadians() { + return this.radians; + } + + @Override + public double cos() { + return Math.cos(this.radians); + } + + @Override + public double sin() { + return Math.sin(this.radians); + } + + @Override + public double tan() { + return Math.tan(this.radians); + } + + @Override + public IAngle add(IAngle other) { + return new Angle(this.radians + other.toRadians()); + } + + @Override + public IAngle sub(IAngle other) { + return new Angle(this.radians - other.toRadians()); + } + + @Override + public IAngle mul(double factor) { + return new Angle(this.radians * factor); + } + + @Override + public IAngle div(double divisor) { + return new Angle(this.radians / divisor); + } + + @Override + public int compareTo(IAngle o) { + return Double.compare(this.radians, o.toRadians()); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null || getClass() != obj.getClass()) + return false; + Angle other = (Angle) obj; + return Double.compare(this.radians, other.radians) == 0; + } + + @Override + public String toString() { + return String.format("%.2frad", this.radians); + } + +} diff --git a/src/main/java/fr/insa/gei/angles/AngleFactory.java b/src/main/java/fr/insa/gei/angles/AngleFactory.java new file mode 100644 index 0000000..17c7d95 --- /dev/null +++ b/src/main/java/fr/insa/gei/angles/AngleFactory.java @@ -0,0 +1,15 @@ +package fr.insa.gei.angles; + +public class AngleFactory implements IAngleFactory { + + @Override + public Angle fromRadians(double radians) { + return new Angle(radians); + } + + @Override + public Angle fromDegrees(double degrees) { + return new Angle(Math.toRadians(degrees)); + } + +} diff --git a/src/main/java/fr/insa/gei/angles/IAngleFactory.java b/src/main/java/fr/insa/gei/angles/IAngleFactory.java index 2504610..6e4f9c8 100644 --- a/src/main/java/fr/insa/gei/angles/IAngleFactory.java +++ b/src/main/java/fr/insa/gei/angles/IAngleFactory.java @@ -1,6 +1,6 @@ package fr.insa.gei.angles; -public interface IAngleFactory { +public interface IAngleFactory { /** * Construct a new Angle object using the given value in radians. @@ -8,7 +8,7 @@ public interface IAngleFactory { * @param radians Value of the angle in radians. * @return a new Angle object. */ - public Angle fromRadians(double radians); + public GAngle fromRadians(double radians); /** * Construct a new Angle object using the given value in degrees. @@ -16,7 +16,7 @@ public interface IAngleFactory { * @param degrees Value of the angle in degrees. * @return a new Angle object. */ - public Angle fromDegrees(double degrees); + public GAngle fromDegrees(double degrees); /** * Construct a new Angle object corresponding to the arc cosine of the given @@ -25,7 +25,7 @@ public interface IAngleFactory { * @param a Value for which to compute the arc cosine. * @return a new Angle object. */ - public default Angle acos(double a) { + public default GAngle acos(double a) { return fromRadians(Math.acos(a)); } @@ -36,7 +36,7 @@ public interface IAngleFactory { * @param a Value for which to compute the arc sine. * @return a new Angle object. */ - public default Angle asin(double a) { + public default GAngle asin(double a) { return fromRadians(Math.asin(a)); } @@ -47,7 +47,7 @@ public interface IAngleFactory { * @param a Value for which to compute the arc tangent. * @return a new Angle object. */ - public default Angle atan(double a) { + public default GAngle atan(double a) { return fromRadians(Math.atan(a)); } @@ -59,7 +59,7 @@ public interface IAngleFactory { * @param x Value for which to compute the arc tangent. * @return a new Angle object. */ - public default Angle atan2(double y, double x) { + public default GAngle atan2(double y, double x) { return fromRadians(Math.atan2(y, x)); } diff --git a/src/main/java/fr/insa/gei/fractions/Fraction.java b/src/main/java/fr/insa/gei/fractions/Fraction.java new file mode 100644 index 0000000..d483ad0 --- /dev/null +++ b/src/main/java/fr/insa/gei/fractions/Fraction.java @@ -0,0 +1,135 @@ +package fr.insa.gei.fractions; + +public class Fraction implements IFraction { + + private final int numerator; + private final int denominator; + + protected Fraction(int numerator, int denominator) { + if (denominator == 0) { + throw new IllegalArgumentException("Denominator cannot be zero."); + } + + if (denominator < 0) { + numerator = -numerator; + denominator = -denominator; + } + + final int gcd = this.gcd(Math.abs(numerator), Math.abs(denominator)); + + this.numerator = numerator / gcd; + this.denominator = denominator / gcd; + } + + private int gcd(int a, int b) { + while (b != 0) { + final int temp = b; + b = a % b; + a = temp; + } + return a; + } + + @Override + public int getNumerator() { + return this.numerator; + } + + @Override + public int getDenominator() { + return this.denominator; + } + + @Override + public IFraction add(IFraction f) { + return new Fraction( + this.numerator * f.getDenominator() + f.getNumerator() * this.denominator, + this.denominator * f.getDenominator()); + } + + @Override + public IFraction add(int value) { + return new Fraction( + this.numerator + value * this.denominator, + this.denominator); + } + + @Override + public IFraction sub(IFraction f) { + return this.add(f.neg()); + } + + @Override + public IFraction sub(int value) { + return this.add(-value); + } + + @Override + public IFraction mul(IFraction f) { + return new Fraction( + this.numerator * f.getNumerator(), + this.denominator * f.getDenominator()); + } + + @Override + public IFraction mul(int value) { + return new Fraction( + this.numerator * value, + this.denominator); + } + + @Override + public IFraction div(IFraction f) { + return new Fraction( + this.numerator * f.getDenominator(), + this.denominator * f.getNumerator()); + } + + @Override + public IFraction div(int value) { + return new Fraction( + this.numerator, + this.denominator * value); + } + + @Override + public IFraction inv() { + return new Fraction( + this.denominator, + this.numerator); + } + + @Override + public IFraction neg() { + return new Fraction( + -this.numerator, + this.denominator); + } + + @Override + public double toDouble() { + return (double) this.numerator / this.denominator; + } + + @Override + public String toString() { + return this.numerator + "/" + this.denominator; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null || getClass() != obj.getClass()) + return false; + Fraction other = (Fraction) obj; + return this.numerator * other.denominator == other.numerator * this.denominator; + } + + @Override + public int compareTo(IFraction other) { + // a/b cmp c/d <=> a*d cmp b*c (because both b and d are positive) + return Integer.compare(this.numerator * other.getDenominator(), this.denominator * other.getNumerator()); + } + +} diff --git a/src/main/java/fr/insa/gei/fractions/FractionFactory.java b/src/main/java/fr/insa/gei/fractions/FractionFactory.java new file mode 100644 index 0000000..5e9a086 --- /dev/null +++ b/src/main/java/fr/insa/gei/fractions/FractionFactory.java @@ -0,0 +1,10 @@ +package fr.insa.gei.fractions; + +public class FractionFactory implements IFractionFactory { + + @Override + public Fraction create(int numerator, int denominator) { + return new Fraction(numerator, denominator); + } + +} diff --git a/src/main/java/fr/insa/gei/fractions/IFractionFactory.java b/src/main/java/fr/insa/gei/fractions/IFractionFactory.java index ae68242..5bfb3d9 100644 --- a/src/main/java/fr/insa/gei/fractions/IFractionFactory.java +++ b/src/main/java/fr/insa/gei/fractions/IFractionFactory.java @@ -1,6 +1,6 @@ package fr.insa.gei.fractions; -public interface IFractionFactory { +public interface IFractionFactory { /** * Creates a fraction with given numerator and denominator. @@ -9,7 +9,7 @@ public interface IFractionFactory { * @param denominator the denominator of the fraction * @return a new fraction instance */ - public Fraction create(int numerator, int denominator); + public GFraction create(int numerator, int denominator); /** * Creates a fraction with given numerator and denominator equal to 1. @@ -17,7 +17,7 @@ public interface IFractionFactory { * @param numerator the numerator of the fraction * @return a new fraction instance */ - public default Fraction create(int numerator) { + public default GFraction create(int numerator) { return this.create(numerator, 1); }