CSCE145 F2026 06 Classes and Objects Part 05
Watch on YouTube →
Overview
A Java taco-tracking example applies the waterfall model and a UML class design to manage tacos by name, location, and price. The implementation covers reference arrays, safe removal by shifting elements, bubble-sorting by price, and a console front end using a `Scanner`, `switch`, and `for-each` loop.
Key takeaways
- An object array stores references rather than embedding objects; Java initializes each reference slot to `null`.
- Keeping all tacos before the first `null` makes insertion, printing, sorting, and removal easier because the populated portion is contiguous.
- Removing an array element requires shifting subsequent references left and clearing the final slot; merely assigning `null` in the middle breaks the contiguous-data convention.
- The taco manager sorts after each insertion with bubble sort, comparing neighboring objects through `getPrice()` and swapping their references.
- Java's for-each loop is convenient for reading every collection element, but the example reserves indexed loops for operations that alter array contents.
- A Java `switch` selects menu actions by integer choice, and `break` prevents execution from falling through into subsequent cases.
Chapters
0:00
Taco Manager Requirements and Object-Array Structure
- The taco manager must store each taco's name, location, and price, and support adding, removing, sorting, and displaying tacos.
- A UML class diagram separates class identifiers, instance variables, and methods; the example continues the `Taco` and `TacoManager` design.
- Java object arrays hold references to objects, and each array element defaults to `null`.
- The manager keeps populated entries together at the start of the array, so the first `null` marks the end of the stored tacos.
2:04
Remove Tacos by Name Without Breaking the Array Invariant
- `removeTaco` initializes `removeIndex` to `-1`, searches non-null entries, and compares names with `String.equals`.
- If no matching taco is found, the method returns immediately; otherwise it shifts later references left to keep populated entries contiguous.
- The shift loop must stop before the final index to avoid reading past the array; the last slot is then explicitly set to `null`.
- Once the array no longer refers to the removed object, Java's garbage collector can reclaim it if no other references remain.
11:26
Bubble-Sort Taco Objects by Price and Print with For-Each
- `sortTacos` is private because it is only called internally after `addTaco` inserts a new taco.
- Bubble sort compares adjacent tacos using `getPrice()` and swaps their references when the left taco costs more.
- A `swapped` flag repeats passes until no swaps occur; comparisons stop when either adjacent array entry is `null`.
- `printTacos` uses Java's for-each syntax to print each taco and stops at the first `null`; for-each is intended for traversal, not structural changes.
17:42
Build the Console Front End with Static State and a Switch Menu
- `TacoManagerFE` imports `java.util.Scanner` and defines static `keyboard` and `TacoManager` fields for use by static methods.
- A `while (!quit)` loop prints choices to add a taco, remove one, or quit; a `switch` handles choices `1`, `2`, and `9`.
- Each `case` uses `break` to prevent fall-through, while `default` reports invalid input; consuming `nextLine()` after `nextInt()` avoids scanner newline issues.
- `addTaco` reads name, location, and price, constructs a `Taco`, and passes it to the manager; `removeTaco` reads a name and calls the manager's removal method.
28:18
Test the Taco Manager and Trace References in Memory
- The console demonstration adds three tacos with prices of 1, 2, and 0.99, showing that insertion triggers ascending-price sorting.
- Removing the taco named `ASDF` leaves the other entries in the array, and choosing `9` ends the program after one final print.
- A four-slot array example shows `new Taco[4]` initially contains four `null` references and that added taco references persist in the array after method parameters go out of scope.
- During removal, shifting references and clearing the final slot eliminates the array's reference to the removed taco, allowing garbage collection when it is otherwise unreachable.
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, UofSC_CSCE145_CSCE146.