|
1 | 1 | package com.thealgorithms.maths;
|
2 | 2 |
|
3 |
| -public class ADTFraction { |
4 |
| - |
5 |
| - public static void main(String[] args) { |
6 |
| - // TODO code application logic here |
7 |
| - |
8 |
| - ADTFraction f1 = new ADTFraction(3, 5); |
9 |
| - f1.display(); |
10 |
| - ADTFraction f2 = new ADTFraction(7, 8); |
11 |
| - f2.display(); |
12 |
| - ADTFraction f3 = f1.plus(f2); |
13 |
| - f3.display(); |
14 |
| - ADTFraction f4 = f1.times(f2); |
15 |
| - f4.display(); |
16 |
| - ADTFraction f5 = f1.times(4); |
17 |
| - f5.display(); |
18 |
| - |
19 |
| - } |
20 |
| - |
21 |
| - private int n; //numerator |
22 |
| - private int d; //denomenator |
23 |
| - |
24 |
| - public ADTFraction() { |
25 |
| - this.n = 0; |
26 |
| - this.d = 1; |
27 |
| - } |
28 |
| - |
29 |
| - public ADTFraction(int a, int b) {//parameter constructor |
30 |
| - |
31 |
| - if (b != 0) { |
32 |
| - this.n = a; |
33 |
| - this.d = b; |
34 |
| - } else { |
35 |
| - this.n = 0; |
36 |
| - this.d = 1; |
37 |
| - System.out.println("denomerator cannot be 0,default values assinged"); |
| 3 | +public record ADTFraction(int numerator, int denominator) { |
| 4 | + |
| 5 | + /** |
| 6 | + * Initializes a newly created {@code ADTFraction} object so that it represents |
| 7 | + * a fraction with the {@code numerator} and {@code denominator} provided as arguments. |
| 8 | + * |
| 9 | + * @param numerator The fraction numerator |
| 10 | + * @param denominator The fraction denominator |
| 11 | + */ |
| 12 | + public ADTFraction { |
| 13 | + if (denominator == 0) { |
| 14 | + throw new IllegalArgumentException("Denominator cannot be 0"); |
38 | 15 | }
|
39 | 16 | }
|
40 | 17 |
|
41 |
| - public void set(int a, int b) {//set numerator and denomenator |
42 |
| - |
43 |
| - if (b != 0) { |
44 |
| - this.n = a; |
45 |
| - this.d = b; |
46 |
| - } else { |
47 |
| - this.n = 0; |
48 |
| - this.d = 1; |
49 |
| - System.out.println("denomerator cannot be 0,default values assinged"); |
50 |
| - } |
| 18 | + /** |
| 19 | + * Add two fractions. |
| 20 | + * |
| 21 | + * @param fraction the {@code ADTFraction} to add |
| 22 | + * @return A new {@code ADTFraction} containing the result of the operation |
| 23 | + */ |
| 24 | + public ADTFraction plus(ADTFraction fraction) { |
| 25 | + var numerator = this.denominator * fraction.numerator + this.numerator * fraction.denominator; |
| 26 | + var denominator = this.denominator * fraction.denominator; |
| 27 | + return new ADTFraction(numerator, denominator); |
51 | 28 |
|
52 | 29 | }
|
53 | 30 |
|
54 |
| - //add two fractions |
55 |
| - public ADTFraction plus(ADTFraction x) { |
56 |
| - |
57 |
| - int num, den; |
58 |
| - num = this.d * x.n + this.n * x.d; |
59 |
| - den = this.d * x.d; |
60 |
| - ADTFraction f = new ADTFraction(num, den); |
61 |
| - return f; |
62 |
| - |
| 31 | + /** |
| 32 | + * Multiply fraction by a number. |
| 33 | + * |
| 34 | + * @param number the number to multiply |
| 35 | + * @return A new {@code ADTFraction} containing the result of the operation |
| 36 | + */ |
| 37 | + public ADTFraction times(int number) { |
| 38 | + return times(new ADTFraction(number, 1)); |
63 | 39 | }
|
64 | 40 |
|
65 |
| - public ADTFraction times(int a) {//multiply fraction by a number |
66 |
| - |
67 |
| - int num, den; |
68 |
| - num = this.n * a; |
69 |
| - den = this.d; |
70 |
| - ADTFraction f1 = new ADTFraction(num, den); |
71 |
| - return f1; |
72 |
| - |
73 |
| - } |
74 |
| - |
75 |
| - public ADTFraction times(ADTFraction x) {//multiply two fractions |
76 |
| - |
77 |
| - int num, dem; |
78 |
| - num = this.n * x.n; |
79 |
| - dem = this.d * x.d; |
80 |
| - ADTFraction f3 = new ADTFraction(num, dem); |
81 |
| - return f3; |
82 |
| - |
| 41 | + /** |
| 42 | + * Multiply two fractions. |
| 43 | + * |
| 44 | + * @param fraction the {@code ADTFraction} to multiply |
| 45 | + * @return A new {@code ADTFraction} containing the result of the operation |
| 46 | + */ |
| 47 | + public ADTFraction times(ADTFraction fraction) { |
| 48 | + var numerator = this.numerator * fraction.numerator; |
| 49 | + var denominator = this.denominator * fraction.denominator; |
| 50 | + return new ADTFraction(numerator, denominator); |
83 | 51 | }
|
84 | 52 |
|
85 |
| - //reciprocal of a fraction |
| 53 | + /** |
| 54 | + * Generates the reciprocal of the fraction. |
| 55 | + * |
| 56 | + * @return A new {@code ADTFraction} with the {@code numerator} and {@code denominator} switched |
| 57 | + */ |
86 | 58 | public ADTFraction reciprocal() {
|
87 |
| - ADTFraction f1 = new ADTFraction(this.d, this.n); |
88 |
| - return f1; |
| 59 | + return new ADTFraction(this.denominator, this.numerator); |
89 | 60 | }
|
90 | 61 |
|
91 |
| - //numerical value of a fraction |
| 62 | + /** |
| 63 | + * Calculates the result of the fraction. |
| 64 | + * |
| 65 | + * @return The numerical result of the division between {@code numerator} and {@code denominator} |
| 66 | + */ |
92 | 67 | public float value() {
|
93 |
| - return (float) this.n / this.d; |
| 68 | + return (float) this.numerator / this.denominator; |
94 | 69 | }
|
95 | 70 |
|
96 |
| - //display the fraction in the format n/d |
97 |
| - public void display() { |
98 |
| - System.out.println(this.n + "/" + this.d); |
| 71 | + /** |
| 72 | + * Returns a string representation of this {@code ADTFraction} in the format |
| 73 | + * {@code numerator}/{@code denominator}. |
| 74 | + * |
| 75 | + * @return A string representation of this {@code ADTFraction} |
| 76 | + */ |
| 77 | + @Override |
| 78 | + public String toString() { |
| 79 | + return String.format("%d/%d", this.numerator, this.denominator); |
99 | 80 | }
|
100 | 81 | }
|
0 commit comments