This repository includes the implementation of various classes related to polygons, including the Polygon, Point, Line, Triangle, and Rectangle classes. Each class has specific attributes and methods to perform different operations related to polygons.
The Polygon class represents a polygon and has the following features:
-
The attribute
verticesis declared as aListofPointsto store the vertices of the polygon. -
The default constructor initializes the
verticesattribute with three points:(0,0),(1,1), and(1,0)in the respective order. -
The constructor with three
Pointparameters initializes theverticesattribute and adds the three parameters respectively to theverticeslist. -
The method
computePerimetercalculates and returns the sum of the lengths of each side of the polygon. It does this by creating a line per consecutive pair of vertices and computing its length using thecomputeLengthmethod of theLineclass. Note that the last vertex is paired with the first vertex to complete the polygon. -
The method
computeAreacalculates and returns the area of the polygon using a specific formula. It considers the number of vertices and the x-values of the first and last vertices. -
The method
addVertexadds a new vertex to theverticeslist. -
The method
removeVertexsearches for a vertex in theverticeslist that has the same x and y values as the provided vertex. If found, it removes it and returnstrue. If the vertex is not found, it returnsfalse. -
The method
getNumberOfSidesreturns the number of sides of the currentPolygonobject.
The Point class represents a point in a two-dimensional space and includes the following features:
-
The class has attributes for
xandycoordinates. -
It provides constructors, getters, and setters for the attributes.
-
The method
computeDistanceFromOrigincalculates and returns the distance between the point (given itsxandyvalues) and the origin(0,0)using the distance formula.
The Line class represents a line segment between two points and includes the following features:
-
The class has two private attributes,
p1andp2, representing the starting and ending points of the line. -
The constructor initializes each parameter to its corresponding attribute.
-
The method
getMidpointreturns the midpoint of the line as aPointobject, calculated using the midpoint formula. -
The method
computeLengthcalculates and returns the distance between thep1andp2attributes using the distance formula. -
The method
computeSlopecalculates and returns the slope of the line using the slope function. If the denominator would result in 0, the slope is overridden with a value of 8 to avoid an undefined value. Note that the numerator is cast to typedoublebefore dividing it by the denominator to ensure the result is of typedouble.
The Triangle class extends the Polygon class and includes additional features specific to triangles:
-
It includes an enum called
TriangleTypethat represents the type of triangle (equilateral, isosceles, or scalene). -
The default constructor calls the default constructor of the superclass and assigns the type based on the vertices, which are obtained from the superclass. Triangles are categorized as equilateral if all sides are equal, isosceles if at least 2 sides are equal, and scalene if no sides are equal.
-
The constructor with three parameters calls the corresponding constructor of the superclass and assigns the type based on the vertices obtained from the superclass.
-
The method
getTypereturns the value of thetypeattribute of theTriangleclass. -
The
addVertexmethod is overridden from thePolygonclass and displays the text "You cannot add another point in a triangle." -
The
removeVertexmethod is overridden from thePolygonclass and displays the text "You cannot remove another point in a triangle" and returnsfalse.
The Rectangle class extends the Polygon class and includes additional features specific to rectangles:
-
The constructor with four parameters creates a rectangle based on the Cartesian coordinates provided. It utilizes the constructor and methods of the superclass to implement this constructor.
-
The method
computeDiagonalLengthreturns the length of the diagonal of the rectangle. -
The method
getCentroidreturns the centroid point of the rectangle. -
The
addVertexmethod is overridden from thePolygonclass and displays the text "You cannot add another point in a rectangle." -
The
removeVertexmethod is overridden from thePolygonclass and displays the text "You cannot remove another point in a rectangle" and returnsfalse.
Contributions to this repository are welcome. If you would like to contribute, please follow these steps:
-
Fork the repository by clicking on the "Fork" button on the repository's page.
-
Clone your forked repository to your local machine:
git clone https://github.com/your-username/name-of-repo.git -
Create a new branch for your changes:
git checkout -b feature/new-feature -
Make your desired changes and commit them:
git commit -m "Add new feature" -
Push your changes to your forked repository:
git push origin feature/new-feature -
Open a pull request on the original repository's page and describe your changes.
Once your pull request is reviewed and approved, it will be merged into the main repository. Thank you for your contribution!
This project was created by Sir Jomari Joseph Barera, an instructor at Visayas State University, as part of the Laboratory Exam in OOP in Java.