Lab 03
Grocery List
Objective:
Implement a system that keeps tracks of groceries in a
singly linked-list.
Lab Solution
Requirements:
- Functionality. (80pts)
- No Syntax, Major Run-Time, or Major Logic Errors.
(80pts*)
- *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
run-time or logic errors is nonfunctional code and will receive no
points for this entire section.
- Set up the Project. (10pts)
- 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.
- Grocery Item Class (10pts)
- 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 0.
- 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: <<Name>> Value: <<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.
- Grocery List Class (60pts total)
- Write another class and name it exactly GroceryList.
- Create an Internal class ListNode (5pts)
- Instance Variables
- Data of type GroceryItem, where its default value
null.
- Link of type ListNode, where its default value is
null.
- Constructors
- Default: sets the instances variables to null.
- 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)
(5pts)
- 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) (5pts)
- 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 doesn’t move forward if that node is null (5pts)
- getCurrent: returns the data at the current node as
long as the current isn’t null. (5pts)
- 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. (5pts)
- 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. (5pts)
- 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. (5pts)
- removeCurrent: removes the current node from the list
by resetting the links. (5pts)
- showList: prints out the contents of the list
line-by-line. (5pts)
- contains: returns a true or false value based on whether
or not the data passed in via a parameter is in the list. (5pts)
- totalCost: returns the sum of all of the groceries.
(5pts)
- Coding Style. (10pts)
- Code functionality organized within multiple methods
other than the main method, and methods organized within multiple classes
where appropriate. (5pts)
- Readable Code (5pts)
- Meaningful identifiers for data and methods.
- Proper indentation that clearly identifies statements
within the body of a class, a method, a branching statement, a loop
statement, etc.
- All the above must apply for full credit.
- Comments. (10pts)
- Your name in the file. (5pts)
- At least 5 meaningful comments in addition to your name.
These must describe the function of the code it is near. (5pts)
Example Dialog:
Grocery List Tester.
-----------------------------------------------
Test 1
-----------------------------------------------
Reading the grocery list.
Grocery Item Name: Apples Value: 4.42
Grocery Item Name: Bananas Value: 1.51
Grocery Item Name: Strawberries Value: 2.51
Grocery Item Name: Bell Peppers Value: 0.98
Grocery Item Name: Carrots Value: 3.52
Grocery Item Name: Broccoli Value: 2.04
Grocery Item Name: Garlic Value: 6.12
Grocery Item Name: Lemons/Limes Value: 0.32
Grocery Item Name: Onion Value: 5.74
Grocery Item Name: Parsley Value: 2.16
Grocery Item Name: Cilantro Value: 3.11
Grocery Item Name: Basil Value: 6.98
Grocery Item Name: Potatoes Value: 5.32
Grocery Item Name: Spinach Value: 4.11
Grocery Item Name: Tomatoes Value: 7.38
Grocery Item Name: Butter Value: 5.51
Grocery Item Name: Sliced Cheese Value: 1.26
Grocery Item Name: Shredded Cheese Value: 3.63
Grocery Item Name: Milk Value: 4.67
Grocery Item Name: Sour Cream Value: 0.74
Grocery Item Name: Greek Yogurt Value: 7.21
Grocery Item Name: Breadcrumbs Value: 4.93
Grocery Item Name: Pasta Value: 1.49
Grocery Item Name: Quinoa Value: 4.78
Grocery Item Name: Rice Value: 6.96
Grocery Item Name: Sandwich Bread Value: 1.22
Grocery Item Name: Tortillas Value: 4.17
-----------------------------------------------
Test 2
-----------------------------------------------
Finding the total cost.
Total Cost: 102.78999999999998
-----------------------------------------------
Test 3
-----------------------------------------------
Checking Contains with item known to be in the list
Grocery Item Name: Potatoes Value: 5.32
Contains? true
Checking Contains with item known to NOT be in the list
Grocery Item Name: none Value: 0.0
Contains? false
-----------------------------------------------
Test 4
-----------------------------------------------
Accessing First Item in the list
Grocery Item Name: Apples Value: 4.42
Removing First Item in the list
Grocery Item Name: Bananas Value: 1.51
Grocery Item Name: Strawberries Value: 2.51
Grocery Item Name: Bell Peppers Value: 0.98
Grocery Item Name: Carrots Value: 3.52
Grocery Item Name: Broccoli Value: 2.04
Grocery Item Name: Garlic Value: 6.12
Grocery Item Name: Lemons/Limes Value: 0.32
Grocery Item Name: Onion Value: 5.74
Grocery Item Name: Parsley Value: 2.16
Grocery Item Name: Cilantro Value: 3.11
Grocery Item Name: Basil Value: 6.98
Grocery Item Name: Potatoes Value: 5.32
Grocery Item Name: Spinach Value: 4.11
Grocery Item Name: Tomatoes Value: 7.38
Grocery Item Name: Butter Value: 5.51
Grocery Item Name: Sliced Cheese Value: 1.26
Grocery Item Name: Shredded Cheese Value: 3.63
Grocery Item Name: Milk Value: 4.67
Grocery Item Name: Sour Cream Value: 0.74
Grocery Item Name: Greek Yogurt Value: 7.21
Grocery Item Name: Breadcrumbs Value: 4.93
Grocery Item Name: Pasta Value: 1.49
Grocery Item Name: Quinoa Value: 4.78
Grocery Item Name: Rice Value: 6.96
Grocery Item Name: Sandwich Bread Value: 1.22
Grocery Item Name: Tortillas Value: 4.17
Accessing Fourth Item in the list
Grocery Item Name: Carrots Value: 3.52
Removing Fourth Item in the list
Grocery Item Name: Bananas Value: 1.51
Grocery Item Name: Strawberries Value: 2.51
Grocery Item Name: Bell Peppers Value: 0.98
Grocery Item Name: Broccoli Value: 2.04
Grocery Item Name: Garlic Value: 6.12
Grocery Item Name: Lemons/Limes Value: 0.32
Grocery Item Name: Onion Value: 5.74
Grocery Item Name: Parsley Value: 2.16
Grocery Item Name: Cilantro Value: 3.11
Grocery Item Name: Basil Value: 6.98
Grocery Item Name: Potatoes Value: 5.32
Grocery Item Name: Spinach Value: 4.11
Grocery Item Name: Tomatoes Value: 7.38
Grocery Item Name: Butter Value: 5.51
Grocery Item Name: Sliced Cheese Value: 1.26
Grocery Item Name: Shredded Cheese Value: 3.63
Grocery Item Name: Milk Value: 4.67
Grocery Item Name: Sour Cream Value: 0.74
Grocery Item Name: Greek Yogurt Value: 7.21
Grocery Item Name: Breadcrumbs Value: 4.93
Grocery Item Name: Pasta Value: 1.49
Grocery Item Name: Quinoa Value: 4.78
Grocery Item Name: Rice Value: 6.96
Grocery Item Name: Sandwich Bread Value: 1.22
Grocery Item Name: Tortillas Value: 4.17
Solutions Tests:
- Is your name written as a comment in all source files?
- Does the solution compile (No Syntax Errors)?
- Does your output match the example dialog’s output?
Lab Report Questions:
- Create a section named “Problem” and describe this lab’s
problem in your own words. (10pts).
- Create a section named “Solution Description” and describe
how the code solves the problem in your own words. (10pts).
- Create a section named “Problems Encountered” and describe
the various syntax, run-time, and logic errors that were encountered while
implementing the solution. (10pts)
- Name some advantages of using a linked list instead of an
array. (10pts)
- Name some disadvantages of using a linked list instead of
an array. (10pts)
For Questions 06-10 you may assume
the following Integer linked list code is provided.
- Using the provided code and assuming the values 1, 2, 3,
4, 5 are currently in the linked list, the following code snippet’s
purpose is print out all even values. However, it never prints anything. Why
is this and how can the code be modified to fix this?
- Using the provided code and assuming the values 1, 2, 3,
4, 5 are currently in the linked list, the following code snippet’s
purpose is to add a value before the head and become the new first value,
while keeping all the other values intact. However, it adds the new value,
but removes every other value that was previously in the list. Why is this
and how can the code be modified to fix this?
- Using the provided code and assuming the values 1, 2, 3,
4, 5 are currently in the linked list, the following code snippet’s
purpose is to remove the first value, but it does not appear to remove
anything. Why is this and how can the code be modified to fix this?
- Using the provided code and assuming the values 1, 2, 3,
4, 5 are currently in the linked list, the following code snippet’s
purpose is to remove the last value, but it results in a
“NullPointerException”. Why is this and how can the code be modified to
fix this?
- Using the provided code and assuming the values 1, 2, 3,
4, 5 are currently in the linked list, the following code snippet’s
purpose is to find the product by multiplying all the values together.
However, instead of getting 120 the method returns 24. Why is this and how
can the code be modified to fix this?
Finally
Upload the Lab Solution’s source code (.JAVA file(s)) and
the Lab Report’s text file to the CSCE Dropbox.