diff --git a/src/main/java/com/thealgorithms/maths/ADTFraction.java b/src/main/java/com/thealgorithms/maths/ADTFraction.java index ca70ba0e0..612dfaeca 100644 --- a/src/main/java/com/thealgorithms/maths/ADTFraction.java +++ b/src/main/java/com/thealgorithms/maths/ADTFraction.java @@ -1,100 +1,81 @@ package com.thealgorithms.maths; -public class ADTFraction { +public record ADTFraction(int numerator, int denominator) { - public static void main(String[] args) { - // TODO code application logic here - - ADTFraction f1 = new ADTFraction(3, 5); - f1.display(); - ADTFraction f2 = new ADTFraction(7, 8); - f2.display(); - ADTFraction f3 = f1.plus(f2); - f3.display(); - ADTFraction f4 = f1.times(f2); - f4.display(); - ADTFraction f5 = f1.times(4); - f5.display(); - - } - - private int n; //numerator - private int d; //denomenator - - public ADTFraction() { - this.n = 0; - this.d = 1; - } - - public ADTFraction(int a, int b) {//parameter constructor - - if (b != 0) { - this.n = a; - this.d = b; - } else { - this.n = 0; - this.d = 1; - System.out.println("denomerator cannot be 0,default values assinged"); + /** + * Initializes a newly created {@code ADTFraction} object so that it represents + * a fraction with the {@code numerator} and {@code denominator} provided as arguments. + * + * @param numerator The fraction numerator + * @param denominator The fraction denominator + */ + public ADTFraction { + if (denominator == 0) { + throw new IllegalArgumentException("Denominator cannot be 0"); } } - public void set(int a, int b) {//set numerator and denomenator - - if (b != 0) { - this.n = a; - this.d = b; - } else { - this.n = 0; - this.d = 1; - System.out.println("denomerator cannot be 0,default values assinged"); - } + /** + * Add two fractions. + * + * @param fraction the {@code ADTFraction} to add + * @return A new {@code ADTFraction} containing the result of the operation + */ + public ADTFraction plus(ADTFraction fraction) { + var numerator = this.denominator * fraction.numerator + this.numerator * fraction.denominator; + var denominator = this.denominator * fraction.denominator; + return new ADTFraction(numerator, denominator); } - //add two fractions - public ADTFraction plus(ADTFraction x) { - - int num, den; - num = this.d * x.n + this.n * x.d; - den = this.d * x.d; - ADTFraction f = new ADTFraction(num, den); - return f; - + /** + * Multiply fraction by a number. + * + * @param number the number to multiply + * @return A new {@code ADTFraction} containing the result of the operation + */ + public ADTFraction times(int number) { + return times(new ADTFraction(number, 1)); } - public ADTFraction times(int a) {//multiply fraction by a number - - int num, den; - num = this.n * a; - den = this.d; - ADTFraction f1 = new ADTFraction(num, den); - return f1; - + /** + * Multiply two fractions. + * + * @param fraction the {@code ADTFraction} to multiply + * @return A new {@code ADTFraction} containing the result of the operation + */ + public ADTFraction times(ADTFraction fraction) { + var numerator = this.numerator * fraction.numerator; + var denominator = this.denominator * fraction.denominator; + return new ADTFraction(numerator, denominator); } - public ADTFraction times(ADTFraction x) {//multiply two fractions - - int num, dem; - num = this.n * x.n; - dem = this.d * x.d; - ADTFraction f3 = new ADTFraction(num, dem); - return f3; - - } - - //reciprocal of a fraction + /** + * Generates the reciprocal of the fraction. + * + * @return A new {@code ADTFraction} with the {@code numerator} and {@code denominator} switched + */ public ADTFraction reciprocal() { - ADTFraction f1 = new ADTFraction(this.d, this.n); - return f1; + return new ADTFraction(this.denominator, this.numerator); } - //numerical value of a fraction + /** + * Calculates the result of the fraction. + * + * @return The numerical result of the division between {@code numerator} and {@code denominator} + */ public float value() { - return (float) this.n / this.d; + return (float) this.numerator / this.denominator; } - //display the fraction in the format n/d - public void display() { - System.out.println(this.n + "/" + this.d); + /** + * Returns a string representation of this {@code ADTFraction} in the format + * {@code numerator}/{@code denominator}. + * + * @return A string representation of this {@code ADTFraction} + */ + @Override + public String toString() { + return String.format("%d/%d", this.numerator, this.denominator); } } diff --git a/src/test/java/com/thealgorithms/maths/ADTFractionTest.java b/src/test/java/com/thealgorithms/maths/ADTFractionTest.java new file mode 100644 index 000000000..611df73bc --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/ADTFractionTest.java @@ -0,0 +1,52 @@ +package com.thealgorithms.maths; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class ADTFractionTest { + + private final ADTFraction fraction1 = new ADTFraction(3, 5); + private final ADTFraction fraction2 = new ADTFraction(7, 8); + + @Test + void testConstructorWithDenominatorEqualToZero() { + Exception exception = assertThrows(IllegalArgumentException.class, () -> new ADTFraction(1, 0)); + assertEquals("Denominator cannot be 0", exception.getMessage()); + } + + @Test + public void testPlus() { + assertEquals(new ADTFraction(59, 40), fraction1.plus(fraction2)); + } + + @Test + public void testTimes() { + assertEquals(new ADTFraction(12, 5), fraction1.times(4)); + assertEquals(new ADTFraction(21, 40), fraction1.times(fraction2)); + } + + @Test + public void testReciprocal() { + assertEquals(new ADTFraction(5, 3), fraction1.reciprocal()); + } + + @Test + public void testValue() { + assertEquals(0.6F, fraction1.value()); + } + + @Test + public void testEqualsAndHashCode() { + ADTFraction fraction3 = new ADTFraction(3, 5); + assertTrue(fraction1.equals(fraction3) && fraction3.equals(fraction1)); + assertEquals(fraction1.hashCode(), fraction3.hashCode()); + } + + @Test + public void testToString() { + assertEquals("3/5", fraction1.toString()); + } +}