Author: Joshua Abens Kayiwa
This Console-based C# code calculates the Area, perimeter, surface area and volume
of the following polygons: square, rechtangle, parellelogram, trapezoid, triangle,
circle, cube, prism (both rechtangular and triangular base), cylinder, cone,
sphere, pyramid and rhombus. All the user does is to input the polygon's dimensions
and the code will produce the polygon's attributes.
1 using System;
2
3 namespace Assign1
4 {
5 /// <summary>
6 /// Just new into the C# programming world - learning class inheritance
7 ///
8 ///
9 /// This Console-based C# code calculates the Area, perimeter, surface area and
10 volume of
11 /// the following polygons: square, rechtangle, parellelogram, trapezoid,
12 triangle, circle,
13 /// cube, prism (both rechtangular and triangular base), cylinder, cone, sphere,
14 pyramid and
15 /// rhombus.
16 /// All the user does is to input the polygon's dimensions and the code will
17 produce the
18 /// polygon's attributes
19 ///
20 /// Programmer: Joshua Abens Kayiwa
21 /// Date: 28th Feb 05
22 ///
23 /// Please get back to me (j_abens@hotmail.com) with comments
24 ///
25 /// </summary>
26 ///
27
28 public enum polygon_type
29 {
30 square = 1,
31 rechtangle,
32 parallelogram,
33 trapezoid,
34 triangle,
35 circle,
36 cube,
37 cylinder,
38 cone,
39 sphere,
40 pyramid,
41 rhombus,
42 prism_rechtangular_base,
43 prism_triangular_base
44 }
45
46 public class polygon_dimensions
47 {
48 private double _radius;
49 private double _height;
50 private double _base;
51 private double _widith;
52
53 public double radius
54 {
55 get
56 {
57 if (_radius < 0)
58 return 0.00;
59 else
60 return _radius;
61 }
62
63 set {_radius = value;}
64 }
65
66 public double height
67 {
68 get
69 {
70 if (_height < 0)
71 return 0.00;
72 else
73 return _height;
74 }
75
76 set {_height = value;}
77 }
78
79 public double side
80 {
81 get
82 {
83 if (_base < 0)
84 return 0.00;
85 else
86 return _base;
87 }
88 set {_base = value;}
89 }
90
91 public double widith
92 {
93 get
94 {
95 if (_widith < 0)
96 return 0.00;
97 else
98 return _widith;
99 }
100 set {_widith = value;}
101 }
102
103 // initialization class constructs
104 public polygon_dimensions()
105 {
106 _radius = _height = _base = _widith = 0.00;
107 }
108
109 public polygon_dimensions(double r)
110 {
111 _radius = r;
112 _height = _base = _widith = 0.00;
113 }
114
115 public polygon_dimensions (double r, double h)
116 {
117 _radius = r;
118 _height = h;
119 _base = _widith = 0.00;
120 }
121
122 public polygon_dimensions (double r, double h, double b)
123 {
124 _radius = r;
125 _height = h;
126 _base = b;
127 _widith = 0.00;
128 }
129
130 public polygon_dimensions (double r, double h, double b, double w)
131 {
132 _radius = r;
133 _height = h;
134 _base = b;
135 _widith = w;
136 }
137
138 public void double_lines(int length)
139 {
140 //Method accomplishments: To draw a horizontal double-line on the console output
141 //Arguments: length - an int determining the length of the line drawn
142 //Return Value: a straight double-line on the console output
143
144 int i;
145 if (length > 70) //keep the double-line length between 15 - 70 spaces
146 length = 70;
147 else if (length < 5)
148 length = 15;
149
150 for (i = 1; i <= length; i++)
151 Console.Write ("=");
152 }
153
154 public string _area (polygon_type type)
155 {
156 double area;
157
158 switch ((int) type)
159 {
160 case 1: // square
161 area = (side * side);
162 return area.ToString();
163
164 case 2: // rechtangle
165 case 3: // paralellogram
166 case 12: // Rhombus
167 area = (side * widith);
168 return area.ToString ();
169
170 case 4: // trapezoid
171 area = 0.5 * height * (side * widith);
172 return area.ToString ();
173
174
175 case 5: // Triangle
176 area = 0.5 * side * height;
177 return area.ToString ();
178
179
180 case 6: // Circle
181 area = Math.PI * _radius * _radius;
182 return area.ToString ();
183
184
185 case 7: // Cube
186 case 8: // Cylinder
187 case 9: // Cone
188 case 10: // Sphere
189 case 11: // Pyramid
190 case 13: // Prism_rechtangular_base
191 case 14: // Prism_triangular_base
192 return "< N/A >";
193
194
195 default:
196 return "Unknown Polygon type";
197
198 }
199 }
200
201 public string _perimeter(polygon_type type)
202 {
203 // Task: the function calculates the perimeter of the passed on polygon
204 // Arguments: polygon_type
205 // Return Value: The polygon's perimeter
206
207 double perimeter = 0.00;
208
209 switch ((int) type)
210 {
211 case 1: // square
212 case 12: // rhombus
213 perimeter = 4 * side;
214 return perimeter.ToString ();
215
216
217 case 2: //rechtangle
218 case 3: // parelellogram
219 perimeter = 2 * (side * widith);
220 return perimeter.ToString ();
221
222
223 case 6: //circle
224 perimeter = 2 * Math.PI * radius;
225 return perimeter.ToString ();
226
227
228 case 4: // trapezoid
229 case 5: // triangle
230 case 7: // cube
231 case 8: // cylinder
232 case 9: // cone
233 case 10: // sphere
234 case 11: // pyramid
235 case 13: //prism_rechtangular_base
236 case 14: //prism_triangular_base
237 return "< N/A >";
238
239
240 default:
241 return "Unrecognised polygon type";
242
243 }
244 }
245
246 public string _surface_area (polygon_type type)
247 {
248 //Task: The function computes of the surface area of the passed on polygon type
249 //Arguments: polygon_type
250 //Return value: polygon's surface area
251
252 double surface_area = 0.00;
253
254 switch ((int) type)
255 {
256 case 1: // square
257 case 2: // rechtangle
258 case 3: // parallelogram
259 case 4: // trapezoid
260 case 5: // triangle
261 case 6: // circle
262 case 9: // cone
263 case 11: // pyramid
264 case 12: // rhombus
265 case 14: // prism_triangular_base
266 return "< N/A >";
267
268
269 case 7: //cube
270 surface_area = 6 * side * side;
271 return surface_area.ToString ();
272
273
274 case 8: //cylinder
275 surface_area = 2 * Math.PI * radius * (radius * height);
276 return surface_area.ToString ();
277
278
279 case 10: //sphere
280 surface_area = 4 * Math.PI * radius * radius;
281 return surface_area.ToString ();
282
283
284 case 13: //prism_triangula_base
285 surface_area = 2 * ((side * widith) + (side * height) + (height * widith));
286 return surface_area.ToString ();
287
288
289 default:
290 return "Unrecognised polygon type";
291
292 }
293 }
294
295 public string _volume (polygon_type type)
296 {
297 //Task: The function computes the volume of the passed on polygon
298 //Arguments: polygon_type
299 //Returns: the volume of the specified polygon
300
301 double volume = 0.00;
302
303 switch ((int) type)
304 {
305 case 1: // square
306 case 2: // rechtangle
307 case 3: // parallelogram
308 case 4: // trapezoid
309 case 5: // triangle
310 case 6: // circle
311 case 12: // rhombus
312
313 return "< N/A >";
314
315
316 case 7: // cube
317 volume = side * side * side ;
318 return volume.ToString ();
319
320
321 case 8: //cylinder
322 volume = Math.PI * radius * radius * height;
323 return volume.ToString ();
324
325
326 case 9: //cone
327 volume = (Math.PI * radius * radius * height)/3;
328 return volume.ToString ();
329
330
331 case 10: //sphere
332 volume = 4/3 * Math.PI * radius * radius * radius;
333 return volume.ToString ();
334
335
336 case 11: //pyramid
337 volume = (side * widith * height)/3;
338 return volume.ToString ();
339
340
341 case 13: //prism_rechtangular_base
342 volume = side * height * widith;
343 return volume.ToString ();
344
345
346 case 14: //prism_triangular_base
347 volume = 0.5 * side * height * widith;
348 return volume.ToString ();
349
350
351 default:
352 return "Unrecogonised polygon type";
353
354 }
355 }
356 }
357
358
359 public class Dimensions : polygon_dimensions
360 {
361 //Class accomplishments:
362 //Class methods:
363 //Class properties:
364
365 public void Dimension (polygon_type type)
366 {
367 //string units;
368
369 double_lines(59);
370 Console.WriteLine ("\n\tPlease input the polygon's dimensions\n");
371 double_lines(59);
372 Console.WriteLine ("\n");
373 switch ((int) type)
374 {
375 case 1: //square
376 case 7: //cube
377 Console.Write ("\n\tPolygon's base (m): ");
378 side = double.Parse (Console.ReadLine ());
379 break;
380
381 case 5: //triangle
382 Console.Write ("\n\tPolygon's base (m): ");
383 side = double.Parse (Console.ReadLine ());
384 Console.Write ("\tPolygon's height (m): ");
385 height = double.Parse (Console.ReadLine ());
386 break;
387
388 case 2: //rechtangle
389 case 3: //paralellogram
390 case 12: //rhombus
391 Console.Write ("\n\tPolygon's base (m): ");
392 side = double.Parse (Console.ReadLine ());
393 Console.Write ("\tPolygon's widith (m): ");
394 widith = double.Parse (Console.ReadLine ());
395 break;
396
397 case 4: //trapezoid
398 case 13: //prism_rechtangular_base
399 case 11: //pyramid
400 case 14: // triangular base prism
401 Console.Write ("\n\tPolygon's base (m): ");
402 side = double.Parse (Console.ReadLine ());
403 Console.Write ("\tPolygon's widith (m): ");
404 widith = double.Parse (Console.ReadLine ());
405 Console.Write ("\tPolygon's height (m): ");
406 height = double.Parse (Console.ReadLine ());
407 break;
408
409 case 6: //circle
410 case 10: //sphere
411 Console.Write ("\n\tPolygon's radius (m): ");
412 radius = double.Parse (Console.ReadLine ());
413 break;
414
415 case 8: //cylinder
416 case 9: //cone
417 Console.Write ("\n\tPolygon's radius (m): ");
418 radius = double.Parse (Console.ReadLine ());
419 Console.Write ("\tPolygon's height (m): ");
420 height = double.Parse (Console.ReadLine ());
421 break;
422
423 default:
424 break;
425 }
426
427 Console.WriteLine ("\n\n");
428 double_lines(60);
429 Console.WriteLine ("\nPolygon's Attributes\n");
430 double_lines(60);
431 Console.WriteLine ("\n\t Polygon Type\t:\t\t{0}", type.ToString ());
432 Console.WriteLine ("\t Polygon's area \t:\t{0:F}\n\t Polygon's Perimeter
433 \t:\t{1:F} \n\t Polygon's Surface Area :\t{2:F}\n\t Polygon's Volume \t:\t{3:F}"
434 _area(type), _perimeter(type), _surface_area(type), _volume(type));
435 }
436 }
437
438 public class polygons
439 {
440 public static void Main()
441 {
442 Dimensions d = new Dimensions ();
443 polygon_dimensions p = new polygon_dimensions ();
444 int choice;
445 char proceed;
446
447 do
448 {
449 Console.WriteLine ("\n\tA simple console-based c# program to calculate the
450 perimeter, area, \n\tsurface area and volume of several polygons ... Enjoy\n"
451 p.double_lines (50);
452 Console.WriteLine ("\n\t\tCOMPUTE POLYGON ATTRIBUTES\n");
453 p.double_lines (50);
454
455 Console.WriteLine ("\n Please select a polygon whose attributes you want to
456 compute"
457 Console.WriteLine ("\n1.\t Square \n2.\t Rechtangle \n3.\t Parallelogram \n4.\t
458 Trapezoid \n5. \t Triangle \n6\t Circle \n7.\t Cube \n8.\t Cylinder \n9.\t Cone
459 \n10.\t Sphere \n11.\t Pyramid \n12.\t Rhombus \n13.\t Prism - rechtangular base
460 \n14.\t Prism - triangular base"
461 do
462 {
463 Console.Write ("\n\tYour Choice: ");
464 choice = int.Parse (Console.ReadLine ());
465 if (choice <1 || choice > 14)
466 Console.WriteLine ("\aPlease input a valid entry");
467 }while (choice <1 || choice > 14);
468
469 d.Dimension ((polygon_type) choice);
470 Console.Write ("\n\n\nDo you want to continue computing polygon's attributes?
471 (y/n) "
472 proceed = char.Parse (Console.ReadLine ());
473 }while (proceed == 'y');
474 Console.Write ("\nJust new into the C# world. \nPlease contact me with comments
475 on j_abens@hotmail.com \n\nPress 'enter' to exit Console"
476 Console.Read ();
477 }
478 }
479 }
|