CSCE145 F2026 06 Classes and Objects Part 05
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
- 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.
- `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.
- `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.
- `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.
- 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.
Transcript excerpt
0:00 Hey everyone, how it goes and welcome back to computer science 145. Today we're going to continue discussing classes and objects. We're going to continue the big example that we established in the previous lecture. Here was our problem. I like to travel around and while I like to travel, I like to enjoy one of the world's most perfect food, a taco. Now, the problem that we're solving is we need to keep track of the tacos that I like. We are engineering solutions to these problems by using software. And it was introduced in the previous lecture that we're using what's called the waterfall model in order to create this software. The waterfall model consists of a couple of parts. Gather the requirements, design the software, implement the
0:49 software, verify it by testing it, and also maintaining it. Now going through a few of these parts, the requirements was the software needs to keep track of important taco information such as its name, location, and price. The software should be able to add tacos, remove tacos, sort them by price, and display all the taco information. The software should also have a clear and simple front end. From there, we started designing the software by using a UML class diagram. Each one of these boxes represents a different class and each one of these has different sections. The top one is the class identifier. The next one are the properties like the instance variables and the final one is the methods that each one of these classes can do.
1:38 In the last example, we implemented the taco class and part of the taco manager. Today we're going to finish implementation. And before we get to that, let's go over a couple of very important elements that was discussed in that previous lecture.
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
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.