Question: 1 . Run and study the example so that you understand all of its code. 2. Convert the class Polygon from using an array of
1 . Run and study the example so that you understand all of its code.
2. Convert the class Polygon from using an array of Point2d to using a java.util.ArrayList
(a) it is not necessary to set the size of the ArrayList in a constructor.
(b) it is not necessary to keep a counter for the number of points stored in the ArrayList (
c) it is not necessary to check to see if the ArrayList is full before adding a new point.
3. Make any changes to the demonstration code in class PA8_Polygon in order to run using the newly reworked class Polygon, but otherwise keep the purpose of the code.
----------------------------------------------------------------------------------------------------------------
Code that is given
package pa8_polygon;
public class PA8_Polygon {
public static void main(String[] args) { Polygon p = new Polygon(5); p.addPoint(new Point2d(.1, .9)); System.out.println("p - points: " + p.getPoints() + ", max points: " + p.getMaxPoints()); p.addPoint(.3, .7); System.out.println("p - points: " + p.getPoints() + ", max points: " + p.getMaxPoints()); p.addPoint(new Point2d(.5, .5)); System.out.println("p - points: " + p.getPoints() + ", max points: " + p.getMaxPoints()); p.addPoint(.4, .8); System.out.println("p - points: " + p.getPoints() + ", max points: " + p.getMaxPoints()); p.addPoint(new Point2d(.2, .5)); System.out.println("p - points: " + p.getPoints() + ", max points: " + p.getMaxPoints()); System.out.println(" p contains:"); System.out.println(p); Polygon p2 = new Polygon(10); p2.addPoint(new Point2d(.1, .9)); p2.addPoint(.3, .7); p2.addPoint(new Point2d(.5, .5)); p2.addPoint(.4, .8); p2.addPoint(new Point2d(.2, .5)); p2.addPoint(new Point2d(.9, .1)); p2.addPoint(.7, .3); p2.addPoint(new Point2d(.4, .5)); p2.addPoint(.8, .4); p2.addPoint(new Point2d(.5, .2)); System.out.println("p2 contains:"); for (int i=0; i } ----------------------------------------------------------------------------- package pa8_polygon; public class Polygon { private Point2d[] p = null; private int points = 0; public Polygon() { p = new Point2d[3]; } public Polygon(int numPoints) { if (numPoints > 3) p = new Point2d[numPoints]; } public int getMaxPoints() { return p.length; } public int getPoints() { return points; } public boolean addPoint(Point2d point) { if (points < p.length) { p[points] = new Point2d(point.getX(), point.getY()); points++; return true; } return false; } public boolean addPoint(double x, double y) { if (points < p.length) { p[points] = new Point2d(x, y); points++; return true; } return false; } public Point2d getPoint(int index) { if (index < 0 || index >= p.length) return null; return new Point2d(p[index]); } public String toString() { String s = ""; for (Point2d q : p) s += q.toString() + " "; return s; } } ----------------------------------------------------------------------------------------------- package pa8_polygon; public class Point2d { private double x = 0, y = 0; public Point2d() { } public Point2d(double x, double y) { setX(x); setY(y); } public Point2d(Point2d p) { setX(p.x); setY(p.y); } public void setX(double initX) { if (initX >= 0 && initX <= 1) x = initX; } public void setY(double y) { if (y >= 0 && y <= 1) this.y = y; } public double getX() { return x; } public double getY() { return y; } public String toString() { return "(" + x + "," + y + ")"; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
