This is the complete solution to find the Doublets whose sum is divisible by 9. This solution is designed to to solve the problem using an interface, super class and two derived classes implementing the interface.
package pack1;
import java.util.List;
public interface DoubletsSumDivBy9Intf {
/* Define the functional interface with SAM 'ParseEachWord' */
public List<List<Integer>> ParseAnArrayInput(Integer arrVal[]);
/* Define the static method 'displayMsg' */
static void displayMsg(List<List<Integer>> doubletsList) {
System.out.println(doubletsList);
}
}
package pack1;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.logging.Logger;
import java.util.stream.IntStream;
class ArrayInputNum {
private Integer[] arrIntVals;
/* Declare the logger object for the class to display the log messages */
static Logger logger = Logger.getLogger(ArrayInputNum.class.getName());
public boolean validateInputs(Integer[] inputAnIntegerArray) {
/* Fetch the class object's name from which this method is called */
String cName = Thread.currentThread().getStackTrace()[2].getClassName();
String mName = "validateInputs()";
logger.info("This method '" + mName + "' called from the Class '" + cName + "'");
/* Validating the function parameters using Optional Integer Array data type */
Optional<Integer[]> arrInput = Optional.ofNullable(inputAnIntegerArray);
/*
* Fetch the value of the Optional Integer Array parameters using 'isPresent'
* condition check. If the value is available use 'get' method otherwise simply
* return 'false' to stop the program execution without proceeding further.
*/
if (arrInput.isPresent()) {
setArrIntVals(arrInput.get());
} else {
return false;
}
logger.info("The provided Input validated Successfully");
return true;
}
/* Define getter and setters for arrIntVals Array variable */
public Integer[] getArrIntVals() {
return arrIntVals;
}
public void setArrIntVals(Integer[] arrIntVals) {
this.arrIntVals = arrIntVals;
}
}
class DoubletsSumDivBy9_Using_Arrays extends ArrayInputNum implements TripletsSumDivBy9Intf {
public List<List<Integer>> ParseAnArrayInput(Integer[] inpArr) {
/* Fetch the class object's name from which this method is called */
String cName = Thread.currentThread().getStackTrace()[2].getClassName();
String mName = "ParseAnArrayInput";
logger.info("This method '" + mName + "' called from the Class '" + cName + "'");
/* Define an Array List of List of Integers */
List<List<Integer>> allDoubletsList = new ArrayList<List<Integer>>();
if (validateInputs(inpArr)) {
int sizeOfArr = getArrIntVals().length;
System.out.println("The given input Array values are");
/* Traversing an array to display the values */
System.out.print("[ ");
for (int i = 0; i < getArrIntVals().length; i++)
System.out.print(getArrIntVals()[i] + " ");
System.out.println("]");
/*
* Traverse through an array using nested for loop, and check whether the sum of
* the any two successive numbers divisible by 9
*/
for (int i = 0; i < sizeOfArr; i++) {
for (int j = i + 1; j < sizeOfArr; j++) {
int sum = getArrIntVals()[i] + getArrIntVals()[j];
if (sum % 9 == 0)
allDoubletsList.add(new ArrayList<Integer>(
Arrays.asList(getArrIntVals()[i], getArrIntVals()[j])));
}
}
} else
logger.info("The given Input Array is null");
DoubletsSumDivBy9Intf.displayMsg(allDoubletsList);
return allDoubletsList;
}
}
class DoubletsSumDivBy9_Using_Predicates extends ArrayInputNum implements TripletsSumDivBy9Intf {
public List<List<Integer>> ParseAnArrayInput(Integer[] inpArr) {
/* Fetch the class object's name from which this method is called */
String cName = Thread.currentThread().getStackTrace()[2].getClassName();
String mName = "ParseAnArrayInput";
logger.info("This method '" + mName + "' called from the Class '" + cName + "'");
/*
* Define the Predicate which returns true/false if the provided number
* divisible by 9
*/
Predicate<Integer> div9 = (i) -> i % 9 == 0;
/* Define an Array List of List of Integers */
List<List<Integer>> allDoubletsList = new ArrayList<List<Integer>>();
if (validateInputs(inpArr)) {
int sizeOfArr = getArrIntVals().length;
System.out.println("The given input Array values are");
/* Traversing an array to display the values */
System.out.print("[ ");
for (int i = 0; i < inpArr.length; i++)
System.out.print(inpArr[i] + " ");
System.out.println("]");
/*
* Traverse through an array using nested for loop, and check whether the sum of
* the any two successive numbers divisible by 9
*/
for (int i = 0; i < sizeOfArr; i++) {
for (int j = i + 1; j < sizeOfArr; j++) {
if (div9.test(getArrIntVals()[i] + getArrIntVals()[j]))
allDoubletsList.add(new ArrayList<Integer>(
Arrays.asList(getArrIntVals()[i], getArrIntVals()[j])));
}
}
} else
logger.info("The given Input Array is null");
DoubletsSumDivBy9Intf.displayMsg(allDoubletsList);
return allDoubletsList;
}
}
class DoubletsSumDivBy9_Using_Stream_and_Predicates extends ArrayInputNum implements TripletsSumDivBy9Intf {
public List<List<Integer>> ParseAnArrayInput(Integer[] inpArr) {
/* Fetch the class object's name from which this method is called */
String cName = Thread.currentThread().getStackTrace()[2].getClassName();
String mName = "ParseAnArrayInput";
logger.info("This method '" + mName + "' called from the Class '" + cName + "'");
/*
* Define the Predicate which returns true/false if the provided number
* divisible by 9
*/
Predicate<Integer> div9 = (i) -> i % 9 == 0;
/* Define an Array List of List of Integers */
List<List<Integer>> allDoubletsList = new ArrayList<List<Integer>>();
if (validateInputs(inpArr)) {
int sizeOfArr = getArrIntVals().length;
System.out.println("The given input Array values are");
/* Traversing the array to display the values */
Arrays.stream(inpArr).forEach(x -> System.out.print(x + " "));
System.out.println("\nThe triplets of numbers which are divisible by 9");
/*
* Using the nested IntStream (2 levels), foreach construct, and the lambda expression.
* The criteria is sum of each doublet is divisible by 9
*/
IntStream.range(0, sizeOfArr).forEach(i -> {
IntStream.range(i+1, sizeOfArr)
.forEach(j -> {
if (div9.test(getArrIntVals()[i]+getArrIntVals()[j])) {
allDoubletsList.add(Arrays.asList(getArrIntVals()[i], getArrIntVals()[j]));
}
});
});
} else
logger.info("The given Input Array is null");
DoubletsSumDivBy9Intf.displayMsg(allDoubletsList);
return allDoubletsList;
}
}
public class DoubletsSumDivBy9J9 {
public static void main(String args[]) {
/*
* create the class object DoubletsSumDivBy9_Using_Arrays, and call the method
* ParseAnArrayInput
*/
DoubletsSumDivBy9_Using_Arrays DSDBUA = new DoubletsSumDivBy9_Using_Arrays();
Integer[] intArr = { 14, 20, 11, 5, 29, 17, 58 };
DSDBUA.ParseAnArrayInput(intArr);
DSDBUA.ParseAnArrayInput(null);
/*
* create the class object DoubletsSumDivBy9_Using_Predicates, and call the
* method ParseAnArrayInput
*/
DoubletsSumDivBy9_Using_Predicates DSDBUP = new DoubletsSumDivBy9_Using_Predicates();
DSDBUP.ParseAnArrayInput(new Integer[] { 90, 9, 0, 54, 32, 51 });
DSDBUP.ParseAnArrayInput(null);
/*
* create the class object DoubletsSumDivBy9_Using_Stream_and_Predicates, and
* call the method ParseAnArrayInput
*/
DoubletsSumDivBy9_Using_Stream_and_Predicates DSDBUSP = new DoubletsSumDivBy9_Using_Stream_and_Predicates();
DSDBUSP.ParseAnArrayInput(new Integer[] { 4, 14, 10, 15, 2, 37, 5 });
DSDBUSP.ParseAnArrayInput(null);
}
}
OUTPUT:
Jun 14, 2023 7:30:27 PM pack1.DoubletsSumDivBy9_Using_Arrays ParseAnArrayInput
INFO: This method 'ParseAnArrayInput' called from the Class 'pack1.DoubletsSumDivBy9J9'
Jun 14, 2023 7:30:27 PM pack1.ArrayInputNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.DoubletsSumDivBy9_Using_Arrays'
Jun 14, 2023 7:30:27 PM pack1.ArrayInputNum validateInputs
INFO: The provided Input validated Successfully
The given input Array values are
[ 14 20 11 5 29 17 58 ]
[[14, 58], [5, 58]]
Jun 14, 2023 7:30:27 PM pack1.DoubletsSumDivBy9_Using_Arrays ParseAnArrayInput
INFO: This method 'ParseAnArrayInput' called from the Class 'pack1.DoubletsSumDivBy9J9'
Jun 14, 2023 7:30:27 PM pack1.ArrayInputNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.DoubletsSumDivBy9_Using_Arrays'
Jun 14, 2023 7:30:27 PM pack1.DoubletsSumDivBy9_Using_Arrays ParseAnArrayInput
INFO: The given Input Array is null
[]
Jun 14, 2023 7:30:27 PM pack1.DoubletsSumDivBy9_Using_Predicates ParseAnArrayInput
INFO: This method 'ParseAnArrayInput' called from the Class 'pack1.DoubletsSumDivBy9J9'
Jun 14, 2023 7:30:27 PM pack1.ArrayInputNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.DoubletsSumDivBy9_Using_Predicates'
Jun 14, 2023 7:30:27 PM pack1.ArrayInputNum validateInputs
INFO: The provided Input validated Successfully
The given input Array values are
[ 90 9 0 54 32 51 ]
[[90, 9], [90, 0], [90, 54], [9, 0], [9, 54], [0, 54]]
Jun 14, 2023 7:30:27 PM pack1.DoubletsSumDivBy9_Using_Predicates ParseAnArrayInput
INFO: This method 'ParseAnArrayInput' called from the Class 'pack1.DoubletsSumDivBy9J9'
Jun 14, 2023 7:30:27 PM pack1.ArrayInputNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.DoubletsSumDivBy9_Using_Predicates'
Jun 14, 2023 7:30:27 PM pack1.DoubletsSumDivBy9_Using_Predicates ParseAnArrayInput
INFO: The given Input Array is null
[]
Jun 14, 2023 7:30:27 PM pack1.DoubletsSumDivBy9_Using_Stream_and_Predicates ParseAnArrayInput
INFO: This method 'ParseAnArrayInput' called from the Class 'pack1.DoubletsSumDivBy9J9'
Jun 14, 2023 7:30:27 PM pack1.ArrayInputNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.DoubletsSumDivBy9_Using_Stream_and_Predicates'
Jun 14, 2023 7:30:27 PM pack1.ArrayInputNum validateInputs
INFO: The provided Input validated Successfully
The given input Array values are
4 14 10 15 2 37 5
The triplets of numbers which are divisible by 9
[[4, 14], [4, 5]]
Jun 14, 2023 7:30:27 PM pack1.DoubletsSumDivBy9_Using_Stream_and_Predicates ParseAnArrayInput
INFO: This method 'ParseAnArrayInput' called from the Class 'pack1.DoubletsSumDivBy9J9'
Jun 14, 2023 7:30:27 PM pack1.ArrayInputNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.DoubletsSumDivBy9_Using_Stream_and_Predicates'
Jun 14, 2023 7:30:27 PM pack1.DoubletsSumDivBy9_Using_Stream_and_Predicates ParseAnArrayInput
INFO: The given Input Array is null
[]
The link to the JUnit5 test for this solution is here – https://atomic-temporary-185308886.wpcomstaging.com/2023/06/15/junit-test-doublets/

Leave a comment