Polygon class
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 filed named perimeter that defines the perimeter of the polygon.
A no argument constructor that creates a Polygon using the default number of sides, default side length, and default x- and y-coordinates.
A constructor that creates a Polygon using a specified number of sides, side length, and x- and y-coordinates
Getter methods for all data fields
A getPerimeter() method that returns a double value representing the perimeter of the Polygon.
A toString() method that displays the number of sides, side length, x-coordinate, and y-coordinates in String format
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 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, the following test data may result:
***Output*** toString(): (numsides=3, sideLength=2.0, xcoord=1.0,ycoord=1.0) getNumSides(): 3 getSideLength(): 2.0 getXCoord(): 1.0 getYCoord(): 1.0 getPerimeter(): 6.0
Document your test cases in the form of table with columns indicating the input values, expected output, actual output and if the test case passed or failed. This table should contain 4 columns with appropriate labels and a row for each test case. An example template is shown below. Note that the actual output should be the actual results you receive when running your program and applying the input for the test record.
Keep in mind, for five Polygons, you will have five different output results. Also, note there is no requirement to actually draw a Polygon.
Example test cases:
The google recommended Java style guide, provided as link in the week 2 content, should be used to format and document your code. Specifically, the following style guide attributes should be addressed:
Header comments include filename, author, date and brief purpose of the program.
In-line comments used to describe major functionality of the code.
Meaningful variable names and prompts applied.
Class names are written in UpperCamelCase.
Variable names are written in lowerCamelCase.
Constant names are in written in All Capitals.
Braces use K&R style.
Solution
Polygon.java
/**
* This file defines the polygon class.
*/
public class Polygon {
// number of sides and length of each side
privateintnumSides;
private double sideLength;
// coordinate of the center of the polygon
private double xCoord;
private double yCoord;
private double perimeter;
// create the polygon with default parameters
public Polygon() {
numSides = 4;
sideLength = 5.0;
xCoord = 0.0;
yCoord = 0.0;
perimeter = numSides * sideLength;
}
// create a new polygon with specified parameters
public Polygon(intnumSides, double sideLength, double xCoord, double yCoord) {
this.numSides = numSides;
this.sideLength = sideLength;
this.xCoord = xCoord;
this.yCoord = yCoord;
perimeter = numSides * sideLength;
}
// getter methods
publicintgetNumSides() {
returnnumSides;
}
public double getSideLength() {
returnsideLength;
}
public double getXCoord() {
returnxCoord;
}
public double getYCoord() {
returnyCoord;
}
// return the perimeter of the polygon
public double getPerimeter() {
return perimeter;
}
// return a string representation of the polygon
public String toString() {
return “(numsides=” + getNumSides() +
“, sideLength=” + getSideLength() +
“, xcoord=” + getXCoord() + “,ycoord=” + getYCoord() + “)”;
}
}
TestPolygon.java
/**
* This program tests the implementation of the Polygon class.
*/
public class TestPolygon {
// output the information of the polygon of this test case.
private static void test(Polygon p) {
System.out.println(“** Output **”);
System.out.println(“toString(): ” + p);
System.out.println(“getNumSides(): ” + p.getNumSides());
System.out.println(“getSideLength(): ” + p.getSideLength());
System.out.println(“getXCoord(): ” + p.getXCoord());
System.out.println(“getYCoord(): ” + p.getYCoord());
System.out.println(“getPerimeter(): ” + p.getPerimeter());
System.out.println();
}
public static void main(String[] args) {
// create 5 test cases and display the information of polygons
Polygon p1 = new Polygon(3, 2.0, 1.0, 1.0);
Polygon p2 = new Polygon(); // default constructor
Polygon p3 = new Polygon(8, 2.0, 2.0, 3.5);
Polygon p4 = new Polygon(6, 1.5, -1.0, 1.0);
Polygon p5 = new Polygon(4, 3.2, 10.0, -1.0);
test(p1);
test(p2);
test(p3);
test(p4);
test(p5);
}
}
Input | Expected Output | Actual Output | Pass? |
Constructor:
numsides=3 sideLength=2.0 xcoord=1.0 ycoord=1.0
|
** Output **
toString(): (numsides=3, sideLength=2.0, xcoord=1.0,ycoord=1.0) getNumSides(): 3 getSideLength(): 2.0 getXCoord(): 1.0 getYCoord(): 1.0 getPerimeter(): 6.0
|
** Output **
toString(): (numsides=3, sideLength=2.0, xcoord=1.0,ycoord=1.0) getNumSides(): 3 getSideLength(): 2.0 getXCoord(): 1.0 getYCoord(): 1.0 getPerimeter(): 6.0 |
Yes |
Default Constructor | ** Output **
toString(): (numsides=4, sideLength=5.0, xcoord=0.0,ycoord=0.0) getNumSides(): 4 getSideLength(): 5.0 getXCoord(): 0.0 getYCoord(): 0.0 getPerimeter(): 20.0 |
** Output **
toString(): (numsides=4, sideLength=5.0, xcoord=0.0,ycoord=0.0) getNumSides(): 4 getSideLength(): 5.0 getXCoord(): 0.0 getYCoord(): 0.0 getPerimeter(): 20.0 |
Yes |
Constructor:
numsides=8 sideLength=2.0 xcoord=2.0 ycoord=3.5
|
** Output **
toString(): (numsides=8, sideLength=2.0, xcoord=2.0,ycoord=3.5) getNumSides(): 8 getSideLength(): 2.0 getXCoord(): 2.0 getYCoord(): 3.5 getPerimeter(): 16.0 |
** Output **
toString(): (numsides=8, sideLength=2.0, xcoord=2.0,ycoord=3.5) getNumSides(): 8 getSideLength(): 2.0 getXCoord(): 2.0 getYCoord(): 3.5 getPerimeter(): 16.0 |
Yes |
Constructor:
numsides=6 sideLength=1.5 xcoord=-1.0 ycoord=1.0
|
** Output **
toString(): (numsides=6, sideLength=1.5, xcoord=-1.0,ycoord=1.0) getNumSides(): 6 getSideLength(): 1.5 getXCoord(): -1.0 getYCoord(): 1.0 getPerimeter(): 9.0 |
** Output **
toString(): (numsides=6, sideLength=1.5, xcoord=-1.0,ycoord=1.0) getNumSides(): 6 getSideLength(): 1.5 getXCoord(): -1.0 getYCoord(): 1.0 getPerimeter(): 9.0 |
Yes |
Constructor:
numsides=4 sideLength=3.2 xcoord=10.0 ycoord=-1.0
|
** Output **
toString(): (numsides=4, sideLength=3.2, xcoord=10.0,ycoord=-1.0) getNumSides(): 4 getSideLength(): 3.2 getXCoord(): 10.0 getYCoord(): -1.0 getPerimeter(): 12.8 |
** Output **
toString(): (numsides=4, sideLength=3.2, xcoord=10.0,ycoord=-1.0) getNumSides(): 4 getSideLength(): 3.2 getXCoord(): 10.0 getYCoord(): -1.0 getPerimeter(): 12.8 |
Yes |
Output Screenshot of test cases 1-5 (the constructor parameters are indicated in the above table).