Repository: TheAlgorithms/Java
Pull request #3016 (2022-04-19 22:22:54)

Code refactor for ADTFraction improvements
### **Describe your change:**

- [ ] Add an algorithm?
- [x] Fix a bug or typo in an existing algorithm?
- [x] Documentation change?

#### References

<!-- Add any reference to previous pull-request or issue -->

See: https://github.com/TheAlgorithms/Java/issues/3015

<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Java files are placed inside an existing directory.
- [x] All functions and variable names follow Java naming conventions.
- [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.



Patch:
diff --git a/src/main/java/com/thealgorithms/maths/ADTFraction.java b/src/main/java/com/thealgorithms/maths/ADTFraction.java
index ca70ba0e..612dfaec 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 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");
+public record ADTFraction(int numerator, int denominator) {
+
+    /**
+     * 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;
-
-    }
-
-    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;
-
+    /**
+     * 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);
     }
 
-    //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);
     }
 }


Test patch:
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 00000000..611df73b
--- /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());
+    }
+}