Answered step by step
Verified Expert Solution
Question
1 Approved Answer
IN JAVA Lab 0 3 Grocery List Objective: Implement a system that keeps tracks of groceries in a singly linked - list. Lab Solution Requirements:
IN JAVA
Lab
Grocery List
Objective:
Implement a system that keeps tracks of groceries in a singly linkedlist.
Lab Solution
Requirements:
Functionality.
o No Syntax, Major RunTime, or Major Logic Errors.
Code that cannot be compiled due to syntax errors is nonfunctional code and will receive no points for this entire section.
Code that cannot be executed or tested due to major runtime or logic errors is nonfunctional code and will receive no points for this entire section.
o Set up the Project.
First download the driver file and include it in your project and the GroceryList text file in your root project folder.
Do not modify the driver or the text file.
o Grocery Item Class
Create a class and name it exactly GroceryItem
Instance Variables
Name: a string value that contains the name of the grocery item. Its default value is none and must not be null.
Value: a decimal value that corresponds to the price of the item. Its default value is
Constructors
Default: Must set all properties to their default values mentioned in the Instance Variables section.
Parameterized: Must take in a parameter for each instance variable in the order named above. This means the first instance variable is the first parameter, the second instance variable is the second parameter, and so on This must set the instance variable values only if the given values are valid, but otherwise it must set the instance variables to their default values.
Methods
Create accessors and mutators for each attribute. Mutators must check for valid values.
toString: a method that returns a type String formatted as:
Grocery Item Name: Value:
Where values denoted in are assumed to be the instance variable values.
equals: This method takes in another instance of GroceryItem and only returns true if all of the instance variables match.
All must apply for full credit.
o Grocery List Class
Write another class and name it exactly GroceryList.
Create an Internal class ListNode pts
Instance Variables
o Data of type GroceryItem, where its default value null.
o Link of type ListNode, where its default value is null.
Constructors
o Default: sets the instances variables to null.
o Parameterized: sets the instance variables to their respective parameters, and does not need to check for valid values.
All must apply for full credit.
Instance Variables for GroceryList not ListNode
Head: a ListNode which always points to the beginning of the linked list
Current: a ListNode which moves to point at different items in the list
Previous: a ListNode which points to the item behind current.
Constructor for GroceryList not ListNode
A default constructor that initializes head to an empty ListNode and sets current and previous to point at the head.
Methods for GroceryList not ListNode
gotoNext: This moves the current node forward in the list by one node. It doesnt move forward if that node is null
getCurrent: returns the data at the current node as long as the current isnt null.
setCurrent: takes in a parameter of type GroceryItem and sets the data at the current node to that value as long as current is not null and the data provided is also not null.
addItem: This method given a grocery item, creates a new list node with the data assigned to the provided grocery item and then adds the new list node to the end of the list. If the list is empty, then it starts the list. Also, if the grocery item is null then the method should do nothing.
addItemAfterCurrent: creates a new node based on data that is passed in by a parameter and puts that node after the current position. If the list is empty or the data provided is null, then this method should do nothing
removeCurrent: removes the current node from the list by resetting the links. pts
showList: prints out the contents of the list linebyline. pts
contains: returns a true or false value based on whether or not the data passed in via a parameter is in the list. pts
totalCost: returns the sum of all of the groceries. pts
Example Dialog:
Grocery List Tester.
Test
Reading the grocery list.
Grocery Item Name: Apples Value:
Grocery Item Name: Bananas Value:
Grocery Item Name: Strawberries Value:
Grocery Item Name: Bell Peppers Value:
Grocery Item Name: Carrots Value:
Grocery Item Name: Broccoli Value:
Grocery Item Name: Garlic Value:
Grocery Item Name: LemonsLimes Value:
Grocery Item Name: Onion Value:
Grocery Item Name: Parsley Value:
Grocery Item Name: Cilantro Value:
Grocery Item Name: Basil Value:
Grocery Item Name: Potatoes Value:
Grocery Item NameImplement a system that keeps tracks of groceries in a singly linkedlist.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started