Advertisement
Guest User

Exercise09_09

a guest
Apr 21st, 2018
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1.  
  2. public class Exercise09_09 {
  3.  
  4. public static void main(String[] args) {
  5. class RegularPolygon {
  6. private int n;
  7. private double side;
  8. private double x;
  9. private double y;
  10.  
  11.  
  12. public RegularPolygon() {
  13. n = 3;
  14. side = 1;
  15. x = 0;
  16. y = 0;
  17.  
  18. }
  19.  
  20. /* Centered at (0,0) */
  21. public RegularPolygon(int numOfSides, double sideLength) {
  22. n = numOfSides;
  23. side = sideLength;
  24. x = 0;
  25. y = 0;
  26. }
  27.  
  28. /*Polygon not centered at (0,0) */
  29. public RegularPolygon(int numOfSides, double sideLength, double xCoord, double yCoord) {
  30. n = numOfSides;
  31. side = sideLength;
  32. x = xCoord;
  33. y = yCoord;
  34. }
  35.  
  36.  
  37.  
  38.  
  39. public double getPerimeter() {
  40. return side * n;
  41. }
  42. public double getArea() {
  43. return (int) (n * Math.pow(side, 2) / ( 4 * Math.tan(Math.PI/n)));
  44. }
  45.  
  46.  
  47. }
  48.  
  49.  
  50. RegularPolygon regularPolygon1 = new RegularPolygon();
  51. RegularPolygon regularPolygon2 = new RegularPolygon(6, 4);
  52. RegularPolygon regularPolygon3 = new RegularPolygon(10, 4, 5.6, 7);
  53.  
  54. System.out.println("Polygon 1 perimeter: " + regularPolygon1.getPerimeter());
  55. System.out.println("Polygon 1 area: " + regularPolygon1.getArea());
  56. System.out.println("Polygon 2 perimeter: " + regularPolygon2.getPerimeter());
  57. System.out.println("Polygon 2 area: " + regularPolygon2.getArea());
  58. System.out.println("Polygon 3 perimeter: " + regularPolygon3.getPerimeter());
  59. System.out.println("Polygon 3 area: " + regularPolygon3.getArea());
  60.  
  61. }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement