Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help me with my lab, thanks! **MyPolyDriver.java** **MyPolygon** Thanks a

ID: 3859128 • Letter: P

Question

Please help me with my lab, thanks!

**MyPolyDriver.java**

**MyPolygon**

Thanks again!

Part I: Coding Download MyPolygon.java (attached to this assignment). A MyPolygon object represents a polygon (an n-sided shape in two-dimensional space). The object keeps track of the lengths of each of the sides of the polygon. (Note that it does not keep track of the locations of sides or of corners.) It supports the following operations: constructor: Initializes the object with a given number of sides. Initial length of every side is 0.0 constructor: Initializes the object with a given number of sides. Initial length of every side is 0.0 * void setsideLength(int side, double length): Sets the length of the specified side. (Assume that the sides are numbered starting at zero.) double getPerimeter): Returns the perimeter of the polygon, i.e. the total length of all the sides. For each piece of information that needs to be stored, define the necessary field in the Mypolygon class. Then fill in the code for each of the methods Do NOT worry about erroneous inputs- assume all parameters to all method calls are valid. Hand in your file MyPolygon.java now. You can hand in MyPolygon.java again later if you change it while doing Part II, but meanwhile you will know that at least your work on Part I is safely handed in. You can hand it in as many times as you want; we only grade the last on-time version of a given file. Part II: Testing with a test driver Download MyPolyDriver.java (attached to this assignment). MyPolyDriver.java is a skeleton of a driver that contains examples of how to call methods on a MyPolygon object. Improve this driver to be a thorough test of the methods you wrote. Run your driver. If you find a bug in either MyPolygon.java or MyPolyDriver.java, try to fix it. Then test again

Explanation / Answer

Below is your code. Let me know in comments if you find any issue.

MyPolygon.java

public class MyPolygon {

// define your fields here

//Array to hold the length of sides of polygon

double[] polygonsides;

// constructor - initializes a MyPolygon to have numsides

// sides, each of length 0

public MyPolygon(int numsides) {

polygonsides = new double[numsides];

}

// sets length of given side to length. side is a number

// from 0 (inclusive) up to number of sides (exclusive)

public void setSideLength(int side, double length) {

polygonsides[side] = length;

}

// returns sum of all the side lengths

public double getPerimeter() {

double sum = 0;

for (int i = 0; i < polygonsides.length; i++) {

sum = sum + polygonsides[i];

}

return sum;

}

}

MyPolyDriver.java

public class MyPolyDriver {

public static void main(String[] args) {

MyPolygon p = new MyPolygon(3); // replace 3 with any number > 0

//Initializing side lengths

p.setSideLength(0, 9.87);

p.setSideLength(1, 9.87);

p.setSideLength(2, 9.87);

//Getting and printing the perimeter

System.out.println(p.getPerimeter());

}

}

Sample Run: -

29.61