CIS 1068 (mini) Assignment 10
Perishable Grocery Item

Posted: Saturday, April 11

Due: Monday, April 20

20 points

This assignment builds directly on your work from Assignment 9. You'll extend the GroceryItem class by writing a subclass.

Background

Not everything on a grocery list has an expiration date. A bottle of dish soap doesn't expire. But milk, yogurt, and fresh produce all do.

We'd like to be able to represent items that have expiration dates alongside ordinary grocery items, treating them all as part of the same shopping list. Inheritance gives us a clean way to do this.

PerishableItem (8 points)

Implement a class PerishableItem that extends GroceryItem.

A PerishableItem has all the same fields as a GroceryItem — name, quantity, and estimated price — plus one additional private field:

Your class should provide the following public methods:

Driver 1 (6 points)

Write a short driver that exercises PerishableItem directly:

PerishableItem yogurt = new PerishableItem("Yogurt", 3, 1.19, "2026-04-12");
System.out.println(yogurt);
System.out.println("Expired: " + yogurt.isExpired("2026-04-08"));

Driver 2 (6 points)

Write a second driver that demonstrates something important about inheritance. Create a GroceryList and add a mix of plain GroceryItem objects and PerishableItem objects to it:

GroceryList groceries = new GroceryList();
groceries.addItem(new GroceryItem("Dish Soap", 1, 3.99));
groceries.addItem(new PerishableItem("Milk", 1, 4.29, "2026-04-15"));
groceries.addItem(new GroceryItem("Paper Towels", 2, 5.49));
groceries.addItem(new PerishableItem("Yogurt", 3, 1.19, "2026-04-12"));
System.out.println(groceries);

Bring your GroceryList from Assignment 9 into this project — you do not need to make any changes to it. GroceryList stores an array of GroceryItem objects, and since every PerishableItem is a GroceryItem, perishable items can be added to the list without GroceryList knowing anything about them.

When the list is printed, notice that the expiration date appears for perishable items and not for plain ones. GroceryList calls toString() on each element the same way, but Java is smart enough to call the right version of toString() depending on what kind of object it actually is. This is called polymorphism, and it's one of the most powerful ideas in object-oriented programming.

Implementation Notes

what to submit

Upload PerishableItem.java, Driver1.java, and Driver2.java to Canvas. Include your GroceryItem.java and GroceryList.java from Assignment 9 as well — we need them to compile your code.

It's a good idea to confirm through the Canvas submission page that what you've intended to submit was uploaded. We will grade what you submit. If you submit a corrupted, empty, or otherwise incorrect file, this is what we'll grade. It is your responsibility to verify through the Canvas submission page that you've submitted the correct files and that they were uploaded properly.

If you're unsure about how to submit, please consult Canvas' tutorial or see us for help.