This Java application manages a collection of regular geometric shapes, including circles, regular triangles, squares, and regular hexagons. The program processes these shapes to determine which one has the largest bounding box area.
A bounding box is the smallest possible rectangle that completely encloses a shape, with its sides parallel to the x and y axes. Each shape is represented by its center coordinates and its side length or radius.
- Unified Hierarchy: All shapes are derived from a single abstract
Shapesuperclass for uniform management. - File-Based Input: The program loads data from text files, where the first line indicates the total number of shapes.
- Polymorphic Calculations: Each specific shape class implements its own logic for calculating its unique bounding box area.
- Robust Validation: Includes comprehensive error handling for incorrect line formats, negative dimensions, and unknown shape types.
The area of the bounding box is calculated based on the shape's specific geometry:
- Circle: Area = (2 * radius)²
- Square: Area = side²
- Regular Triangle: Area = side * (sqrt(3) / 2 * side)
- Regular Hexagon: Area = (2 * side) * (sqrt(3) * side)
The project consists of the following key files:
- Shape.java: The abstract base class defining common properties like coordinates and dimensions.
- ShapeAnalyzer.java: The main class that handles file reading, data parsing, error management, and identification of the largest shape.
- Circle.java, Square.java, RegularTriangle.java, RegularHexagon.java: Specific implementations for each shape type.
The application reads from a text file with the following structure:
- First Line: An integer representing the total number of shapes in the file.
- Subsequent Lines: A character identifier (C, T, S, or H) followed by the X coordinate, Y coordinate, and the side length or radius.
Example Input: 4 C 0 0 3 T 1 1 5 S -2 2 4 H 0 3 6
The application is tested against various edge cases to ensure stability:
- Missing Files: Displays "File not found" if the input file is not accessible.
- Invalid Formats: Detects and reports lines that do not contain exactly four arguments.
- Data Integrity: Rejects shapes with negative coordinates or side lengths/radiuses that are less than or equal to zero.
- Parsing Errors: Safely handles cases where non-numeric text is provided instead of coordinates or dimensions.
- Unknown Types: Flags any shape identifier that is not C, S, T, or H as an "Unknown shape".