Advertisement
Guest User

Untitled

a guest
Mar 27th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. //Create a Polygon class. A polygon is a closed shape with lines joining the corner points.
  2. //You will keep the points in an array list. Use object of java.awt.Point for the point.
  3.  
  4. //Polygon will have as an instance variable an ArrayList of Points to hold the points
  5. //The constructor takes no parameters but initializes the instance variable
  6. //
  7. //The add method adds a Point to the polygon
  8. //
  9. //The perimeter method returns the perimeter of the polygon
  10. //
  11. //The draw method draws the polygon by connecting consecutive points and then
  12. //connecting the last point to the first.
  13. //
  14. //No methods headers or javadoc is provided this time. You get to try your hand at writing a class almost from scratch
  15. //
  16. // Need help starting this question? In the lesson titled
  17. // "Starting points: Problem Set Questions", go to the
  18. // problem titled "Problem Set 6 - Question 3" for some tips on
  19. // how to begin.
  20. //
  21.  
  22. import java.util.ArrayList;
  23. import java.awt.Point;
  24. public class Polygon
  25. {
  26. // TODO: provide the required constructor, instance variable, and methods
  27.  
  28. ArrayList<Point> points; // instance variable
  29.  
  30. /**
  31. * Constructor for objects of class ArrayPointsMethods
  32. */
  33. public void ArrayPointsMethods(ArrayList<Point> arrayList)
  34. {
  35. // initialize instance variables
  36. points = arrayList;
  37. }
  38.  
  39.  
  40. /**
  41. * Add a point to the polygon.
  42. */
  43. public void add(Point point)
  44. {
  45. points.add(point); // Point newPoint = new Point(x, y); and then points.add(newPoint)
  46. }
  47.  
  48. /**
  49. * return perimeter of polygon
  50. */
  51.  
  52. public int perimeter()
  53. {
  54.  
  55. int perimeter = 0;
  56. int x1 = 0;
  57. int y1 = 0;
  58. int x2 = 0;
  59. int y2 = 0;
  60.  
  61. for (int i = 0; i <= points.size() ; i++)
  62. {
  63. if (i == 0)
  64. {
  65. x1 = points.get(i).x;
  66. y1 = points.get(i).y;
  67. }
  68.  
  69.  
  70. else if (i == points.size())
  71. {
  72. x2 = points.get(0).x;
  73. y2 = points.get(0).y;
  74. int distance = (int)Math.sqrt(Math.pow(x2-x1,2) + Math.pow(y2-y1,2));
  75. perimeter = perimeter + distance;
  76. }
  77.  
  78. else
  79. {
  80. x2 = points.get(i).x;
  81. y2 = points.get(i).y;
  82.  
  83. int distance = (int)Math.sqrt(Math.pow(x2-x1,2) + Math.pow(y2-y1,2));
  84. perimeter = perimeter + distance;
  85.  
  86. x1 = x2;
  87. y1 = y2;
  88. }
  89. }
  90.  
  91. return perimeter;
  92. }
  93.  
  94. public void draw()
  95. {
  96. int x1 = 0;
  97. int y1 = 0;
  98. int x2 = 0;
  99. int y2 = 0;
  100.  
  101. for (int i = 0; i <= points.size() ; i++)
  102. {
  103. if (i == 0)
  104. {
  105. x1 = points.get(i).x;
  106. y1 = points.get(i).y;
  107. }
  108.  
  109.  
  110. else if (i == points.size())
  111. {
  112. x2 = points.get(0).x;
  113. y2 = points.get(0).y;
  114. Line line = new Line(x1, y1, x2, y2);
  115. line.draw();
  116. }
  117.  
  118. else
  119. {
  120. x2 = points.get(i).x;
  121. y2 = points.get(i).y;
  122.  
  123. Line line = new Line(x1, y1, x2, y2);
  124. line.draw();
  125.  
  126. x1 = x2;
  127. y1 = y2;
  128. }
  129. }
  130.  
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement