UNC COMP 301 - F26/Lec 2 - Maven Build; Motivating the Object Oriented Way
Watch on YouTube →
Overview
Muhammad Sayeed Ghani demonstrates how Maven turns Java source into a distributable JAR, including how the POM selects the default main class and how users can launch a different class. The lecture then begins a stepwise refactoring of a triangle-area program from static, procedural methods toward object-oriented design, introducing triangle validation, floating-point comparison, abstraction, object-owned state, constructors, and Java name resolution.
Key takeaways
- Maven's package goal builds a JAR under target, and the JAR's configured main class determines what `java -jar` launches when multiple Java classes contain `main` methods.
- Java floating-point values should not generally be compared with exact equality: checking whether the absolute difference is below a small tolerance avoids errors such as treating `0.1 + 0.2` as unequal to `0.3`.
- A valid triangle must satisfy the triangle inequality for all three side-length pairs; the procedural classifier shown does not enforce that condition before returning a category.
- The object-oriented shift changes the design from static functions operating on loose coordinate parameters to a triangle object that owns its state, behavior, and validation.
- In Java, constructor parameters shadow same-named fields; `this.field = field` is necessary to initialize the field when both identifiers are identical.
Chapters
- The lecture finishes the Java workflow by converting source files and resources into a format customers can execute.
- The next topic begins with a triangle-area application, first implemented procedurally with static methods before evolving toward object-oriented design.
- Lecture 01 and Lecture 02 code are available as downloadable folders that can be opened in IntelliJ IDEA.
- Maven's clean goal removes generated output, including files under target; compile produces Java class files.
- The test goal can run unit tests before delivery, while package creates the distributable JAR.
- Verify, site, and deploy support later checks, documentation generation, and publishing workflows.
- After cleaning, running Maven's package goal recreates target and places the JAR there.
- The command `java -jar` launches the packaged application using the Java runtime; customers do not need the JDK's `javac` compiler to run it.
- The JAR combines compiled classes into one archive, making it suitable for distribution.
- A JAR can refer to external dependencies or include them in a larger “fat JAR”; a lean JAR typically ships application classes and relies on external dependencies being available.
- Maven's tool window is available in IntelliJ IDEA for Maven projects through View > Tool Windows.
- Procedural programming can suit hardware-constrained systems with little tolerance for object-oriented overhead, while object-oriented design is useful for large programs.
- The triangle application begins with three two-dimensional vertices, represented by six floating-point coordinates, plus a triangle name.
- Input such as `T1` followed by coordinates `(0,0)`, `(3,3)`, and `(6,0)` identifies a triangle; entering `end` stops input.
- The intended application will classify triangles and report area statistics, including the smallest triangle and category averages.
- Version 01 uses a `TriangleAreaApp` class and `main` method because Java source still requires a class, even when the design is procedural.
- A `Scanner` reads each triangle ID and six `double` values into local variables; `end` terminates the input loop.
- The first version prints the name and the coordinates of the three vertices without yet modeling a triangle as an object.
- Version 02 adds a static point-distance function that computes distances between vertex pairs using the Euclidean distance formula.
- A static triangle-category method compares the three side lengths to classify a triangle as equilateral, isosceles, or scalene.
- The methods accept coordinate values as parameters and return results without creating triangle instances.
- The classification logic does not verify that its side lengths form a valid triangle; the triangle inequality requires each pair of side lengths to sum to more than the third.
- Direct equality comparisons between `double` side lengths can misclassify triangles because binary floating-point representation can make values such as `0.1 + 0.2` differ slightly from `0.3`.
- A practical equality test compares the absolute difference between values with a small tolerance rather than using exact `==`.
- The program can compute category averages by maintaining a cumulative area total and triangle count for each of the three categories.
- For the smallest-area triangle, it tracks the current minimum area and ID, initializes them from the first triangle, and replaces them when a smaller area appears.
- The accumulated totals and counts are divided at the end to produce average areas.
- Version 04 keeps input handling, reporting, and calculations in one application file, with static methods for triangle category, point distance, and area.
- Data lives in local variables or arrives as method parameters; the procedural implementation has no triangle objects, instance fields, or Java global variables.
- A parameter is a variable declared in a method signature, while an argument is the value supplied when calling that method.
- Object-oriented design applies abstraction by hiding implementation details and exposing the operations a triangle supports.
- A triangle can be treated as an abstract data type with a domain of valid triangles and operations such as computing area, perimeter, and category.
- Unlike the procedural version's loose collection of coordinates passed to functions, an abstract triangle represents a coherent concept whose validity can be checked.
- A triangle can be represented by six vertex coordinates or by three side lengths, showing that an abstraction can have multiple internal representations.
- An interface describes supported behavior, such as retrieving area, perimeter, or type, without specifying fields or implementation details.
- A class supplies a concrete implementation: an object owns its data and methods, can answer questions about itself, and can validate its state during construction.
- A constructor has the same name as its class and declares no return type; its parameters can initialize an instance's fields.
- Different parameter names allow direct assignments such as `this.ax = x1`; another convention prefixes field names with underscores to distinguish fields from parameters.
- When parameter and field names differ, the constructor can assign the parameter to the intended field without relying on naming conventions.
- When a parameter and field share a name, an unqualified name resolves to the parameter in local scope first, so `ax = ax` assigns the parameter to itself rather than initializing the field.
- This naming mistake compiles and runs without an error, but the object's fields retain their default values; use `this.ax = ax` or distinct names to assign correctly.
- IntelliJ IDEA's debugger shows method parameters separately from fields inside `this`; the triangle object is created on the heap only after the constructor call returns.
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Muhammad Sayeed Ghani.