This is the complete solution to check whether a given number is ArmStrong number or not. This solution is designed to solve the problem using an interface, super class and three derived classes implementing the interface.
package pack1;
public interface ArmStrongNumberIntf {
/* Define the functional interface with SAM 'RdInput' */
public Boolean RdInput(Integer intVal);
/* Define the static method 'displayMsg' */
static void displayMsg(Integer inpVal, boolean isArmStrong) {
String res = isArmStrong ? "" : "Not ";
System.out.println("The number " + inpVal + " is " + res + "ArmStrong Number");
System.out.println();
}
}
package pack1;
import java.util.ArrayList;
import java.util.Optional;
import java.util.logging.Logger;
class InputTheNumber {
private int inputIntvalue;
/* Declare the logger object for the class to display the log messages */
static Logger logger = Logger.getLogger(InputTheNumber.class.getName());
public boolean validateInputs(Integer inputAnInteger) {
/* 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> inputVal = Optional.ofNullable(inputAnInteger);
/*
* Fetch the value of the Optional Integer inputAnInteger 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 (inputVal.isPresent()) {
setInputIntvalue(inputVal.get());
System.out.println("The given number is " + getInputIntvalue());
} else
return false;
logger.info("The provided Input validated Successfully");
return true;
}
/* Define getter and setters for an inputIntvalue int variable */
public int getInputIntvalue() {
return inputIntvalue;
}
public void setInputIntvalue(int inputIntvalue) {
this.inputIntvalue = inputIntvalue;
}
}
class ArmStrongNumber_Using_String extends InputTheNumber implements ArmStrongNumberIntf {
public Boolean RdInput(Integer inpVal) {
/* Fetch the class object's name from which this method is called */
String cName = Thread.currentThread().getStackTrace()[2].getClassName();
String mName = "RdInput";
logger.info("This method '" + mName + "' called from the Class '" + cName + "'");
/* Initialize the sumCheck value to 0, and initialize isArmStrong to false */
int sumCheck = 0;
boolean isArmStrong = false;
if (validateInputs(inpVal)) {
String numToStr = String.valueOf(getInputIntvalue());
int numLen = numToStr.length();
// Calculate the sum of each digit's cube power value from any side
for (; numLen > 0; numLen--)
if (Character.isDigit(numToStr.charAt(numLen - 1)))
sumCheck = (int) (sumCheck + Math.pow(Character.getNumericValue(numToStr.charAt(numLen - 1)), 3));
// Compare the sumCheck with actual number
if (sumCheck == inpVal)
isArmStrong = true;
} else
logger.info("The given Input number is null");
ArmStrongNumberIntf.displayMsg(inpVal, isArmStrong);
return isArmStrong;
}
}
class ArmStrongNumber_Using_CoreLogic extends InputTheNumber implements ArmStrongNumberIntf {
public Boolean RdInput(Integer inpVal) {
/* Fetch the class object's name from which this method is called */
String cName = Thread.currentThread().getStackTrace()[2].getClassName();
String mName = "RdInput";
logger.info("This method '" + mName + "' called from the Class '" + cName + "'");
/* Initialize the sumCheck value to 0, and initialize isArmStrong to false */
int sumCheck = 0, remValue;
boolean isArmStrong = false;
if (validateInputs(inpVal)) {
int storeNum = getInputIntvalue();
while (storeNum != 0) {
remValue = storeNum % 10;
sumCheck = sumCheck + (remValue * remValue * remValue);
storeNum = storeNum / 10;
}
// Compare the sumCheck with actual number
if (sumCheck == inpVal)
isArmStrong = true;
} else
logger.info("The given Input number is null");
ArmStrongNumberIntf.displayMsg(inpVal, isArmStrong);
return isArmStrong;
}
}
class ArmStrongNumber_Using_Stream extends InputTheNumber implements ArmStrongNumberIntf {
public Boolean RdInput(Integer inpVal) {
/* Fetch the class object's name from which this method is called */
String cName = Thread.currentThread().getStackTrace()[2].getClassName();
String mName = "RdInput";
logger.info("This method '" + mName + "' called from the Class '" + cName + "'");
/* Initialize the flag isArmStrong to false */
boolean isArmStrong = false;
if (validateInputs(inpVal)) {
String strVal = String.valueOf(getInputIntvalue());
ArrayList<Integer> arrOfDigits = new ArrayList<Integer>();
for (char ch : strVal.toCharArray())
arrOfDigits.add(Character.getNumericValue(ch));
// Applying map and reduce method to find our sum of cubes
int sumOfCubes = arrOfDigits.stream().map(a -> a * a * a).reduce(0, (x, y) -> x + y);
if (sumOfCubes == inpVal)
isArmStrong = true;
} else
logger.info("The given Input number is null");
ArmStrongNumberIntf.displayMsg(inpVal, isArmStrong);
return isArmStrong;
}
}
public class ArmstrongNumberJ8 {
public static void main(String args[]) {
/*
* create the class object ArmStrongNumber_Using_String, and call the method
* RdInput
*/
ArmStrongNumber_Using_String ANUS = new ArmStrongNumber_Using_String();
ANUS.RdInput(154);
ANUS.RdInput(null);
/*
* create the class object ArmStrongNumber_Using_CoreLogic, and call the method
* RdInput
*/
ArmStrongNumber_Using_CoreLogic ANUCL = new ArmStrongNumber_Using_CoreLogic();
ANUCL.RdInput(407);
ANUCL.RdInput(null);
/*
* create the class object ArmStrongNumber_Using_Stream, and call the method
* RdInput
*/
ArmStrongNumber_Using_Stream ANUStream = new ArmStrongNumber_Using_Stream();
ANUStream.RdInput(371);
ANUStream.RdInput(null);
}
}
OUTPUT:
Apr 03, 2023 3:28:44 PM pack1.ArmStrongNumber_Using_String RdInput
INFO: This method 'RdInput' called from the Class 'pack1.ArmstrongNumberJ8'
Apr 03, 2023 3:28:44 PM pack1.InputTheNumber validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ArmStrongNumber_Using_String'
The given number is 154
Apr 03, 2023 3:28:44 PM pack1.InputTheNumber validateInputs
INFO: The provided Input validated Successfully
The number 154 is Not ArmStrong Number
Apr 03, 2023 3:28:44 PM pack1.ArmStrongNumber_Using_String RdInput
INFO: This method 'RdInput' called from the Class 'pack1.ArmstrongNumberJ8'
Apr 03, 2023 3:28:44 PM pack1.InputTheNumber validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ArmStrongNumber_Using_String'
Apr 03, 2023 3:28:44 PM pack1.ArmStrongNumber_Using_String RdInput
INFO: The given Input number is null
The number null is Not ArmStrong Number
Apr 03, 2023 3:28:44 PM pack1.ArmStrongNumber_Using_CoreLogic RdInput
INFO: This method 'RdInput' called from the Class 'pack1.ArmstrongNumberJ8'
Apr 03, 2023 3:28:44 PM pack1.InputTheNumber validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ArmStrongNumber_Using_CoreLogic'
The given number is 407
Apr 03, 2023 3:28:44 PM pack1.InputTheNumber validateInputs
INFO: The provided Input validated Successfully
The number 407 is ArmStrong Number
Apr 03, 2023 3:28:44 PM pack1.ArmStrongNumber_Using_CoreLogic RdInput
INFO: This method 'RdInput' called from the Class 'pack1.ArmstrongNumberJ8'
Apr 03, 2023 3:28:44 PM pack1.InputTheNumber validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ArmStrongNumber_Using_CoreLogic'
Apr 03, 2023 3:28:44 PM pack1.ArmStrongNumber_Using_CoreLogic RdInput
INFO: The given Input number is null
The number null is Not ArmStrong Number
Apr 03, 2023 3:28:44 PM pack1.ArmStrongNumber_Using_Stream RdInput
INFO: This method 'RdInput' called from the Class 'pack1.ArmstrongNumberJ8'
Apr 03, 2023 3:28:44 PM pack1.InputTheNumber validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ArmStrongNumber_Using_Stream'
The given number is 371
Apr 03, 2023 3:28:44 PM pack1.InputTheNumber validateInputs
INFO: The provided Input validated Successfully
The number 371 is ArmStrong Number
Apr 03, 2023 3:28:45 PM pack1.ArmStrongNumber_Using_Stream RdInput
INFO: This method 'RdInput' called from the Class 'pack1.ArmstrongNumberJ8'
Apr 03, 2023 3:28:45 PM pack1.InputTheNumber validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ArmStrongNumber_Using_Stream'
Apr 03, 2023 3:28:45 PM pack1.ArmStrongNumber_Using_Stream RdInput
INFO: The given Input number is null
The number null is Not ArmStrong Number
The link to the JUnit5 test for this solution is here – https://atomic-temporary-185308886.wpcomstaging.com/2023/04/04/junittest-armstrong-number/

Leave a comment