This is the complete solution to find the Triplets 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 TripletsSumDivBy9Intf {
/* Define the functional interface with SAM 'ParseEachWord' */
List<List<Integer>> ParseAnArrayInput(Integer arrVal[]);
/* Define the static method 'displayMsg' */
static void displayMsg(List<List<Integer>> tripletsList) {
System.out.println(tripletsList);
}
}
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.Collectors;
import java.util.stream.IntStream;
class ArrayInput {
private Integer[] arrIntVals;
/* Declare the logger object for the class to display the log messages */
static Logger logger = Logger.getLogger(ArrayInput.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 TripletsSumDivBy9_Using_Arrays extends ArrayInput 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>> allTripletsList = 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 three successive numbers divisible by 9
*/
for (int i = 0; i < sizeOfArr; i++) {
for (int j = i + 1; j < sizeOfArr; j++) {
for (int k = j + 1; k < sizeOfArr; k++) {
int sum = getArrIntVals()[i] + getArrIntVals()[j] + getArrIntVals()[k];
if (sum % 9 == 0)
allTripletsList.add(new ArrayList<Integer>(
Arrays.asList(getArrIntVals()[i], getArrIntVals()[j], getArrIntVals()[k])));
}
}
}
} else
logger.info("The given Input Array is null");
TripletsSumDivBy9Intf.displayMsg(allTripletsList);
return allTripletsList;
}
}
class TripletsSumDivBy9_Using_Predicates extends ArrayInput 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>> allTripletsList = 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 three successive numbers divisible by 9
*/
for (int i = 0; i < sizeOfArr; i++) {
for (int j = i + 1; j < sizeOfArr; j++) {
for (int k = j + 1; k < sizeOfArr; k++) {
if (div9.test(getArrIntVals()[i] + getArrIntVals()[j] + getArrIntVals()[k]))
allTripletsList.add(new ArrayList<Integer>(
Arrays.asList(getArrIntVals()[i], getArrIntVals()[j], getArrIntVals()[k])));
}
}
}
} else
logger.info("The given Input Array is null");
TripletsSumDivBy9Intf.displayMsg(allTripletsList);
return allTripletsList;
}
}
class TripletsSumDivBy9_Using_Stream_and_Predicates extends ArrayInput 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>> allTripletsList = 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 (3 levels), flatMap, and filter return list
* objects as triplets, the criteria is sum of each triplet is divisible by 9
*/
allTripletsList = IntStream.range(0, sizeOfArr).boxed().flatMap(i -> IntStream.range(i + 1, sizeOfArr)
.boxed()
.flatMap(j -> IntStream.range(j + 1, sizeOfArr)
.filter(k -> div9.test(getArrIntVals()[i] + getArrIntVals()[j] + getArrIntVals()[k]))
.mapToObj(k -> Arrays.asList(getArrIntVals()[i], getArrIntVals()[j], getArrIntVals()[k]))))
.collect(Collectors.toList());
} else
logger.info("The given Input Array is null");
TripletsSumDivBy9Intf.displayMsg(allTripletsList);
return allTripletsList;
}
}
public class TripletsSumDivBy9J8 {
public static void main(String args[]) {
/*
* create the class object TripletsSumDivBy9_Using_Arrays, and call the method
* ParseAnArrayInput
*/
TripletsSumDivBy9_Using_Arrays TSDBUA = new TripletsSumDivBy9_Using_Arrays();
Integer[] intArr = { 14, 20, 11, 5, 29, 17, 58 };
TSDBUA.ParseAnArrayInput(intArr);
TSDBUA.ParseAnArrayInput(null);
/*
* create the class object TripletsSumDivBy9_Using_Predicates, and call the
* method ParseAnArrayInput
*/
TripletsSumDivBy9_Using_Predicates TSDBUP = new TripletsSumDivBy9_Using_Predicates();
TSDBUP.ParseAnArrayInput(new Integer[] { 90, 9, 0, 54, 32, 51 });
TSDBUP.ParseAnArrayInput(null);
/*
* create the class object TripletsSumDivBy9_Using_Stream_and_Predicates, and
* call the method ParseAnArrayInput
*/
TripletsSumDivBy9_Using_Stream_and_Predicates TSDBUSP = new TripletsSumDivBy9_Using_Stream_and_Predicates();
TSDBUSP.ParseAnArrayInput(new Integer[] { 4, 13, 10, 15, 2, 37, 8 });
TSDBUSP.ParseAnArrayInput(null);
}
}
OUTPUT:
Mar 10, 2023 12:03:41 PM pack1.TripletsSumDivBy9_Using_Arrays ParseAnArrayInput
INFO: This method 'ParseAnArrayInput' called from the Class 'pack1.TripletsSumDivBy9J8'
Mar 10, 2023 12:03:41 PM pack1.ArrayInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.TripletsSumDivBy9_Using_Arrays'
Mar 10, 2023 12:03:41 PM pack1.ArrayInput validateInputs
INFO: The provided Input validated Successfully
The given input Array values are
[ 14 20 11 5 29 17 58 ]
[[14, 20, 11], [14, 20, 29], [14, 11, 29], [14, 5, 17], [20, 11, 5], [20, 5, 29], [11, 5, 29]]
Mar 10, 2023 12:03:41 PM pack1.TripletsSumDivBy9_Using_Arrays ParseAnArrayInput
INFO: This method 'ParseAnArrayInput' called from the Class 'pack1.TripletsSumDivBy9J8'
Mar 10, 2023 12:03:41 PM pack1.ArrayInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.TripletsSumDivBy9_Using_Arrays'
[]
Mar 10, 2023 12:03:41 PM pack1.TripletsSumDivBy9_Using_Arrays ParseAnArrayInput
INFO: The given Input Array is null
Mar 10, 2023 12:03:41 PM pack1.TripletsSumDivBy9_Using_Predicates ParseAnArrayInput
INFO: This method 'ParseAnArrayInput' called from the Class 'pack1.TripletsSumDivBy9J8'
Mar 10, 2023 12:03:41 PM pack1.ArrayInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.TripletsSumDivBy9_Using_Predicates'
Mar 10, 2023 12:03:41 PM pack1.ArrayInput validateInputs
INFO: The provided Input validated Successfully
The given input Array values are
[ 90 9 0 54 32 51 ]
[[90, 9, 0], [90, 9, 54], [90, 0, 54], [9, 0, 54]]
Mar 10, 2023 12:03:41 PM pack1.TripletsSumDivBy9_Using_Predicates ParseAnArrayInput
INFO: This method 'ParseAnArrayInput' called from the Class 'pack1.TripletsSumDivBy9J8'
Mar 10, 2023 12:03:41 PM pack1.ArrayInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.TripletsSumDivBy9_Using_Predicates'
Mar 10, 2023 12:03:41 PM pack1.TripletsSumDivBy9_Using_Predicates ParseAnArrayInput
INFO: The given Input Array is null
[]
Mar 10, 2023 12:03:41 PM pack1.TripletsSumDivBy9_Using_Stream_and_Predicates ParseAnArrayInput
INFO: This method 'ParseAnArrayInput' called from the Class 'pack1.TripletsSumDivBy9J8'
Mar 10, 2023 12:03:41 PM pack1.ArrayInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.TripletsSumDivBy9_Using_Stream_and_Predicates'
Mar 10, 2023 12:03:41 PM pack1.ArrayInput validateInputs
INFO: The provided Input validated Successfully
The given input Array values are
4 13 10 15 2 37 8
The triplets of numbers which are divisible by 9
[[4, 13, 10], [4, 13, 37], [4, 15, 8], [13, 15, 8], [10, 15, 2], [15, 2, 37]]
Mar 10, 2023 12:03:41 PM pack1.TripletsSumDivBy9_Using_Stream_and_Predicates ParseAnArrayInput
INFO: This method 'ParseAnArrayInput' called from the Class 'pack1.TripletsSumDivBy9J8'
Mar 10, 2023 12:03:41 PM pack1.ArrayInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.TripletsSumDivBy9_Using_Stream_and_Predicates'
Mar 10, 2023 12:03:41 PM pack1.TripletsSumDivBy9_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/03/10/junit-test-finding-triplets-sum-divisible-by-9/

Leave a comment