Skip to content

nikkan1/ShapeAnalyzer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Shape Bounding Box Analyzer

Project Description

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.

Features

  • Unified Hierarchy: All shapes are derived from a single abstract Shape superclass 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.

Bounding Box Calculations

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)

Project Structure

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.

Input Format

The application reads from a text file with the following structure:

  1. First Line: An integer representing the total number of shapes in the file.
  2. 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

Validation and Error Handling

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".

About

A Java application that identifies the shape with the largest bounding box area from a collection of geometric shapes.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages