Lab 03

Grocery List

 

Objective:

 

Implement a system that keeps tracks of groceries in a singly linked-list.

 

Lab Solution

 

Requirements:

 

 

Grocery Item Name: <<Name>> Value: <<Value>>

 

Where values denoted in “<<>>” are assumed to be the instance variable values.

 

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:

  1. Is your name written as a comment in all source files?
  2. Does the solution compile (No Syntax Errors)?
  3. Does your output match the example dialog’s output?

 

Lab Report Questions:

  1. Create a section named “Problem” and describe this lab’s problem in your own words. (10pts).
  2. Create a section named “Solution Description” and describe how the code solves the problem in your own words. (10pts).
  3. Create a section named “Problems Encountered” and describe the various syntax, run-time, and logic errors that were encountered while implementing the solution. (10pts)
  4. Name some advantages of using a linked list instead of an array. (10pts)
  5. 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.

  1. 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?

  1. 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?

 

  1. 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?

  1. 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?

  1. 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.