Add implementation of Fraction and Angle.

This commit is contained in:
Mikaël Capelle
2025-11-23 20:49:35 +01:00
parent ff1356e35f
commit c88a6318b9
6 changed files with 246 additions and 10 deletions

View File

@@ -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);
}
}

View File

@@ -0,0 +1,15 @@
package fr.insa.gei.angles;
public class AngleFactory implements IAngleFactory<Angle> {
@Override
public Angle fromRadians(double radians) {
return new Angle(radians);
}
@Override
public Angle fromDegrees(double degrees) {
return new Angle(Math.toRadians(degrees));
}
}

View File

@@ -1,6 +1,6 @@
package fr.insa.gei.angles;
public interface IAngleFactory<Angle extends IAngle> {
public interface IAngleFactory<GAngle extends IAngle> {
/**
* Construct a new Angle object using the given value in radians.
@@ -8,7 +8,7 @@ public interface IAngleFactory<Angle extends IAngle> {
* @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<Angle extends IAngle> {
* @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<Angle extends IAngle> {
* @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<Angle extends IAngle> {
* @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<Angle extends IAngle> {
* @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<Angle extends IAngle> {
* @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));
}

View File

@@ -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());
}
}

View File

@@ -0,0 +1,10 @@
package fr.insa.gei.fractions;
public class FractionFactory implements IFractionFactory<Fraction> {
@Override
public Fraction create(int numerator, int denominator) {
return new Fraction(numerator, denominator);
}
}

View File

@@ -1,6 +1,6 @@
package fr.insa.gei.fractions;
public interface IFractionFactory<Fraction extends IFraction> {
public interface IFractionFactory<GFraction extends IFraction> {
/**
* Creates a fraction with given numerator and denominator.
@@ -9,7 +9,7 @@ public interface IFractionFactory<Fraction extends IFraction> {
* @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<Fraction extends IFraction> {
* @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);
}