Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            string input = null;

            //Variable used for user input for creating circle.
            int diameter = 0;

            //Variables used for user input for creating triangle
            int sideOne   = 0;
            int sideTwo   = 0;
            int sideThree = 0;

            //Variables used for user input for creating rectangle
            int length = 0;
            int width  = 0;

            //Forces the program to loop through the prompt even if user creates an invalid object or enters an invalid value.
            do
            {
                Console.WriteLine("What shape do you want to create?");
                Console.WriteLine(" - Enter 1 for a circle");
                Console.WriteLine(" - Enter 2 for a triangle");
                Console.WriteLine(" - Enter 3 for a rectangle");
                input = Console.ReadLine();

                switch (input)
                {
                case "1":
                    Console.WriteLine("Enter the diameter of the circle: ");

                    try
                    {
                        diameter = int.Parse(Console.ReadLine());

                        var Circle = new Circle(diameter);
                        if (Circle.IsValid == true)
                        {
                            Console.WriteLine("The circle is valid");
                            Console.WriteLine("The area of the specified circle is: " + Circle.Area());
                            Console.WriteLine("The perimeter of the specified circle is: " + Circle.Perimeter());
                            Console.WriteLine();
                        }
                        else
                        {
                            Console.WriteLine("The circle is not valid");
                            Console.WriteLine();
                        }
                    }
                    catch
                    {
                        Console.WriteLine("You must enter a whole number for diameter.");
                        Console.WriteLine();
                    }
                    break;

                case "2":
                    try
                    {
                        Console.WriteLine("Enter the length of the first side: ");
                        sideOne = int.Parse(Console.ReadLine());
                        Console.WriteLine("Enter the length of the second side: ");
                        sideTwo = int.Parse(Console.ReadLine());
                        Console.WriteLine("Enter the length of the third side: ");
                        sideThree = int.Parse(Console.ReadLine());

                        var Triangle = new Triangle(sideOne, sideTwo, sideThree);
                        if (Triangle.IsValid == true)
                        {
                            Console.WriteLine("The triangle is valid");
                            Console.WriteLine("The area of the specified triangle is: " + Triangle.Area());
                            Console.WriteLine("The perimeter of the specified triangle is: " + Triangle.Perimeter());
                            Console.WriteLine();
                        }
                        else
                        {
                            Console.WriteLine("The triangle is not valid");
                            Console.WriteLine();
                        }
                    }
                    catch
                    {
                        Console.WriteLine("Please enter whole numbers for the length of side.");
                        Console.WriteLine();
                    }

                    break;

                case "3":
                    try
                    {
                        Console.WriteLine("Enter the length of the rectangle: ");
                        length = int.Parse(Console.ReadLine());
                        Console.WriteLine("Enter the width of the rectangle: ");
                        width = int.Parse(Console.ReadLine());

                        var Rectangle = new Rectangle(length, width);
                        if (Rectangle.IsValid == true)
                        {
                            Console.WriteLine("The rectangle is valid");
                            Console.WriteLine("The area of the specified triangle is: " + Rectangle.Area());
                            Console.WriteLine("The perimeter of the specified triangle is: " + Rectangle.Perimeter());
                            Console.WriteLine();
                        }
                        else
                        {
                            Console.WriteLine("The rectangle is not valid");
                            Console.WriteLine();
                        }
                    }
                    catch
                    {
                        Console.WriteLine("Please enter whole numbers only.");
                        Console.WriteLine();
                    }

                    break;

                case "exit":
                    System.Environment.Exit(0);
                    break;

                default:
                    Console.WriteLine("You did not enter a valid shape number");
                    Console.WriteLine();
                    break;
                }
            }while (input != "exit");
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Rectangles:");
            Rectangle r1 = new Rectangle(new Point(3, 4), new Point(7, 9));

            Console.WriteLine($"r1: {r1}");
            Console.WriteLine($"Perimeter: {r1.Perimeter()}   Area: {r1.Area()}");
            Console.WriteLine();
            Rectangle r2 = new Rectangle(new Point(3, 4), new Point(7, 9), new Point(-2, -2), new Point(8, 1));

            Console.WriteLine($"r2: {r2}");
            Console.WriteLine($"Perimeter: {r2.Perimeter()}   Area: {r2.Area()}");
            Console.WriteLine();

            Console.WriteLine("Triangles:");
            Triangle t1 = new Triangle(new Point(1, -4), new Point(9, 1), new Point(3, 4));

            Console.WriteLine($"t1: {t1}");
            Console.WriteLine($"t1 is scalene? {t1.IsScalene()}");
            Console.WriteLine($"t1 is isosceles? {t1.IsIsosceles()}");
            Console.WriteLine($"t1 is equilateral? {t1.IsEquilateral()}");
            Console.WriteLine($"Perimeter: {t1.Perimeter()}   Area: {t1.Area()}");
            Console.WriteLine();
            Triangle t2 = new Triangle(new Point(10, 0), new Point(0, 10), new Point(0, 0));

            Console.WriteLine($"t2: {t2}");
            Console.WriteLine($"t2 is scalene? {t2.IsScalene()}");
            Console.WriteLine($"t2 is isosceles? {t2.IsIsosceles()}");
            Console.WriteLine($"t2 is equilateral? {t2.IsEquilateral()}");
            Console.WriteLine($"Perimeter: {t2.Perimeter()}   Area: {t2.Area()}");
            Console.WriteLine();
            Triangle t3 = new Triangle(new List <Point>()
            {
                new Point(0, 0), new Point(10, 0), new Point(5, 8.660254037845)
            });

            Console.WriteLine($"t3: {t3}");
            Console.WriteLine($"t3 is scalene? {t3.IsScalene()}");
            Console.WriteLine($"t3 is isosceles? {t3.IsIsosceles()}");
            Console.WriteLine($"t3 is equilateral? {t3.IsEquilateral()}");
            Console.WriteLine($"Perimeter: {t3.Perimeter()}   Area: {t3.Area()}");
            Console.WriteLine();

            Console.WriteLine("Shapes:");
            List <Shape> shapes = new List <Shape>()
            {
                r1, r2, t1, t2, t3
            };

            foreach (Shape s in shapes)
            {
                Console.WriteLine(s);
                Console.WriteLine($"Perimeter: {s.Perimeter()}   Area: {s.Area()}");
            }
            Console.WriteLine();

            Console.WriteLine("Split r1:");
            Console.WriteLine($"r1: {r1}");
            Console.WriteLine($"Perimeter: {r1.Perimeter()}   Area: {r1.Area()}");
            var triangles = r1.ToTriangles();

            foreach (Triangle t in triangles)
            {
                Console.WriteLine(t);
                Console.WriteLine($"Perimeter: {t.Perimeter()}   Area: {t.Area()}");
            }
            double area = triangles[0].Area() + triangles[1].Area();

            Console.WriteLine($"r1 area == combined triangle area?: {Utils.IsRelativelyEqual(area, r1.Area())}");

            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to continue...");
            Console.ReadLine();
        }