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
vertices
is declared as aList
ofPoints
to store the vertices of the polygon. -
The default constructor initializes the
vertices
attribute with three points:(0,0)
,(1,1)
, and(1,0)
in the respective order. -
The constructor with three
Point
parameters initializes thevertices
attribute and adds the three parameters respectively to thevertices
list. -
The method
computePerimeter
calculates 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 thecomputeLength
method of theLine
class. Note that the last vertex is paired with the first vertex to complete the polygon. -
The method
computeArea
calculates 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
addVertex
adds a new vertex to thevertices
list. -
The method
removeVertex
searches for a vertex in thevertices
list 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
getNumberOfSides
returns the number of sides of the currentPolygon
object.
The Point
class represents a point in a two-dimensional space and includes the following features:
-
The class has attributes for
x
andy
coordinates. -
It provides constructors, getters, and setters for the attributes.
-
The method
computeDistanceFromOrigin
calculates and returns the distance between the point (given itsx
andy
values) 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,
p1
andp2
, representing the starting and ending points of the line. -
The constructor initializes each parameter to its corresponding attribute.
-
The method
getMidpoint
returns the midpoint of the line as aPoint
object, calculated using the midpoint formula. -
The method
computeLength
calculates and returns the distance between thep1
andp2
attributes using the distance formula. -
The method
computeSlope
calculates 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 typedouble
before 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
TriangleType
that 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
getType
returns the value of thetype
attribute of theTriangle
class. -
The
addVertex
method is overridden from thePolygon
class and displays the text "You cannot add another point in a triangle." -
The
removeVertex
method is overridden from thePolygon
class 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
computeDiagonalLength
returns the length of the diagonal of the rectangle. -
The method
getCentroid
returns the centroid point of the rectangle. -
The
addVertex
method is overridden from thePolygon
class and displays the text "You cannot add another point in a rectangle." -
The
removeVertex
method is overridden from thePolygon
class 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.