This is the complete solution to find the nth Fibonacci number in the series. This solution is designed to solve the problem using an interface, super class and three derived classes implementing the interface.
package pack1;
public interface NthFibonacciIntf {
/* Define the functional interface with SAM 'RdInput' */
public Integer RdInput(Integer intVal);
/* Define the static method 'displayMsg' */
static void displayMsg(Integer inpVal, Integer nthFib) {
if (nthFib == 0)
System.out.println("Nth Fibonacci of " + inpVal + " not exists");
else
System.out.println("Nth Fibonacci of " + inpVal + " is " + nthFib);
System.out.println();
}
}
package pack1;
import java.util.List;
import java.util.Optional;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class ReadInputNthNum {
private Integer inputIntvalue;
/* Declare the logger object for the class to display the log messages */
static Logger logger = Logger.getLogger(ReadInputNthNum.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 BigInteger variable */
public Integer getInputIntvalue() {
return inputIntvalue;
}
public void setInputIntvalue(Integer inputIntvalue) {
this.inputIntvalue = inputIntvalue;
}
}
class NthFibonacci_Using_CoreLogic extends ReadInputNthNum implements NthFibonacciIntf {
public Integer 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 + "'");
/* Define a BigInteger variable and initialize factValue value to 1 */
int nthFib = 0;
if (validateInputs(inpVal))
if (getInputIntvalue() >= 0) {
nthFib = NthFib(getInputIntvalue());
} else
logger.info("The given Input number is null");
NthFibonacciIntf.displayMsg(inpVal, nthFib);
return nthFib;
}
private static int NthFib(int nthNum) {
int[] fib = new int[nthNum];
if (nthNum == 0 || nthNum == 1)
return nthNum;
else {
// Initialize the first two values in the Fibonacci series
fib[0] = 0;
fib[1] = 1;
// Evaluate each term in the series until the nthNum
for (int i = 2; i < nthNum; i++)
fib[i] = fib[i - 1] + fib[i - 2];
return fib[nthNum - 1];
}
}
}
class NthFibonacci_Using_Recursion extends ReadInputNthNum implements NthFibonacciIntf {
public Integer 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 + "'");
/* Define a BigInteger variable and initialize factValue value to 1 */
int nthFib = 0;
if (validateInputs(inpVal))
if (getInputIntvalue() > 0) {
nthFib = NthFibRecursion(getInputIntvalue() - 1);
} else
logger.info("The given Input number is null");
NthFibonacciIntf.displayMsg(inpVal, nthFib);
return nthFib;
}
private static int NthFibRecursion(int nthNum) {
// Exit criteria of this recursive function when the parameter value reaches 0
// or 1.
if (nthNum == 0 || nthNum == 1)
return nthNum;
else
/*
* Recursive calls to NthFib() with n-1 and n-2 as a parameter values. The
* following recursive calls will update the parameter values as per the
* Fibonacci series logic
*/
return (NthFibRecursion(nthNum - 1) + NthFibRecursion(nthNum - 2));
}
}
class NthFibonacci_Using_StreamAPI extends ReadInputNthNum implements NthFibonacciIntf {
public Integer 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 + "'");
/* Define an int variable and initialize nthFib value to 0 */
int nthFib = 0;
if (validateInputs(inpVal)) {
if (getInputIntvalue() == 0 || getInputIntvalue() == 1)
return getInputIntvalue();
if (getInputIntvalue() >= 0) {
List<Integer> fibSeries = Stream.iterate(new int[] { 0, 1 }, t -> new int[] { t[1], t[0] + t[1] })
.limit(getInputIntvalue()).map(k -> k[0]).collect(Collectors.toList());
nthFib = fibSeries.get(getInputIntvalue() - 1);
}
} else
logger.info("The given Input number is null");
NthFibonacciIntf.displayMsg(inpVal, nthFib);
return nthFib;
}
}
public class NthFibonacciJ8 {
public static void main(String args[]) {
/*
* create the class object NthFibonacci_Using_CoreLogic, and call the method
* RdInput
*/
NthFibonacci_Using_CoreLogic NFUCL = new NthFibonacci_Using_CoreLogic();
NFUCL.RdInput(-16);
NFUCL.RdInput(null);
/*
* create the class object NthFibonacci_Using_CoreLogic, and call the method
* RdInput
*/
NthFibonacci_Using_Recursion NFUR = new NthFibonacci_Using_Recursion();
NFUR.RdInput(10);
NFUR.RdInput(null);
/*
* create the class object NthFibonacci_Using_CoreLogic, and call the method
* RdInput
*/
NthFibonacci_Using_StreamAPI NFUSA = new NthFibonacci_Using_StreamAPI();
NFUSA.RdInput(10);
NFUSA.RdInput(null);
}
}
OUTPUT:
Apr 16, 2023 2:27:51 PM pack1.NthFibonacci_Using_CoreLogic RdInput
INFO: This method 'RdInput' called from the Class 'pack1.NthFibonacciJ8'
Apr 16, 2023 2:27:51 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_CoreLogic'
The given number is -16
Apr 16, 2023 2:27:51 PM pack1.ReadInputNthNum validateInputs
INFO: The provided Input validated Successfully
Apr 16, 2023 2:27:51 PM pack1.NthFibonacci_Using_CoreLogic RdInput
INFO: The given Input number is null
Nth Fibonacci of -16 not exists
Apr 16, 2023 2:27:51 PM pack1.NthFibonacci_Using_CoreLogic RdInput
INFO: This method 'RdInput' called from the Class 'pack1.NthFibonacciJ8'
Apr 16, 2023 2:27:51 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_CoreLogic'
Nth Fibonacci of null not exists
Apr 16, 2023 2:27:51 PM pack1.NthFibonacci_Using_Recursion RdInput
INFO: This method 'RdInput' called from the Class 'pack1.NthFibonacciJ8'
Apr 16, 2023 2:27:51 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_Recursion'
The given number is 10
Apr 16, 2023 2:27:51 PM pack1.ReadInputNthNum validateInputs
INFO: The provided Input validated Successfully
Nth Fibonacci of 10 is 34
Apr 16, 2023 2:27:51 PM pack1.NthFibonacci_Using_Recursion RdInput
INFO: This method 'RdInput' called from the Class 'pack1.NthFibonacciJ8'
Apr 16, 2023 2:27:51 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_Recursion'
Nth Fibonacci of null not exists
Apr 16, 2023 2:27:51 PM pack1.NthFibonacci_Using_StreamAPI RdInput
INFO: This method 'RdInput' called from the Class 'pack1.NthFibonacciJ8'
Apr 16, 2023 2:27:51 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_StreamAPI'
The given number is 10
Apr 16, 2023 2:27:51 PM pack1.ReadInputNthNum validateInputs
INFO: The provided Input validated Successfully
Nth Fibonacci of 10 is 34
Apr 16, 2023 2:27:51 PM pack1.NthFibonacci_Using_StreamAPI RdInput
INFO: This method 'RdInput' called from the Class 'pack1.NthFibonacciJ8'
Apr 16, 2023 2:27:51 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_StreamAPI'
Apr 16, 2023 2:27:51 PM pack1.NthFibonacci_Using_StreamAPI RdInput
INFO: The given Input number is null
Nth Fibonacci of null not exists
The link to the JUnit5 test for this solution is here – https://atomic-temporary-185308886.wpcomstaging.com/2023/04/17/junit-test-nth-fibonacci/

Leave a comment