135k views
3 votes
Item Node.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);
}
}
Shopping list.java
import java.util.Scanner;
public class ShoppingList {
public static void main (String[] args) {
Scanner scnr = new Scanner(System.in);
ItemNode headNode; // Create intNode objects
ItemNode currNode;
ItemNode lastNode;
String item;
int i;
// Front of nodes list
headNode = new ItemNode();
lastNode = headNode;
int input = scnr.nextInt();
for(i = 0; i < input; i++ ){
item = scnr.next();
currNode = new ItemNode(item);
lastNode.insertAtEnd(headNode, currNode);
lastNode = currNode;
}
// Print linked list
currNode = headNode.getNext();
while (currNode != null) {
currNode.printNodeData();
currNode = currNode.getNext();
}
}
}
The Fibonacci sequence begins with O and then 1 follows. All subsequent values are the sum of the previous two, for example: 0,1,1,2,3,5, 8, 13. Complete the fibonacci() method, which takes in an index, n, and returns the nth value in the sequence. Any negative index values should return -1. Ex: If the input is: 7 the output is: fibonacci (7) is 13 Note: Use recursion and DO NOT use any loops.

User Harminder
by
8.8k points

1 Answer

2 votes

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;

}

}

User Gldanoob
by
8.0k points