To complete the fibonacci() method, one need to use the recursive logic to calculate the Fibonacci sequence, so the code for the ItemNode class will be:
Java
public
class
ItemNode
{
private String item;
private ItemNode nextNodeRef; // Reference to the next node
public
ItemNode()
{
item = "";
nextNodeRef = null;
}
// Constructor
public
ItemNode(String itemInit)
{
this.item = itemInit;
this.nextNodeRef = null;
}
// Constructor
public
ItemNode(String itemInit, ItemNode nextLoc)
{
this.item = itemInit;
this.nextNodeRef = nextLoc;
}
// Insert node after this node.
public
void
insertAfter(ItemNode nodeLoc)
{
ItemNode tmpNext;
tmpNext = this.nextNodeRef;
this.nextNodeRef = nodeLoc;
nodeLoc.nextNodeRef = tmpNext;
}
// TODO: Define insertAtEnd() method that inserts a node
// to the end of the linked list
// Get location pointed by nextNodeRef
public ItemNode getNext()
{
return
this.nextNodeRef;
}
public
void
printNodeData()
{
System.out.println(this.item);
}
// Recursive fibonacci method
public static int fibonacci(int n) {
// Check for negative indices
if (n < 0) {
return -1;
}
// Base cases
if (n == 0 || n == 1) {
return n;
}
// Recursive calls to calculate the next two Fibonacci numbers
int n1 = fibonacci(n - 1);
int n2 = fibonacci(n - 2);
// Return the sum of the two Fibonacci numbers
return n1 + n2;
}
}