Skip to content

Commit 4b15b2c

Browse files
Code refactor for ADTFraction improvements (TheAlgorithms#3016)
Fix TheAlgorithms#3015 Co-authored-by: Yang Libin <szuyanglb@outlook.com>
1 parent 5eed084 commit 4b15b2c

File tree

2 files changed

+113
-80
lines changed

2 files changed

+113
-80
lines changed
Lines changed: 61 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,81 @@
11
package com.thealgorithms.maths;
22

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");
3815
}
3916
}
4017

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);
5128

5229
}
5330

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));
6339
}
6440

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);
8351
}
8452

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+
*/
8658
public ADTFraction reciprocal() {
87-
ADTFraction f1 = new ADTFraction(this.d, this.n);
88-
return f1;
59+
return new ADTFraction(this.denominator, this.numerator);
8960
}
9061

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+
*/
9267
public float value() {
93-
return (float) this.n / this.d;
68+
return (float) this.numerator / this.denominator;
9469
}
9570

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);
9980
}
10081
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.thealgorithms.maths;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertThrows;
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
8+
9+
public class ADTFractionTest {
10+
11+
private final ADTFraction fraction1 = new ADTFraction(3, 5);
12+
private final ADTFraction fraction2 = new ADTFraction(7, 8);
13+
14+
@Test
15+
void testConstructorWithDenominatorEqualToZero() {
16+
Exception exception = assertThrows(IllegalArgumentException.class, () -> new ADTFraction(1, 0));
17+
assertEquals("Denominator cannot be 0", exception.getMessage());
18+
}
19+
20+
@Test
21+
public void testPlus() {
22+
assertEquals(new ADTFraction(59, 40), fraction1.plus(fraction2));
23+
}
24+
25+
@Test
26+
public void testTimes() {
27+
assertEquals(new ADTFraction(12, 5), fraction1.times(4));
28+
assertEquals(new ADTFraction(21, 40), fraction1.times(fraction2));
29+
}
30+
31+
@Test
32+
public void testReciprocal() {
33+
assertEquals(new ADTFraction(5, 3), fraction1.reciprocal());
34+
}
35+
36+
@Test
37+
public void testValue() {
38+
assertEquals(0.6F, fraction1.value());
39+
}
40+
41+
@Test
42+
public void testEqualsAndHashCode() {
43+
ADTFraction fraction3 = new ADTFraction(3, 5);
44+
assertTrue(fraction1.equals(fraction3) && fraction3.equals(fraction1));
45+
assertEquals(fraction1.hashCode(), fraction3.hashCode());
46+
}
47+
48+
@Test
49+
public void testToString() {
50+
assertEquals("3/5", fraction1.toString());
51+
}
52+
}

0 commit comments

Comments
 (0)