summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHaidong Ji2018-08-06 22:15:40 -0500
committerHaidong Ji2018-08-06 22:15:40 -0500
commiteb3a6e192072e9ddb82c5a9cf283196558d61e45 (patch)
treeb41d22b922522e4c8d6099af27611fd6c0cfbc9c
parentd9978c0c13659c82ff288035474e485f6ee44c14 (diff)
Maximum Pairwise Product done!
-rw-r--r--AlgoDesignAndTechniqueEdxJava/sources/MaxPairProduct.java59
-rw-r--r--AlgoDesignAndTechniqueEdxJava/tests/MaxPairProductTest.java103
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());
+ }
+
+}