From 9b812d58f1a87df481449ea39737a3502b4ce46f Mon Sep 17 00:00:00 2001 From: akshtih30388 Date: Mon, 3 Nov 2025 08:21:00 +0530 Subject: [PATCH] Add ADTFraction class for arithmetic operations on fractions --- .../com/thealgorithms/maths/ADTFraction.java | 89 +++++-------------- 1 file changed, 21 insertions(+), 68 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/ADTFraction.java b/src/main/java/com/thealgorithms/maths/ADTFraction.java index a85ef09079c9..5011f70c242c 100644 --- a/src/main/java/com/thealgorithms/maths/ADTFraction.java +++ b/src/main/java/com/thealgorithms/maths/ADTFraction.java @@ -1,80 +1,33 @@ package com.thealgorithms.maths; -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"); - } - } - - /** - * 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); - } +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; - /** - * 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 class ADTFractionTest { - /** - * 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); + @Test + void testAddition() { + ADTFraction a = new ADTFraction(1, 2); + ADTFraction b = new ADTFraction(1, 3); + assertEquals(new ADTFraction(5, 6), a.plus(b)); } - /** - * Generates the reciprocal of the fraction. - * - * @return A new {@code ADTFraction} with the {@code numerator} and {@code denominator} switched - */ - public ADTFraction reciprocal() { - return new ADTFraction(this.denominator, this.numerator); + @Test + void testMultiplication() { + ADTFraction a = new ADTFraction(2, 3); + ADTFraction b = new ADTFraction(3, 4); + assertEquals(new ADTFraction(1, 2), a.times(b)); // simplified to 1/2 } - /** - * 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.numerator / this.denominator; + @Test + void testReciprocal() { + ADTFraction a = new ADTFraction(3, 4); + assertEquals(new ADTFraction(4, 3), a.reciprocal()); } - /** - * 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 + void testSimplification() { + ADTFraction a = new ADTFraction(2, 4); + assertEquals(new ADTFraction(1, 2), a); } }