using System; namespace Assign1 { /// /// Just new into the C# programming world - learning class inheritance /// /// /// 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 /// /// Programmer: Joshua Abens Kayiwa /// Date: 28th Feb 05 /// /// Please get back to me (j_abens@hotmail.com) with comments /// /// /// public enum polygon_type { square = 1, rechtangle, parallelogram, trapezoid, triangle, circle, cube, cylinder, cone, sphere, pyramid, rhombus, prism_rechtangular_base, prism_triangular_base } public class polygon_dimensions { private double _radius; private double _height; private double _base; private double _widith; public double radius { get { if (_radius < 0) return 0.00; else return _radius; } set {_radius = value;} } public double height { get { if (_height < 0) return 0.00; else return _height; } set {_height = value;} } public double side { get { if (_base < 0) return 0.00; else return _base; } set {_base = value;} } public double widith { get { if (_widith < 0) return 0.00; else return _widith; } set {_widith = value;} } // initialization class constructs public polygon_dimensions() { _radius = _height = _base = _widith = 0.00; } public polygon_dimensions(double r) { _radius = r; _height = _base = _widith = 0.00; } public polygon_dimensions (double r, double h) { _radius = r; _height = h; _base = _widith = 0.00; } public polygon_dimensions (double r, double h, double b) { _radius = r; _height = h; _base = b; _widith = 0.00; } public polygon_dimensions (double r, double h, double b, double w) { _radius = r; _height = h; _base = b; _widith = w; } public void double_lines(int length) { //Method accomplishments: To draw a horizontal double-line on the console output //Arguments: length - an int determining the length of the line drawn //Return Value: a straight double-line on the console output int i; if (length > 70) //keep the double-line length between 15 - 70 spaces length = 70; else if (length < 5) length = 15; for (i = 1; i <= length; i++) Console.Write ("="); } public string _area (polygon_type type) { double area; switch ((int) type) { case 1: // square area = (side * side); return area.ToString(); case 2: // rechtangle case 3: // paralellogram case 12: // Rhombus area = (side * widith); return area.ToString (); case 4: // trapezoid area = 0.5 * height * (side * widith); return area.ToString (); case 5: // Triangle area = 0.5 * side * height; return area.ToString (); case 6: // Circle area = Math.PI * _radius * _radius; return area.ToString (); case 7: // Cube case 8: // Cylinder case 9: // Cone case 10: // Sphere case 11: // Pyramid case 13: // Prism_rechtangular_base case 14: // Prism_triangular_base return "< N/A >"; default: return "Unknown Polygon type"; } } public string _perimeter(polygon_type type) { // Task: the function calculates the perimeter of the passed on polygon // Arguments: polygon_type // Return Value: The polygon's perimeter double perimeter = 0.00; switch ((int) type) { case 1: // square case 12: // rhombus perimeter = 4 * side; return perimeter.ToString (); case 2: //rechtangle case 3: // parelellogram perimeter = 2 * (side * widith); return perimeter.ToString (); case 6: //circle perimeter = 2 * Math.PI * radius; return perimeter.ToString (); case 4: // trapezoid case 5: // triangle case 7: // cube case 8: // cylinder case 9: // cone case 10: // sphere case 11: // pyramid case 13: //prism_rechtangular_base case 14: //prism_triangular_base return "< N/A >"; default: return "Unrecognised polygon type"; } } public string _surface_area (polygon_type type) { //Task: The function computes of the surface area of the passed on polygon type //Arguments: polygon_type //Return value: polygon's surface area double surface_area = 0.00; switch ((int) type) { case 1: // square case 2: // rechtangle case 3: // parallelogram case 4: // trapezoid case 5: // triangle case 6: // circle case 9: // cone case 11: // pyramid case 12: // rhombus case 14: // prism_triangular_base return "< N/A >"; case 7: //cube surface_area = 6 * side * side; return surface_area.ToString (); case 8: //cylinder surface_area = 2 * Math.PI * radius * (radius * height); return surface_area.ToString (); case 10: //sphere surface_area = 4 * Math.PI * radius * radius; return surface_area.ToString (); case 13: //prism_triangula_base surface_area = 2 * ((side * widith) + (side * height) + (height * widith)); return surface_area.ToString (); default: return "Unrecognised polygon type"; } } public string _volume (polygon_type type) { //Task: The function computes the volume of the passed on polygon //Arguments: polygon_type //Returns: the volume of the specified polygon double volume = 0.00; switch ((int) type) { case 1: // square case 2: // rechtangle case 3: // parallelogram case 4: // trapezoid case 5: // triangle case 6: // circle case 12: // rhombus return "< N/A >"; case 7: // cube volume = side * side * side ; return volume.ToString (); case 8: //cylinder volume = Math.PI * radius * radius * height; return volume.ToString (); case 9: //cone volume = (Math.PI * radius * radius * height)/3; return volume.ToString (); case 10: //sphere volume = 4/3 * Math.PI * radius * radius * radius; return volume.ToString (); case 11: //pyramid volume = (side * widith * height)/3; return volume.ToString (); case 13: //prism_rechtangular_base volume = side * height * widith; return volume.ToString (); case 14: //prism_triangular_base volume = 0.5 * side * height * widith; return volume.ToString (); default: return "Unrecogonised polygon type"; } } } public class Dimensions : polygon_dimensions { //Class accomplishments: //Class methods: //Class properties: public void Dimension (polygon_type type) { //string units; double_lines(59); Console.WriteLine ("\n\tPlease input the polygon's dimensions\n"); double_lines(59); Console.WriteLine ("\n"); switch ((int) type) { case 1: //square case 7: //cube Console.Write ("\n\tPolygon's base (m): "); side = double.Parse (Console.ReadLine ()); break; case 5: //triangle Console.Write ("\n\tPolygon's base (m): "); side = double.Parse (Console.ReadLine ()); Console.Write ("\tPolygon's height (m): "); height = double.Parse (Console.ReadLine ()); break; case 2: //rechtangle case 3: //paralellogram case 12: //rhombus Console.Write ("\n\tPolygon's base (m): "); side = double.Parse (Console.ReadLine ()); Console.Write ("\tPolygon's widith (m): "); widith = double.Parse (Console.ReadLine ()); break; case 4: //trapezoid case 13: //prism_rechtangular_base case 11: //pyramid case 14: // triangular base prism Console.Write ("\n\tPolygon's base (m): "); side = double.Parse (Console.ReadLine ()); Console.Write ("\tPolygon's widith (m): "); widith = double.Parse (Console.ReadLine ()); Console.Write ("\tPolygon's height (m): "); height = double.Parse (Console.ReadLine ()); break; case 6: //circle case 10: //sphere Console.Write ("\n\tPolygon's radius (m): "); radius = double.Parse (Console.ReadLine ()); break; case 8: //cylinder case 9: //cone Console.Write ("\n\tPolygon's radius (m): "); radius = double.Parse (Console.ReadLine ()); Console.Write ("\tPolygon's height (m): "); height = double.Parse (Console.ReadLine ()); break; default: break; } Console.WriteLine ("\n\n"); double_lines(60); Console.WriteLine ("\nPolygon's Attributes\n"); double_lines(60); Console.WriteLine ("\n\t Polygon Type\t:\t\t{0}", type.ToString ()); Console.WriteLine ("\t Polygon's area \t:\t{0:F}\n\t Polygon's Perimeter \t:\t{1:F} \n\t Polygon's Surface Area :\t{2:F}\n\t Polygon's Volume \t:\t{3:F}", _area(type), _perimeter(type), _surface_area(type), _volume(type)); } } public class polygons { public static void Main() { Dimensions d = new Dimensions (); polygon_dimensions p = new polygon_dimensions (); int choice; char proceed; do { Console.WriteLine ("\n\tA simple console-based c# program to calculate the perimeter, area, \n\tsurface area and volume of several polygons ... Enjoy\n"); p.double_lines (50); Console.WriteLine ("\n\t\tCOMPUTE POLYGON ATTRIBUTES\n"); p.double_lines (50); Console.WriteLine ("\n Please select a polygon whose attributes you want to compute"); Console.WriteLine ("\n1.\t Square \n2.\t Rechtangle \n3.\t Parallelogram \n4.\t Trapezoid \n5. \t Triangle \n6\t Circle \n7.\t Cube \n8.\t Cylinder \n9.\t Cone \n10.\t Sphere \n11.\t Pyramid \n12.\t Rhombus \n13.\t Prism - rechtangular base \n14.\t Prism - triangular base"); do { Console.Write ("\n\tYour Choice: "); choice = int.Parse (Console.ReadLine ()); if (choice <1 || choice > 14) Console.WriteLine ("\aPlease input a valid entry"); }while (choice <1 || choice > 14); d.Dimension ((polygon_type) choice); Console.Write ("\n\n\nDo you want to continue computing polygon's attributes? (y/n) "); proceed = char.Parse (Console.ReadLine ()); }while (proceed == 'y'); Console.Write ("\nJust new into the C# world. \nPlease contact me with comments on j_abens@hotmail.com \n\nPress 'enter' to exit Console"); Console.Read (); } } }