diff options
-rw-r--r-- | AlgoDesignAndTechniqueEdxJava/sources/MaxPairProduct.java | 59 | ||||
-rw-r--r-- | AlgoDesignAndTechniqueEdxJava/tests/MaxPairProductTest.java | 103 |
2 files changed, 162 insertions, 0 deletions
diff --git a/AlgoDesignAndTechniqueEdxJava/sources/MaxPairProduct.java b/AlgoDesignAndTechniqueEdxJava/sources/MaxPairProduct.java new file mode 100644 index 0000000..18b9521 --- /dev/null +++ b/AlgoDesignAndTechniqueEdxJava/sources/MaxPairProduct.java @@ -0,0 +1,59 @@ +import java.util.Scanner; + +public class MaxPairProduct { + int numberOfIntegers; + int biggest; + int bigger; + + public MaxPairProduct(int size) { + numberOfIntegers = size; + } + + public int getCount() { + return numberOfIntegers; + } + + public static void main(String[] args) { + Scanner s = new Scanner(System.in); + + int n = s.nextInt(); + MaxPairProduct mpp = new MaxPairProduct(n); + for (int i = 0; i<n; i++) { + mpp.newNumber(s.nextInt()); + } + + s.close(); + + System.out.println(mpp.getProduct()); + + } + + public int getBiggest() { + return biggest; + } + + public int getBigger() { + return bigger; + } + + public void newNumber(int i) { + if (i > biggest) { + bigger = biggest; + biggest = i; + } else if (i == biggest) { + bigger = i; + } else if (i > bigger) { + bigger = i; + } + } + + public long getProduct() { + if (numberOfIntegers == 0) { + return (long) 0; + } else if (numberOfIntegers == 1) { + return (long) biggest; + } + return (long) biggest * (long) bigger; + } + +} diff --git a/AlgoDesignAndTechniqueEdxJava/tests/MaxPairProductTest.java b/AlgoDesignAndTechniqueEdxJava/tests/MaxPairProductTest.java new file mode 100644 index 0000000..7f0b313 --- /dev/null +++ b/AlgoDesignAndTechniqueEdxJava/tests/MaxPairProductTest.java @@ -0,0 +1,103 @@ + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +public class MaxPairProductTest { + + @Test + void testMaxPairProductSize() { + MaxPairProduct mpp = new MaxPairProduct(5); + assertEquals(5, mpp.getCount()); + } + + @Test + void testMaxPairProductBiggest0() { + MaxPairProduct mpp = new MaxPairProduct(1); + assertEquals(0, mpp.getBiggest()); + } + + @Test + void testMaxPairProductBigger0() { + MaxPairProduct mpp = new MaxPairProduct(1); + assertEquals(0, mpp.getBigger()); + } + + @Test + void testMaxPairProductNewNumber1() { + MaxPairProduct mpp = new MaxPairProduct(1); + mpp.newNumber(1); + assertEquals(0, mpp.getBigger()); + assertEquals(1, mpp.getBiggest()); + assertEquals(1, mpp.getProduct()); + } + + @Test + void testMaxPairProductNewNumber2() { + MaxPairProduct mpp = new MaxPairProduct(2); + mpp.newNumber(1); + mpp.newNumber(1); + assertEquals(1, mpp.getBigger()); + assertEquals(1, mpp.getBiggest()); + assertEquals(1, mpp.getProduct()); + } + + @Test + void testMaxPairProductNewNumber2_1() { + MaxPairProduct mpp = new MaxPairProduct(2); + mpp.newNumber(1); + mpp.newNumber(2); + assertEquals(1, mpp.getBigger()); + assertEquals(2, mpp.getBiggest()); + assertEquals(2, mpp.getProduct()); + } + + @Test + void testMaxPairProductNewNumber3() { + MaxPairProduct mpp = new MaxPairProduct(3); + mpp.newNumber(1); + mpp.newNumber(2); + mpp.newNumber(2); + assertEquals(2, mpp.getBigger()); + assertEquals(2, mpp.getBiggest()); + assertEquals(4, mpp.getProduct()); + } + + @Test + void testMaxPairProductNewNumber3_1() { + MaxPairProduct mpp = new MaxPairProduct(3); + mpp.newNumber(1); + mpp.newNumber(2); + mpp.newNumber(3); + assertEquals(2, mpp.getBigger()); + assertEquals(3, mpp.getBiggest()); + assertEquals(6, mpp.getProduct()); + } + + @Test + void testMaxPairProductNewNumber10() { + MaxPairProduct mpp = new MaxPairProduct(10); + mpp.newNumber(7); + mpp.newNumber(5); + mpp.newNumber(14); + mpp.newNumber(2); + mpp.newNumber(8); + mpp.newNumber(8); + mpp.newNumber(10); + mpp.newNumber(1); + mpp.newNumber(2); + mpp.newNumber(3); + assertEquals(140, mpp.getProduct()); + } + + @Test + void testMaxPairProductNewNumber2_2() { + MaxPairProduct mpp = new MaxPairProduct(2); + mpp.newNumber(100000); + mpp.newNumber(90000); + assertEquals(90000, mpp.getBigger()); + assertEquals(100000, mpp.getBiggest()); + assertEquals(9000000000L, mpp.getProduct()); + } + +} |