To install StudyMoose App tap and then “Add to Home Screen”
Save to my list
Remove from my list
The objective of this lab assignment is to modify the provided Java code to implement an "insertAtEnd" method in the "ItemNode" class. This method will add elements to the end of a linked list, and user input will be used to determine the number of items and their names to be added to the list. The assignment also requires proper prompting of the user for input.
In this assignment, we will be working with two Java classes: "Shoppinglist" and "ItemNode".
The main steps involved in completing this assignment are as follows:
Below is the modified code for the "Shoppinglist" class:
import java.util.Scanner;
public class Shoppinglist {
public static void main(String[] args) {
Scanner scr = new Scanner(System.in);
// Prompt the user to enter the number of items
System.out.print("Enter the number of items to be inserted: ");
int input = scr.nextInt();
ItemNode headNode; // Create ItemNode objects
ItemNode currNode;
ItemNode lastNode;
String item;
int i;
// Front of nodes list
headNode = new ItemNode();
lastNode = headNode;
for (i = 0; i < input; i++) {
System.out.print("Enter item " + (i + 1) + ": ");
item = scr.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();
}
}
}
Additionally, the "insertAtEnd" method has been added to the "ItemNode" class as follows:
// Insert node at the end of the linked list
public void insertAtEnd(ItemNode head, ItemNode node) {
if (head == null) {
headNode = node;
} else {
ItemNode currNode = head;
while (currNode.getNext() != null) {
currNode = currNode.getNext();
}
currNode.nextNodeRef = node;
}
}
Upon running the modified code, the program will prompt the user to enter the number of items they want to add to the linked list. Then, it will collect the names of the items and insert them at the end of the list. Finally, it will print the contents of the linked list.
This lab assignment demonstrates how to modify Java code to implement an "insertAtEnd" method for adding elements to the end of a linked list. By prompting the user for input, the program allows for dynamic creation of the linked list based on user preferences. This assignment enhances the understanding of data structures and their manipulation in Java.
Below are the Java classes used in this assignment:
import java.util.Scanner;
public class Shoppinglist {
public static void main(String[] args) {
// ... (Refer to Methodology section for the complete code)
}
}
public class ItemNode {
private String item;
private ItemNode nextNodeRef; // Reference to the next node
// Constructors...
// Insert node at the end of the linked list
public void insertAtEnd(ItemNode head, ItemNode node) {
// Implementation...
}
// Get location pointed by nextNodeRef...
public void printNodeData() {
System.out.println(this.item);
}
}
Lab Assignment Report - Shopping List. (2023, Aug 04). Retrieved from https://studymoose.com/document/comp-182-lab-assignment-5-shopping-list
👋 Hi! I’m your smart assistant Amy!
Don’t know where to start? Type your requirements and I’ll connect you to an academic expert within 3 minutes.
get help with your assignment