Skip to content

Commit 9ea9251

Browse files
authored
Merge branch 'master' into master
2 parents 7546eb7 + 64f1e51 commit 9ea9251

File tree

11 files changed

+296
-161
lines changed

11 files changed

+296
-161
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
}

src/main/java/com/thealgorithms/maths/AbsoluteMax.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,28 @@
22

33
import java.util.Arrays;
44

5-
/**
6-
* description:
7-
*
8-
* <p>
9-
* absMax([0, 5, 1, 11]) = 11, absMax([3 , -10, -2]) = -10
10-
*/
115
public class AbsoluteMax {
126

13-
public static void main(String[] args) {
14-
int[] testnums = {-2, 0, 16};
15-
assert absMax(testnums) == 16;
16-
17-
int[] numbers = {3, -10, -2};
18-
System.out.println("absMax(" + Arrays.toString(numbers) + ") = " + absMax(numbers));
19-
}
20-
217
/**
22-
* get the value, return the absolute max value
8+
* Compares the numbers given as arguments to get the absolute max value.
239
*
24-
* @param numbers contains elements
25-
* @return the absolute max value
10+
* @param numbers The numbers to compare
11+
* @return The absolute max value
2612
*/
27-
public static int absMax(int[] numbers) {
28-
int absMaxValue = numbers[0];
29-
for (int i = 1, length = numbers.length; i < length; ++i) {
30-
if (Math.abs(numbers[i]) > Math.abs(absMaxValue)) {
31-
absMaxValue = numbers[i];
32-
}
13+
public static int getMaxValue(int... numbers) {
14+
if (numbers.length == 0) {
15+
throw new IllegalArgumentException("Numbers array cannot be empty");
3316
}
34-
return absMaxValue;
17+
18+
var absMaxWrapper = new Object() {
19+
int value = numbers[0];
20+
};
21+
22+
Arrays.stream(numbers)
23+
.skip(1)
24+
.filter(number -> Math.abs(number) > Math.abs(absMaxWrapper.value))
25+
.forEach(number -> absMaxWrapper.value = number);
26+
27+
return absMaxWrapper.value;
3528
}
3629
}

src/main/java/com/thealgorithms/maths/AbsoluteMin.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,28 @@
22

33
import java.util.Arrays;
44

5-
/**
6-
* description:
7-
*
8-
* <p>
9-
* absMin([0, 5, 1, 11]) = 0, absMin([3 , -10, -2]) = -2
10-
*/
115
public class AbsoluteMin {
126

13-
public static void main(String[] args) {
14-
int[] testnums = {4, 0, 16};
15-
assert absMin(testnums) == 0;
16-
17-
int[] numbers = {3, -10, -2};
18-
System.out.println("absMin(" + Arrays.toString(numbers) + ") = " + absMin(numbers));
19-
}
20-
217
/**
22-
* get the value, returns the absolute min value min
8+
* Compares the numbers given as arguments to get the absolute min value.
239
*
24-
* @param numbers contains elements
25-
* @return the absolute min value
10+
* @param numbers The numbers to compare
11+
* @return The absolute min value
2612
*/
27-
public static int absMin(int[] numbers) {
28-
int absMinValue = numbers[0];
29-
for (int i = 1, length = numbers.length; i < length; ++i) {
30-
if (Math.abs(numbers[i]) < Math.abs(absMinValue)) {
31-
absMinValue = numbers[i];
32-
}
13+
public static int getMinValue(int... numbers) {
14+
if (numbers.length == 0) {
15+
throw new IllegalArgumentException("Numbers array cannot be empty");
3316
}
34-
return absMinValue;
17+
18+
var absMinWrapper = new Object() {
19+
int value = numbers[0];
20+
};
21+
22+
Arrays.stream(numbers)
23+
.skip(1)
24+
.filter(number -> Math.abs(number) < Math.abs(absMinWrapper.value))
25+
.forEach(number -> absMinWrapper.value = number);
26+
27+
return absMinWrapper.value;
3528
}
3629
}
Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
package com.thealgorithms.maths;
22

3-
import java.util.Random;
4-
53
public class AbsoluteValue {
64

7-
public static void main(String[] args) {
8-
Random random = new Random();
9-
10-
/* test 1000 random numbers */
11-
for (int i = 1; i <= 1000; ++i) {
12-
int randomNumber = random.nextInt();
13-
assert absVal(randomNumber) == Math.abs(randomNumber);
14-
}
15-
}
16-
175
/**
18-
* If value is less than zero, make value positive.
6+
* Returns the absolute value of a number.
197
*
20-
* @param value a number
21-
* @return the absolute value of a number
8+
* @param number The number to be transformed
9+
* @return The absolute value of the {@code number}
2210
*/
23-
public static int absVal(int value) {
24-
return value < 0 ? -value : value;
11+
public static int getAbsValue(int number) {
12+
return number < 0 ? -number : number;
2513
}
2614
}
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.thealgorithms.maths;
22

3+
import java.util.stream.IntStream;
4+
35
/**
46
* In number theory, the aliquot sum s(n) of a positive integer n is the sum of
57
* all proper divisors of n, that is, all divisors of n other than n itself. For
@@ -9,26 +11,22 @@
911
*/
1012
public class AliquotSum {
1113

12-
public static void main(String[] args) {
13-
assert aliquotSum(1) == 0;
14-
assert aliquotSum(6) == 6;
15-
assert aliquotSum(15) == 9;
16-
assert aliquotSum(19) == 1;
17-
}
18-
1914
/**
20-
* Finds the aliquot sum of an integer number
15+
* Finds the aliquot sum of an integer number.
2116
*
2217
* @param number a positive integer
2318
* @return aliquot sum of given {@code number}
2419
*/
25-
public static int aliquotSum(int number) {
26-
int sum = 0;
27-
for (int i = 1, limit = number / 2; i <= limit; ++i) {
28-
if (number % i == 0) {
29-
sum += i;
30-
}
31-
}
32-
return sum;
20+
public static int getAliquotValue(int number) {
21+
var sumWrapper = new Object() {
22+
int value = 0;
23+
};
24+
25+
IntStream.iterate(1, i -> ++i)
26+
.limit(number / 2)
27+
.filter(i -> number % i == 0)
28+
.forEach(i -> sumWrapper.value += i);
29+
30+
return sumWrapper.value;
3331
}
3432
}

0 commit comments

Comments
 (0)