Skip to content

Commit a3fdd99

Browse files
author
sampadawagde
committed
JAVA-626: Added 3 articles
1 parent 25ada35 commit a3fdd99

6 files changed

Lines changed: 176 additions & 0 deletions

File tree

core-java-modules/core-java-lang-math/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@
1010
- [Basic Calculator in Java](https://www.baeldung.com/java-basic-calculator)
1111
- [Overflow and Underflow in Java](https://www.baeldung.com/java-overflow-underflow)
1212
- [Obtaining a Power Set of a Set in Java](https://www.baeldung.com/java-power-set-of-a-set)
13+
- [Calculating Logarithms in Java](https://www.baeldung.com/java-logarithms)
14+
- [Finding Greatest Common Divisor in Java](https://www.baeldung.com/java-greatest-common-divisor)
15+
- [Calculate Percentage in Java](https://www.baeldung.com/java-calculate-percentage)
16+
- More articles: [[<-- next]](/../core-java-lang-math-2)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.algorithms.gcd;
2+
3+
public class GCDImplementation {
4+
5+
public static int gcdByBruteForce(int n1, int n2) {
6+
int gcd = 1;
7+
for (int i = 1; i <= n1 && i <= n2; i++) {
8+
if (n1 % i == 0 && n2 % i == 0) {
9+
gcd = i;
10+
}
11+
}
12+
return gcd;
13+
}
14+
15+
public static int gcdByEuclidsAlgorithm(int n1, int n2) {
16+
if (n2 == 0) {
17+
return n1;
18+
}
19+
return gcdByEuclidsAlgorithm(n2, n1 % n2);
20+
}
21+
22+
public static int gcdBySteinsAlgorithm(int n1, int n2) {
23+
if (n1 == 0) {
24+
return n2;
25+
}
26+
27+
if (n2 == 0) {
28+
return n1;
29+
}
30+
31+
int n;
32+
for (n = 0; ((n1 | n2) & 1) == 0; n++) {
33+
n1 >>= 1;
34+
n2 >>= 1;
35+
}
36+
37+
while ((n1 & 1) == 0) {
38+
n1 >>= 1;
39+
}
40+
41+
do {
42+
while ((n2 & 1) == 0) {
43+
n2 >>= 1;
44+
}
45+
46+
if (n1 > n2) {
47+
int temp = n1;
48+
n1 = n2;
49+
n2 = temp;
50+
}
51+
n2 = (n2 - n1);
52+
} while (n2 != 0);
53+
return n1 << n;
54+
}
55+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.algorithms.percentage;
2+
3+
import java.util.Scanner;
4+
5+
public class PercentageCalculator {
6+
7+
public double calculatePercentage(double obtained,double total){
8+
return obtained*100/total;
9+
}
10+
11+
public static void main(String[] args) {
12+
PercentageCalculator pc = new PercentageCalculator();
13+
Scanner in = new Scanner(System.in);
14+
System.out.println("Enter obtained marks:");
15+
double obtained = in.nextDouble();
16+
System.out.println("Enter total marks:");
17+
double total =in.nextDouble();
18+
System.out.println("Percentage obtained :"+pc.calculatePercentage(obtained,total));
19+
}
20+
21+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.algorithms.gcd;
2+
3+
import org.junit.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
public class GCDImplementationUnitTest {
8+
9+
@Test
10+
public void whenCalculatingGCDByBruteForceMethod_thenCorrect() {
11+
int n1 = 60;
12+
int n2 = 90;
13+
int gcd = GCDImplementation.gcdByBruteForce(n1, n2);
14+
assertThat(gcd).isEqualTo(30);
15+
}
16+
17+
@Test
18+
public void whenCalculatingGCDByEuclidsAlgorithm_thenCorrect() {
19+
int n1 = 60;
20+
int n2 = 90;
21+
int gcd = GCDImplementation.gcdByEuclidsAlgorithm(n1, n2);
22+
assertThat(gcd).isEqualTo(30);
23+
}
24+
25+
@Test
26+
public void whenCalculatingGCDBySteinsAlgorithm_thenCorrect() {
27+
int n1 = 60;
28+
int n2 = 90;
29+
int gcd = GCDImplementation.gcdBySteinsAlgorithm(n1, n2);
30+
assertThat(gcd).isEqualTo(30);
31+
}
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.algorithms.logarithm;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class LogarithmUnitTest {
8+
9+
@Test
10+
public void givenLog10_shouldReturnValidResults() {
11+
assertEquals(Math.log10(100), 2);
12+
assertEquals(Math.log10(1000), 3);
13+
}
14+
15+
@Test
16+
public void givenLogE_shouldReturnValidResults() {
17+
assertEquals(Math.log(Math.E), 1);
18+
assertEquals(Math.log(10), 2.30258, 0.00001);
19+
}
20+
21+
@Test
22+
public void givenCustomLog_shouldReturnValidResults() {
23+
assertEquals(customLog(2, 256), 8);
24+
assertEquals(customLog(10, 100), 2);
25+
}
26+
27+
private static double customLog(double base, double logNumber) {
28+
return Math.log(logNumber) / Math.log(base);
29+
}
30+
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.algorithms.percentage;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class PercentageCalculatorUnitTest {
7+
private PercentageCalculator pc = new PercentageCalculator();
8+
9+
@Test
10+
public void whenPass2Integers_thenShouldCalculatePercentage(){
11+
Assert.assertEquals("Result not as expected",
12+
50.0,pc.calculatePercentage(50,100),0.1);
13+
}
14+
15+
@Test
16+
public void whenPassObtainedMarksAsDouble_thenShouldCalculatePercentage(){
17+
Assert.assertEquals("Result not as expected",5.05,
18+
pc.calculatePercentage(50.5,1000),0.1);
19+
}
20+
21+
@Test
22+
public void whenPassTotalMarksAsDouble_thenShouldCalculatePercentage(){
23+
Assert.assertEquals("Result not as expected",19.6,
24+
pc.calculatePercentage(5,25.5),0.1);
25+
}
26+
27+
@Test
28+
public void whenPass2DoubleNumbers_thenShouldCalculatePercentage(){
29+
Assert.assertEquals("Result not as expected",20,
30+
pc.calculatePercentage(5.5,27.5),0.1);
31+
}
32+
33+
}

0 commit comments

Comments
 (0)