|
1 | 1 | package com.thealgorithms.maths; |
2 | 2 |
|
3 | | -public record ADTFraction(int numerator, int denominator) { |
4 | | - /** |
5 | | - * Initializes a newly created {@code ADTFraction} object so that it represents |
6 | | - * a fraction with the {@code numerator} and {@code denominator} provided as arguments. |
7 | | - * |
8 | | - * @param numerator The fraction numerator |
9 | | - * @param denominator The fraction denominator |
10 | | - */ |
11 | | - public ADTFraction { |
12 | | - if (denominator == 0) { |
13 | | - throw new IllegalArgumentException("Denominator cannot be 0"); |
14 | | - } |
15 | | - } |
16 | | - |
17 | | - /** |
18 | | - * Add two fractions. |
19 | | - * |
20 | | - * @param fraction the {@code ADTFraction} to add |
21 | | - * @return A new {@code ADTFraction} containing the result of the operation |
22 | | - */ |
23 | | - public ADTFraction plus(ADTFraction fraction) { |
24 | | - var numerator = this.denominator * fraction.numerator + this.numerator * fraction.denominator; |
25 | | - var denominator = this.denominator * fraction.denominator; |
26 | | - return new ADTFraction(numerator, denominator); |
27 | | - } |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | +import org.junit.jupiter.api.Test; |
28 | 5 |
|
29 | | - /** |
30 | | - * Multiply fraction by a number. |
31 | | - * |
32 | | - * @param number the number to multiply |
33 | | - * @return A new {@code ADTFraction} containing the result of the operation |
34 | | - */ |
35 | | - public ADTFraction times(int number) { |
36 | | - return times(new ADTFraction(number, 1)); |
37 | | - } |
| 6 | +public class ADTFractionTest { |
38 | 7 |
|
39 | | - /** |
40 | | - * Multiply two fractions. |
41 | | - * |
42 | | - * @param fraction the {@code ADTFraction} to multiply |
43 | | - * @return A new {@code ADTFraction} containing the result of the operation |
44 | | - */ |
45 | | - public ADTFraction times(ADTFraction fraction) { |
46 | | - var numerator = this.numerator * fraction.numerator; |
47 | | - var denominator = this.denominator * fraction.denominator; |
48 | | - return new ADTFraction(numerator, denominator); |
| 8 | + @Test |
| 9 | + void testAddition() { |
| 10 | + ADTFraction a = new ADTFraction(1, 2); |
| 11 | + ADTFraction b = new ADTFraction(1, 3); |
| 12 | + assertEquals(new ADTFraction(5, 6), a.plus(b)); |
49 | 13 | } |
50 | 14 |
|
51 | | - /** |
52 | | - * Generates the reciprocal of the fraction. |
53 | | - * |
54 | | - * @return A new {@code ADTFraction} with the {@code numerator} and {@code denominator} switched |
55 | | - */ |
56 | | - public ADTFraction reciprocal() { |
57 | | - return new ADTFraction(this.denominator, this.numerator); |
| 15 | + @Test |
| 16 | + void testMultiplication() { |
| 17 | + ADTFraction a = new ADTFraction(2, 3); |
| 18 | + ADTFraction b = new ADTFraction(3, 4); |
| 19 | + assertEquals(new ADTFraction(1, 2), a.times(b)); // simplified to 1/2 |
58 | 20 | } |
59 | 21 |
|
60 | | - /** |
61 | | - * Calculates the result of the fraction. |
62 | | - * |
63 | | - * @return The numerical result of the division between {@code numerator} and {@code |
64 | | - * denominator} |
65 | | - */ |
66 | | - public float value() { |
67 | | - return (float) this.numerator / this.denominator; |
| 22 | + @Test |
| 23 | + void testReciprocal() { |
| 24 | + ADTFraction a = new ADTFraction(3, 4); |
| 25 | + assertEquals(new ADTFraction(4, 3), a.reciprocal()); |
68 | 26 | } |
69 | 27 |
|
70 | | - /** |
71 | | - * Returns a string representation of this {@code ADTFraction} in the format |
72 | | - * {@code numerator}/{@code denominator}. |
73 | | - * |
74 | | - * @return A string representation of this {@code ADTFraction} |
75 | | - */ |
76 | | - @Override |
77 | | - public String toString() { |
78 | | - return String.format("%d/%d", this.numerator, this.denominator); |
| 28 | + @Test |
| 29 | + void testSimplification() { |
| 30 | + ADTFraction a = new ADTFraction(2, 4); |
| 31 | + assertEquals(new ADTFraction(1, 2), a); |
79 | 32 | } |
80 | 33 | } |
0 commit comments