Skip to content

Commit fb34ecb

Browse files
hardik-r-shahpivovarit
authored andcommitted
BAEL-2149 initial commit (eugenp#5186)
* BAEL-2149 initial commit * BAEL-2149 calculate percentage
1 parent aad613a commit fb34ecb

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.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)