JAVA-626: Added 3 articles
This commit is contained in:
parent
cac1779e0d
commit
05e0753a9c
@ -10,3 +10,7 @@
|
||||
- [Basic Calculator in Java](https://www.baeldung.com/java-basic-calculator)
|
||||
- [Overflow and Underflow in Java](https://www.baeldung.com/java-overflow-underflow)
|
||||
- [Obtaining a Power Set of a Set in Java](https://www.baeldung.com/java-power-set-of-a-set)
|
||||
- [Calculating Logarithms in Java](https://www.baeldung.com/java-logarithms)
|
||||
- [Finding Greatest Common Divisor in Java](https://www.baeldung.com/java-greatest-common-divisor)
|
||||
- [Calculate Percentage in Java](https://www.baeldung.com/java-calculate-percentage)
|
||||
- More articles: [[<-- next]](/../core-java-lang-math-2)
|
||||
|
@ -0,0 +1,55 @@
|
||||
package com.baeldung.algorithms.gcd;
|
||||
|
||||
public class GCDImplementation {
|
||||
|
||||
public static int gcdByBruteForce(int n1, int n2) {
|
||||
int gcd = 1;
|
||||
for (int i = 1; i <= n1 && i <= n2; i++) {
|
||||
if (n1 % i == 0 && n2 % i == 0) {
|
||||
gcd = i;
|
||||
}
|
||||
}
|
||||
return gcd;
|
||||
}
|
||||
|
||||
public static int gcdByEuclidsAlgorithm(int n1, int n2) {
|
||||
if (n2 == 0) {
|
||||
return n1;
|
||||
}
|
||||
return gcdByEuclidsAlgorithm(n2, n1 % n2);
|
||||
}
|
||||
|
||||
public static int gcdBySteinsAlgorithm(int n1, int n2) {
|
||||
if (n1 == 0) {
|
||||
return n2;
|
||||
}
|
||||
|
||||
if (n2 == 0) {
|
||||
return n1;
|
||||
}
|
||||
|
||||
int n;
|
||||
for (n = 0; ((n1 | n2) & 1) == 0; n++) {
|
||||
n1 >>= 1;
|
||||
n2 >>= 1;
|
||||
}
|
||||
|
||||
while ((n1 & 1) == 0) {
|
||||
n1 >>= 1;
|
||||
}
|
||||
|
||||
do {
|
||||
while ((n2 & 1) == 0) {
|
||||
n2 >>= 1;
|
||||
}
|
||||
|
||||
if (n1 > n2) {
|
||||
int temp = n1;
|
||||
n1 = n2;
|
||||
n2 = temp;
|
||||
}
|
||||
n2 = (n2 - n1);
|
||||
} while (n2 != 0);
|
||||
return n1 << n;
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.baeldung.algorithms.percentage;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class PercentageCalculator {
|
||||
|
||||
public double calculatePercentage(double obtained,double total){
|
||||
return obtained*100/total;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
PercentageCalculator pc = new PercentageCalculator();
|
||||
Scanner in = new Scanner(System.in);
|
||||
System.out.println("Enter obtained marks:");
|
||||
double obtained = in.nextDouble();
|
||||
System.out.println("Enter total marks:");
|
||||
double total =in.nextDouble();
|
||||
System.out.println("Percentage obtained :"+pc.calculatePercentage(obtained,total));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.baeldung.algorithms.gcd;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class GCDImplementationUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenCalculatingGCDByBruteForceMethod_thenCorrect() {
|
||||
int n1 = 60;
|
||||
int n2 = 90;
|
||||
int gcd = GCDImplementation.gcdByBruteForce(n1, n2);
|
||||
assertThat(gcd).isEqualTo(30);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCalculatingGCDByEuclidsAlgorithm_thenCorrect() {
|
||||
int n1 = 60;
|
||||
int n2 = 90;
|
||||
int gcd = GCDImplementation.gcdByEuclidsAlgorithm(n1, n2);
|
||||
assertThat(gcd).isEqualTo(30);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCalculatingGCDBySteinsAlgorithm_thenCorrect() {
|
||||
int n1 = 60;
|
||||
int n2 = 90;
|
||||
int gcd = GCDImplementation.gcdBySteinsAlgorithm(n1, n2);
|
||||
assertThat(gcd).isEqualTo(30);
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.baeldung.algorithms.logarithm;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class LogarithmUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenLog10_shouldReturnValidResults() {
|
||||
assertEquals(Math.log10(100), 2);
|
||||
assertEquals(Math.log10(1000), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLogE_shouldReturnValidResults() {
|
||||
assertEquals(Math.log(Math.E), 1);
|
||||
assertEquals(Math.log(10), 2.30258, 0.00001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCustomLog_shouldReturnValidResults() {
|
||||
assertEquals(customLog(2, 256), 8);
|
||||
assertEquals(customLog(10, 100), 2);
|
||||
}
|
||||
|
||||
private static double customLog(double base, double logNumber) {
|
||||
return Math.log(logNumber) / Math.log(base);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.baeldung.algorithms.percentage;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PercentageCalculatorUnitTest {
|
||||
private PercentageCalculator pc = new PercentageCalculator();
|
||||
|
||||
@Test
|
||||
public void whenPass2Integers_thenShouldCalculatePercentage(){
|
||||
Assert.assertEquals("Result not as expected",
|
||||
50.0,pc.calculatePercentage(50,100),0.1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPassObtainedMarksAsDouble_thenShouldCalculatePercentage(){
|
||||
Assert.assertEquals("Result not as expected",5.05,
|
||||
pc.calculatePercentage(50.5,1000),0.1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPassTotalMarksAsDouble_thenShouldCalculatePercentage(){
|
||||
Assert.assertEquals("Result not as expected",19.6,
|
||||
pc.calculatePercentage(5,25.5),0.1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPass2DoubleNumbers_thenShouldCalculatePercentage(){
|
||||
Assert.assertEquals("Result not as expected",20,
|
||||
pc.calculatePercentage(5.5,27.5),0.1);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user