CSCE145 F2026 06 Classes and Objects Part 04
Watch on YouTube →
Overview
A Java taco-tracking example applies the waterfall model—requirements, design, implementation, testing, and maintenance—to show how classes and objects can solve a concrete problem. The design separates a Taco data class, a TacoManager that protects and maintains a fixed-size array, and a static front end; implementation covers UML, constructors, encapsulation, validation, object-array references, and an efficient add operation.
Key takeaways
- The waterfall workflow provides a concrete sequence for the taco tracker: define required fields and operations, design classes, implement them, test them, and maintain the software.
- Separating Taco data, TacoManager collection behavior, and the static user interface gives each class a focused responsibility.
- Private Taco fields and validated mutators prevent null names or locations and negative prices; constructors reuse those mutators rather than duplicating checks.
- TacoManager intentionally does not accept or expose an external Taco array, so callers cannot bypass its add and remove operations to alter internal storage.
- A Taco[] contains object references and begins with null elements; keeping occupied entries contiguous makes the first null a reliable insertion point and a non-null final slot a simple full-array test.
- The add operation rejects null input, checks capacity, fills the first null slot, and breaks; price sorting is planned but not implemented in this segment.
Chapters
- The example’s goal is to keep track of tacos by name, location, and price.
- The required features are adding tacos, removing one by name, sorting by price, and displaying taco information.
- The waterfall model organizes work into requirements, design, implementation, verification through testing, and maintenance.
- The design separates the user-facing front end from the back end that provides taco-management functionality.
- The Taco UML class has private String name, String location, and double price properties, plus toString and equals methods.
- TacoManager holds a Taco array and a public constant for its default size; its planned methods include add, remove, print, and private sort operations.
- A UML association indicates that TacoManager has zero or more Taco instances; the static front-end class uses a TacoManager and Scanner.
- The default Taco constructor sets name and location to "none" and price to 0; a parameterized constructor accepts all three values.
- Accessors return each private field, while mutators reject null strings and replace them with "none".
- The setPrice mutator accepts values greater than or equal to 0 and falls back to 0 for negative prices.
- The parameterized constructor calls the mutators to reuse their validation; toString formats the fields, and equals compares names and locations with String.equals and prices with ==.
- TacoManager stores its collection in a private Taco[] and defines public static final int DEFAULT_SIZE = 10.
- Its default constructor initializes the array to 10 slots; its parameterized constructor accepts an integer size rather than an external Taco array.
- The init method creates an array of the requested size when size is at least 1, otherwise it uses DEFAULT_SIZE.
- No getter or setter exposes the array: callers must use manager operations such as addTaco and removeTaco to preserve control over the collection.
- A Java Taco[] stores references to Taco objects, and every element starts as null until assigned an object reference.
- The manager keeps occupied slots together at the start of the array, with no null gaps; the first null marks the next insertion point.
- For example, adding references at indices 0, 1, and 2 leaves index 3 and every later slot null.
- This layout makes it possible to check whether the array is full by examining its last valid index.
- addTaco(Taco at) immediately returns if the supplied Taco reference is null.
- If tacos[tacos.length - 1] is not null, the array is full, so the method returns without inserting.
- Otherwise, a for loop scans from index 0 for the first null slot, stores the Taco reference there, and breaks to avoid inserting it repeatedly.
- Sorting by price is identified as a follow-up step after insertion; removal, sorting, printing, and the front-end implementation are deferred to the next lecture.
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.