Lab Assignment Report - Shopping List

Categories: Technology

Introduction

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.

Methodology

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:

  1. Prompt the user to enter the number of items to be inserted.
  2. Iterate through the user's input, collecting the names of items to be added to the linked list.

    Get to Know The Price Estimate For Your Paper
    Topic
    Number of pages
    Email Invalid email

    By clicking “Check Writers’ Offers”, you agree to our terms of service and privacy policy. We’ll occasionally send you promo and account related email

    "You must agree to out terms of services and privacy policy"
    Write my paper

    You won’t be charged yet!

    Get quality help now
    Dr. Karlyna PhD
    Dr. Karlyna PhD
    checked Verified writer

    Proficient in: Technology

    star star star star 4.7 (235)

    “ Amazing writer! I am really satisfied with her work. An excellent price as well. ”

    avatar avatar avatar
    +84 relevant experts are online
    Hire writer

  3. Create an instance of the "ItemNode" class for each item and insert it at the end of the linked list using the "insertAtEnd" method.
  4. Print the contents of the linked list.

Code Implementation

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;
}
}

Results

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.

Conclusion

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.

Appendix

Below are the Java classes used in this assignment:

ShoppingList.java


import java.util.Scanner;

public class Shoppinglist {
public static void main(String[] args) {
// ... (Refer to Methodology section for the complete code)
}
}

ItemNode.java


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);
}
}
Updated: Jan 03, 2024
Cite this page

Lab Assignment Report - Shopping List. (2023, Aug 04). Retrieved from https://studymoose.com/document/comp-182-lab-assignment-5-shopping-list

Lab Assignment Report - Shopping List essay
Live chat  with support 24/7

👋 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