From fb34ecb351d966768e14c821adf010952aed6e29 Mon Sep 17 00:00:00 2001 From: hardik-r-shah Date: Sun, 30 Sep 2018 10:14:49 +0530 Subject: [PATCH] BAEL-2149 initial commit (#5186) * BAEL-2149 initial commit * BAEL-2149 calculate percentage --- .../percentage/PercentageCalculator.java | 21 ++++++++++++ .../PercentageCalculatorUnitTest.java | 33 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 java-numbers/src/main/java/com/baeldung/percentage/PercentageCalculator.java create mode 100644 java-numbers/src/test/java/com/baeldung/percentage/PercentageCalculatorUnitTest.java diff --git a/java-numbers/src/main/java/com/baeldung/percentage/PercentageCalculator.java b/java-numbers/src/main/java/com/baeldung/percentage/PercentageCalculator.java new file mode 100644 index 0000000000..e74de2cc67 --- /dev/null +++ b/java-numbers/src/main/java/com/baeldung/percentage/PercentageCalculator.java @@ -0,0 +1,21 @@ +package com.baeldung.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)); + } + +} diff --git a/java-numbers/src/test/java/com/baeldung/percentage/PercentageCalculatorUnitTest.java b/java-numbers/src/test/java/com/baeldung/percentage/PercentageCalculatorUnitTest.java new file mode 100644 index 0000000000..202d4f8112 --- /dev/null +++ b/java-numbers/src/test/java/com/baeldung/percentage/PercentageCalculatorUnitTest.java @@ -0,0 +1,33 @@ +package com.baeldung.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); + } + +}