-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolygonTest.java
More file actions
158 lines (120 loc) · 3.71 KB
/
PolygonTest.java
File metadata and controls
158 lines (120 loc) · 3.71 KB
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
150
151
152
153
154
155
156
157
// Sukhamrit Singh
// Polygon Test
/*
The program will create three RegularPolygon objects, created using:
The no-argument constructor
RegularPolygon(6, 4)
RegularPolygon(10, 4, 5.6, 7.8)
For each object, display its perimeter and area, properly labeled.
*/
public class PolygonTest {
public static void main(String[] args) {
// Displays the first polygon
System.out.println("Polygon 1");
// Creating a new polygon from the class
// Default shape
RegularPolygon shape1 = new RegularPolygon();
// Prints the perimeter and area of the polygon
System.out.println("Perimeter: " + shape1.getPerimeter());
System.out.printf("Area: %.2f", shape1.getArea());
// Displays an empty line
System.out.println("");
System.out.println("");
// Displays the second polygon
System.out.println("Polygon 2");
// Creating a new polygon from the class
// Second Polygon with side and lenght only
RegularPolygon shape2 = new RegularPolygon(6, 4);
// Prints the perimeter and area of the polygon
System.out.println("Perimeter: " + shape2.getPerimeter());
System.out.printf("Area: %.2f", shape2.getArea());
// Displays an empty line
System.out.println("");
System.out.println("");
// Displays the third polygon
System.out.println("Polygon 3");
// Creating a new polygon from the class
// Third Polygon with side, length, coordinates
RegularPolygon shape3 = new RegularPolygon(10, 4, 5.6, 7.8);
// Prints the perimeter and area of the polygon
System.out.println("Perimeter: " + shape3.getPerimeter());
System.out.printf("Area: %.2f", shape3.getArea());
}
}
// Creating a new class
class RegularPolygon {
// Defined the data fields
private int n;
private double side;
private double x;
private double y;
// Created a no-argument constructor
RegularPolygon() {
// Set the data-fields to default values
this(3, 1.0);
}
/*
Created a constructor that creates a regular
polygon with the specified number of sides and
length of side
*/
RegularPolygon(int side, double length) {
// Set the data-fields to different values
this(side, length, 0.0, 0.0);
}
/*
Created a constructor that creates a regular
polygon with the specified number of sides,
length of side, and x-and y- coordinates
*/
RegularPolygon(int side, double length, double x, double y) {
// Set the data-fields to different values
this.n = side;
this.side = length;
this.x = x;
this.y = y;
}
/*
Generates the getter and setter
methods for the data fields
*/
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = side;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
// Created a constructor method
public double getPerimeter() {
// Defined the variable perimeter
double perimeter = n * side;
// Returned perimeter
return perimeter;
}
// Created a constructor method
public double getArea() {
// Defined the variable area
double area = n * Math.pow(side, 2) /
(4 * Math.tan(Math.PI / n));
// Returned area
return area;
}
}