Design a Java class named Polygon that contains:
A private int data field named numSides that defines the number of sides of the polygon. The default value should be 4.
A private double data field named sideLength that defines the length of each side. The default value should be 5.0.
A private double data field named xCoord that defines the x-coordinate of the center of the polygon. The default value should be 0.0.
A private double data field named yCoord that defines the y-coordinate of the center of the polygon. The default value should be 0.0.
A private double data field named apothem that defines the apothem of the polygon. The default value should 5.0.
A private double data filed named perimeter that defines the perimeter of the polygon. The default value should 20.0.
A no argument constructor that creates a Polygon using the default number of sides, default side length, default x- and y-coordinates, and the default apothem.
A constructor that creates a Polygon using a specified number of sides, side length, x- and y-coordinates and the apothem
Getter and setter methods for all data fields
A getArea() method that returns a double value representing the area of the polygon.
(Note the area of a regular Polgon can be calculated from 1⁄2 * Apothem*Perimeter)
A toString() method that displays the number of sides, side length, x-coordinate, y-
coordinate and apothem values in String format
Be sure your code compiles. You should use the command prompt and not a GUI for data entry and display.
Write a Java test program, named TestPolygon, to create 5 different polygons representing the 5 test cases you just created. When creating the five polygons, create one using the no argument constructor. For the remaining four, feel free to use any number of sides, side length and x-, and y-coordinates and apothem that are not equal to the default values and not equal to each other. For each of the five polygons, call all of the methods and display the results. For example for a Polygon with 3 sides, side length of 2.0 and x-coordinate and y-coordinates of 1.0, and apothem of 1.0 the following test data may result:
***Output***
toString(): (numsides=3, sideLength=2.0, xcoord=1.0,ycoord=1.0,
apothem=1.0) getNumSides(): 3
getSideLength(): 2.0
getXCoord(): 1.0
getYCoord(): 1.0 getApothem():1.0 getPerimeter(): 6.0
getArea(): 3.0
So, the complete solution for the same is as follows:
Source-Code;
Polygon class
class Polygon
{
private int numSides;
private double sideLength;
private double xCoord;
private double yCoord;
private double apothem;
private double perimeter;
public Polygon()
{
numSides = 4;
sideLength = 5.0;
xCoord = 0.0;
yCoord = 0.0;
apothem = 5.0;
perimeter = 20.0;
}
public Polygon(int no, double len, double x, double y, double
a)
{
numSides = no;
sideLength = len;
xCoord = x;
yCoord = y;
apothem = a;
setPerimeter();
}
public int getNumSides() {
return numSides;
}
public void setNumSides(int numSides) {
this.numSides = numSides;
}
public double getSideLength() {
return sideLength;
}
public void setSideLength(double sideLength) {
this.sideLength = sideLength;
}
public double getXCoord() {
return xCoord;
}
public void setXCoord(double xCoord) {
this.xCoord = xCoord;
}
public double getYCoord() {
return yCoord;
}
public void setYCoord(double yCoord) {
this.yCoord = yCoord;
}
public double getApothem() {
return apothem;
}
public void setApothem(double apothem) {
this.apothem = apothem;
}
public double getPerimeter() {
return perimeter;
}
public void setPerimeter() {
perimeter = numSides * sideLength;
}
public double getArea()
{
double area;
area = apothem * perimeter / 2;
return area;
}
@Override
public String toString()
{
String str;
str = "(numsides=" + numSides +", sideLength=" + sideLength +",
xcoord="+xCoord + ", ycoord="+yCoord+",
apothem="+apothem+")";
return str;
}
}
TestPolygon class
public class TestPolygon {
public static void main(String[] args) {
Polygon p1 = new Polygon();
System.out.println("Polygon 1:");
System.out.println("toString(): "+ p1.toString() );
System.out.println("getNumSides(): "+p1.getNumSides());
System.out.println("getSideLength(): "+p1.getSideLength());
System.out.println("getXCoord(): "+p1.getXCoord());
System.out.println("getYCoord(): "+p1.getYCoord());
System.out.println("getApothem(): "+p1.getApothem());
System.out.println("getPerimeter(): "+p1.getPerimeter());
System.out.println("getArea(): "+p1.getArea());
System.out.println("");
Polygon p2 = new Polygon(3, 2.0, 1.0, 1.0, 1.0);
System.out.println("Polygon 2:");
System.out.println("toString(): "+ p2.toString() );
System.out.println("getNumSides(): "+p2.getNumSides());
System.out.println("getSideLength(): "+p2.getSideLength());
System.out.println("getXCoord(): "+p2.getXCoord());
System.out.println("getYCoord(): "+p2.getYCoord());
System.out.println("getApothem(): "+p2.getApothem());
System.out.println("getPerimeter(): "+p2.getPerimeter());
System.out.println("getArea(): "+p2.getArea());
System.out.println("");
Polygon p3 = new Polygon(5, 3.0, 1.5, 1.5, 1.0);
System.out.println("Polygon 3:");
System.out.println("toString(): "+ p3.toString() );
System.out.println("getNumSides(): "+p3.getNumSides());
System.out.println("getSideLength(): "+p3.getSideLength());
System.out.println("getXCoord(): "+p3.getXCoord());
System.out.println("getYCoord(): "+p3.getYCoord());
System.out.println("getApothem(): "+p3.getApothem());
System.out.println("getPerimeter(): "+p3.getPerimeter());
System.out.println("getArea(): "+p3.getArea());
System.out.println("");
Polygon p4 = new Polygon(6, 6.0, 0.0, 1.0, 2.0);
System.out.println("Polygon 4:");
System.out.println("toString(): "+ p4.toString() );
System.out.println("getNumSides(): "+p4.getNumSides());
System.out.println("getSideLength(): "+p4.getSideLength());
System.out.println("getXCoord(): "+p4.getXCoord());
System.out.println("getYCoord(): "+p4.getYCoord());
System.out.println("getApothem(): "+p4.getApothem());
System.out.println("getPerimeter(): "+p4.getPerimeter());
System.out.println("getArea(): "+p4.getArea());
System.out.println("");
Polygon p5 = new Polygon(7, 2.0, 2.0, 2.0, 3.0);
System.out.println("Polygon 5:");
System.out.println("toString(): "+ p5.toString() );
System.out.println("getNumSides(): "+p5.getNumSides());
System.out.println("getSideLength(): "+p5.getSideLength());
System.out.println("getXCoord(): "+p5.getXCoord());
System.out.println("getYCoord(): "+p5.getYCoord());
System.out.println("getApothem(): "+p5.getApothem());
System.out.println("getPerimeter(): "+p5.getPerimeter());
System.out.println("getArea(): "+p5.getArea());
System.out.println("");
}
}
Output:
Description:
Do comment if there is any query. Thank you. :)
Get Answers For Free
Most questions answered within 1 hours.