forked from HarryDulaney/intro-to-java-programming
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRegularPolygon.java
149 lines (115 loc) · 4.14 KB
/
RegularPolygon.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package ch_09.exercise09_09;
//|_____________________________UML Diagram_____________________________________|
/* |
* RegularPolygon |
* _____________________________________________________________________________|
* |
* + n:int (defines # of sides default 3) |
* + side:double (length of side default 1) |
* + x:double (x of center coordinate default 0) |
* + y:double (y of center coordinate default 0) |
* _____________________________________________________________________________|
* RegularPolygon() |
* RegularPolygon(sides:int,lengthSide:double) (at (0,0)) |
* RegularPolygon(sides:int,lengthSide:double,x:double,y:double) (x,y) |
* getSide(): double |
* setSide(side: double) :void |
* getNumSides(): double |
* setNumSides(numSides:int) :void |
* setX(newX:int) :void |
* getX(): double |
* setY(newY:int) :void |
* getY(): double |
* getPerimeter() |
* getArea() : double |
* |
* |
* |
*______________________________________________________________________________*/
/**
* Exercise 9.9 (Geometry: n-sided regular polygon) In an n-sided regular polygon,
* all sides have the same length and all angles have the same degree (i.e.,
* the polygon is both equilateral and equiangular). Design a class named
* RegularPolygon that contains:
* <p>
* A private int data field named n that defines the number of sides in the polygon with default value 3.
* <p>
* A private double data field named side that stores the length of the side with default value 1.
* <p>
* A private double data field named x that defines the x-coordinate of the polygon's center with default value 0.
* <p>
* A private double data field named y that defines the y-coordinate of the polygon's center with default value 0.
* <p>
* A no-arg constructor that creates a regular polygon with default values.
* <p>
* A constructor that creates a regular polygon with the specified number of sides and length of side, centered at (0, 0).
* <p>
* A constructor that creates a regular polygon with the specified number of sides, length of side, and x- and y-coordinates.
* <p>
* The accessor and mutator methods for all data fields.
* <p>
* The method getPerimeter() that returns the perimeter of the polygon.
* <p>
* The method get
* Area() that returns the area of the polygon. The formula for computing the area of a regular polygon is
* Area=n�s24�tan(n).
* <p>
* Draw the UML diagram for the class and then implement the class.
* <p>
* Write a test program that creates three RegularPolygon objects,
* created using the no-arg constructor, using RegularPolygon(6, 4),
* and using RegularPolygon(10, 4, 5.6, 7.8). For each object,
* display its perimeter and area.
*/
public class RegularPolygon {
private int n;
private double side;
private double x;
private double y;
RegularPolygon() {
n = 3;
side = 1;
x = 0;
y = 0;
}
RegularPolygon(int numSides, double sideLength) {
n = numSides;
side = sideLength;
}
RegularPolygon(int numSides, double sideLength, double ex, double why) {
n = numSides;
side = sideLength;
x = ex;
y = why;
}
public void setNumSides(int newNum) {
n = newNum;
}
public int getNumSides() {
return n;
}
public void setSide(double newLength) {
side = newLength;
}
public double getSide() {
return side;
}
public void setX(int newX) {
x = newX;
}
public double getX() {
return x;
}
public void setY(double newY) {
y = newY;
}
public double getY() {
return y;
}
public double getPerimeter() {
return n * side;
}
public double getArea() {
return n * (Math.pow(side, 2) / 4 * Math.tan(Math.PI / n));
}
}