-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolygon-program-pseudocode.txt
More file actions
144 lines (102 loc) · 3.6 KB
/
polygon-program-pseudocode.txt
File metadata and controls
144 lines (102 loc) · 3.6 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
// polygon_test.cpp pseudocode
// create a class RegularPolygon
Declare class Regular Polygon {
// define private variables
Private:
declare int nSides;
declare double sideLength;
// define public constructors and methods
Public:
// create no-argument constructor
declare RegularPolygon() {
// set variables to default values
set nSides = 3;
set sideLength = 1;
}
// create constructor that creates a regular polygon
// with the specified number of sides and length of side
declare RegularPolygon(int sides, double length) {
// set variables to parameter values and call getter
// and setter methods
setNSides(sides);
setSideLength(length);
}
// create getter and setter methods for both variables
// setter method for nSides
declare void setNSides(int sides) {
// if passed value is less than 3, set it to 3
Set nSides = std::max(3, sides);
}
// getter method for nSides
Declare int getNSides() {
return nSides;
}
// setter method for sideLength. If passed value is
// less than 0, the value of sideLength will be abs of that value
Declare void setSideLength(int length) {
if (length < 0) {
Set sideLength = abs(length);
}
Set sideLength = length;
}
// getter method for sideLength
Declare int getSideLength() {
return sideLength;
}
// create display method
declare void display() {
printf("Number of sides: %i Side length: %.2f",
nSides, sideLength);
}
// create getPerimeter method
declare double getPerimeter() {
// calculating the perimeter
Set double perimeter = nSides * sideLength;
// returning the value of perimeter
return perimeter;
}
// create getArea method
declare double getArea() {
// calculating the area using some functions from the
// cmath library
Set double area = (nSides * pow(sideLength, 2)) /
(4 * tan(M_PI / nSides));
// returning the value of area
return area;
}
};
// create main function
Int main (void) {
// create default object from class
Declare RegularPolygon polyA;
// call perimeter and area methods and store values
// call display method to display traits and print perimeter and area
Set double perimeter1 = polyA.getPerimeter();
Set double area1 = polyA.getArea();
printf("\nNormal Polygon\n");
Call polyA.display();
printf("\nPerimeter: %.2f\nArea: %.2f\n\n", perimeter1,
area1);
// create object from class with parameters
RegularPolygon polyB(6, 4.0);
// call perimeter and area methods and store values
// call display method to display traits and print perimeter and area
Set double perimeter2 = polyB.getPerimeter();
Set double area2 = polyB.getArea();
printf("Polygon 2\n");
call polyB.display();
printf("\nPerimeter: %.2f\nArea: %.2f\n\n", perimeter2,
area2);
// create object from class with parameters
RegularPolygon polyC(4, 7.5);
// call perimeter and area methods and store values
// call display method to display traits and print perimeter and area
Set double perimeter3 = polyC.getPerimeter();
Set double area3 = polyC.getArea();
printf("Polygon 3\n");
Call polyC.display();
printf("\nPerimeter: %.2f\nArea: %.2f\n\n", perimeter3,
area3);
// conclude program
Return 0;
}