コード例 #1
0
ファイル: TriangleTests.cs プロジェクト: wrswin/OOChallenge1
        public void Init()
        {
            ra1 = new RightAngleTriangle("White", 3, 4);
            eq1 = new EquilateralTriangle("black", 6);

            ra2 = new RightAngleTriangle("White", 7, 4);
            eq2 = new EquilateralTriangle("black", 8);
        }
コード例 #2
0
ファイル: Tests.cs プロジェクト: 102103545/OOP_Sem2
        public void Init()
        {
            sq1  = new Square("Blue", 4);
            rec1 = new Rectangle("Red", 4, 3);
            ra1  = new RightAngleTriangle("White", 3, 4);
            eq1  = new EquilateralTriangle("black", 6);

            sq2  = new Square("Blue", 6);
            rec2 = new Rectangle("Red", 5, 4);
            ra2  = new RightAngleTriangle("White", 7, 4);
            eq2  = new EquilateralTriangle("black", 8);
        }
コード例 #3
0
        public void RightAngleTriangleTests(string color, float s1, float s2, float expArea, float expPer)
        {
            ra1 = new RightAngleTriangle(color, s1, s2);

            //float area = 6;
            //float perimeter = 12;

            Assert.AreEqual(Math.Round(expArea, 2), Math.Round(ra1.GetArea(), 2));
            Assert.AreEqual(Math.Round(expPer, 2), Math.Round(ra1.GetPerimeter(), 2));
            //area = 14;
            //perimeter = 19.06f;

            //Assert.AreEqual(Math.Round(area, 2), Math.Round(ra2.GetArea(), 2));
            //Assert.AreEqual(Math.Round(perimeter, 2), Math.Round(ra2.GetPerimeter(), 2));
        }
コード例 #4
0
        static void Main(string[] args)
        {
            bool running = true;

            while (running)
            {
                Console.Clear();

                Console.WriteLine("Make a Shape!");
                Console.WriteLine("1. Square");
                Console.WriteLine("2. Rectangle");
                Console.WriteLine("3. Equilateral Triangle");
                Console.WriteLine("4. Right Angle Triangle");
                Console.WriteLine("e. Exit");

                switch (Console.ReadLine())
                {
                case "1":
                {
                    Console.Clear();
                    Console.WriteLine("You are making a square");
                    Console.WriteLine("What colour is it?");
                    string colour = Console.ReadLine();
                    Console.Clear();
                    Console.WriteLine($"You are making a {colour} square");
                    Console.WriteLine("What is its side length?");
                    int    side   = GetInt();
                    Square square = new Square(colour, side);
                    Console.Clear();
                    Console.WriteLine($"You created a {colour} square with side length: {side}, an area of {square.GetArea()} and a perimeter of {square.GetPerimeter()}");
                    Console.ReadKey();
                }
                break;

                case "2":
                {
                    Console.Clear();
                    Console.WriteLine("You are making a rectangle");
                    Console.WriteLine("What colour is it?");
                    string colour = Console.ReadLine();
                    Console.Clear();
                    Console.WriteLine($"You are making a {colour} rectangle");
                    Console.WriteLine("How wide is it?");
                    int width = GetInt();
                    Console.Clear();
                    Console.WriteLine($"You are making a {colour} rectangle with width: {width}");
                    Console.WriteLine("How tall is it?");
                    int       height    = GetInt();
                    Rectangle rectangle = new Rectangle(colour, width, height);
                    Console.Clear();
                    Console.WriteLine($"You created a {colour} rectangle with width: {width}, height: {height}, an area of {rectangle.GetArea()} and a perimeter of {rectangle.GetPerimeter()}");
                    Console.ReadKey();
                }
                break;

                case "3":
                {
                    Console.Clear();
                    Console.WriteLine("You are making an equilateral triangle");
                    Console.WriteLine("What colour is it?");
                    string colour = Console.ReadLine();
                    Console.Clear();
                    Console.WriteLine($"You are making a {colour} equilateral triangle");
                    Console.WriteLine("What is its side length?");
                    float side = GetFloat();
                    EquilateralTriangle equilateralTriangle = new EquilateralTriangle(colour, side);
                    Console.Clear();
                    Console.WriteLine($"You created a {colour} equilateral triangle with side length: {side}, an area of {equilateralTriangle.GetArea()} and a perimeter of {equilateralTriangle.GetPerimeter()}");
                    Console.ReadKey();
                }
                break;

                case "4":
                {
                    Console.Clear();
                    Console.WriteLine("You are making a right angle triangle");
                    Console.WriteLine("What colour is it?");
                    string colour = Console.ReadLine();
                    Console.Clear();
                    Console.WriteLine($"You are making a {colour} right angle triangle");
                    Console.WriteLine("How wide is it?");
                    float width = GetFloat();
                    Console.Clear();
                    Console.WriteLine($"You are making a {colour} right angle triangle with base: {width}");
                    Console.WriteLine("How tall is it?");
                    float height = GetFloat();
                    RightAngleTriangle rightAngleTriangle = new RightAngleTriangle(colour, width, height);
                    Console.Clear();
                    Console.WriteLine($"You created a {colour} right angle triangle with base: {width}, height: {height}, an area of {rightAngleTriangle.GetArea()} and a perimeter of {rightAngleTriangle.GetPerimeter()}");
                    Console.ReadKey();
                }
                break;

                case "e":
                {
                    running = false;
                }
                break;

                default:
                    continue;
                }
            }
        }
コード例 #5
0
        private void bCalculate_Click(object sender, EventArgs e)
        {
            if (cbChoose.Text == "Circle")
            {
                lbAnswer.Items.Clear();
                _radius = Convert.ToInt32(tbRadius.Text);
                Circle circle = new Circle()
                {
                    Radius = _radius
                };
                lbAnswer.Items.Add("Circle Area with radius " + _radius + " is: " + circle.CalculateArea());
                lbAnswer.Items.Add("Circle circumference with radius " + _radius + " is: " + circle.CalculateCircumference());
            }

            else if (cbChoose.Text == "Square")
            {
                lbAnswer.Items.Clear();
                _side = Convert.ToInt32(tbSide.Text);
                Square square = new Square()
                {
                    Side = _side
                };
                lbAnswer.Items.Add("Square Area with radius " + _side + " is: " + square.CalculateArea());
                lbAnswer.Items.Add("Square circumference with radius " + _side + " is: " + square.CalculateCircumference());
            }

            else if (cbChoose.Text == "Rectangle")
            {
                lbAnswer.Items.Clear();
                _height = Convert.ToInt32(tbHeight.Text);
                _width  = Convert.ToInt32(tbWidth.Text);
                RectangleShape rectangleShape = new RectangleShape()
                {
                    Height = _height, Width = _width
                };
                lbAnswer.Items.Add("Rectangle Area with radius is: " + rectangleShape.CalculateArea());
                lbAnswer.Items.Add("Rectangle circumference with radius is: " + rectangleShape.CalculateCircumference());
            }

            else if (cbChoose.Text == "Right Angle Triangle")
            {
                lbAnswer.Items.Clear();
                _height = Convert.ToInt32(tbHeight.Text);
                _length = Convert.ToInt32(tbLength.Text);
                RightAngleTriangle rightAngleTriangle = new RightAngleTriangle()
                {
                    Height = _height, Length = _length
                };
                lbAnswer.Items.Add("Right Angle Triangle area with radius is: " + rightAngleTriangle.CalculateArea());
                lbAnswer.Items.Add("Right Angle Triangle circumference with radius is: " + rightAngleTriangle.CalculateCircumference());
            }
            else
            {
                lbAnswer.Items.Clear();
                _length = Convert.ToInt32(tbLength.Text);
                TriangleShape triangleShape = new TriangleShape()
                {
                    Length = _length
                };
                lbAnswer.Items.Add("Triangle area with radius is: " + triangleShape.CalculateArea());
                lbAnswer.Items.Add("Triangle circumference with radius is: " + triangleShape.CalculateCircumference());
            }
        }
コード例 #6
0
        static void Main(string[] args)
        {
            List <Shape> Shapes = new List <Shape>();

            string choice = "";

            while (choice.ToLower() != "e")
            {
                Console.WriteLine("---------------------------------");
                Console.WriteLine("What would you like to create?");
                Console.WriteLine("a. Square");
                Console.WriteLine("b. Rectangle");
                Console.WriteLine("c. Right Angle Triangle");
                Console.WriteLine("d. Equilateral Triangle");
                Console.WriteLine("e. Exit");
                Console.WriteLine();
                Console.Write("Selection: ");

                choice = Console.ReadLine();

                try {
                    Console.WriteLine();
                    if (choice.ToLower() == "a")
                    {
                        Console.WriteLine("---------------------------------");
                        Console.Write("Colour: ");
                        string colour = Console.ReadLine().ToLower();
                        Console.Write("Side Length: ");
                        int side = int.Parse(Console.ReadLine());
                        if (side < 1)
                        {
                            throw new LengthBelowOne("Please input a length greater that one");
                        }

                        Square newSquare = new Square(colour, side);
                        Shapes.Add(newSquare);
                        Console.WriteLine();
                        Console.WriteLine("Shape created");
                        Console.WriteLine();
                    }
                    else if (choice.ToLower() == "b")
                    {
                        Console.WriteLine("---------------------------------");
                        Console.Write("Colour: ");
                        string colour = Console.ReadLine().ToLower();
                        Console.WriteLine("Side 1 Length: ");
                        int side1 = int.Parse(Console.ReadLine());
                        if (side1 < 1)
                        {
                            throw new LengthBelowOne("Please input a length greater that one");
                        }
                        Console.WriteLine("Side 2 Length: ");
                        int side2 = int.Parse(Console.ReadLine());
                        if (side2 < 1)
                        {
                            throw new LengthBelowOne("Please input a length greater that one");
                        }

                        Rectangle newRectangle = new Rectangle(colour, side1, side2);
                        Shapes.Add(newRectangle);
                        Console.WriteLine();
                        Console.WriteLine("Shape created");
                        Console.WriteLine();
                    }
                    else if (choice.ToLower() == "c")
                    {
                        Console.WriteLine("---------------------------------");
                        Console.Write("Colour: ");
                        string colour = Console.ReadLine().ToLower();
                        Console.Write("Side 1 Length: ");
                        string side1Input = Console.ReadLine();
                        if (side1Input.Contains("."))
                        {
                            throw new NoDecimal("Please input a whole number");
                        }
                        float side1 = float.Parse(side1Input);
                        if (side1 < 1)
                        {
                            throw new LengthBelowOne("Please input a length greater that one");
                        }
                        Console.Write("Side 2 Length: ");
                        string side2Input = Console.ReadLine();
                        if (side2Input.Contains("."))
                        {
                            throw new NoDecimal("Please input a whole number");
                        }
                        float side2 = float.Parse(side2Input);
                        if (side2 < 1)
                        {
                            throw new LengthBelowOne("Please input a length greater that one");
                        }

                        RightAngleTriangle newRightTriangle = new RightAngleTriangle(colour, side1, side2);
                        Shapes.Add(newRightTriangle);
                        Console.WriteLine();
                        Console.WriteLine("Shape created");
                        Console.WriteLine();
                    }
                    else if (choice.ToLower() == "d")
                    {
                        Console.WriteLine("---------------------------------");
                        Console.Write("Colour: ");
                        string colour = Console.ReadLine().ToLower();
                        Console.Write("Side Length: ");
                        string sideInput = Console.ReadLine();
                        if (sideInput.Contains("."))
                        {
                            throw new NoDecimal("Please input whole number");
                        }
                        int side = int.Parse(sideInput);
                        if (side < 1)
                        {
                            throw new LengthBelowOne("Please input a length greater than one");
                        }

                        EquilateralTriangle newETriangle = new EquilateralTriangle(colour, side);
                        Shapes.Add(newETriangle);
                        Console.WriteLine();
                        Console.WriteLine("Shape created");
                        Console.WriteLine();
                    }
                    else
                    {
                        throw new InvalidChoice("Please choose a valid option");
                    }
                }
                catch (FormatException e) {
                    Console.WriteLine("Error: " + e.Message);
                }
                catch (InvalidChoice e) {
                    Console.WriteLine("Error: " + e.Message);
                }
                catch (LengthBelowOne e) {
                    Console.WriteLine("Error: " + e.Message);
                }
                catch (NoDecimal e) {
                    Console.WriteLine("Error: " + e.Message);
                }
                finally {
                    Console.WriteLine();
                }
            }
        }
コード例 #7
0
        static void Main(string[] args)
        {
            Square              sq1;
            Rectangle           rec1;
            RightAngleTriangle  ra1;
            EquilateralTriangle eq1;
            Input t = new Input();

            Console.WriteLine("Hello World!");

            Console.WriteLine("A: Square, B: Rectangle, C: Equilateral, D: Right-Angled");

            string input = Console.ReadLine();
            //int s1;
            //int s2;
            int count = 0;
            //float t1;
            //float t2;
            //float t3;
            string tests;

            while (count == 0)
            {
                if (input == "A")
                {
                    Console.WriteLine("Please enter required values");
                    Console.WriteLine("Please enter a colour");
                    tests = Console.ReadLine();
                    Console.WriteLine("Side 1: ");
                    try
                    {
                        t.s1 = int.Parse(Console.ReadLine());
                    }
                    catch (Exception ex)
                    {
                        if (ex is FormatException || ex is InvalidInput)
                        {
                            Console.WriteLine("Test: " + ex.Message);
                        }
                    }

                    sq1 = new Square(tests, t.s1);

                    Console.WriteLine(sq1.GetArea());
                    Console.WriteLine(sq1.GetPerimeter());
                }
                else if (input == "B")
                {
                    Console.WriteLine("Please enter required values");
                    Console.WriteLine("Please enter a colour");
                    tests = Console.ReadLine();
                    try
                    {
                        Console.WriteLine("Side 1: ");
                        t.s1 = int.Parse(Console.ReadLine());
                        Console.WriteLine("Side 2: ");
                        t.s2 = int.Parse(Console.ReadLine());
                    }
                    catch (Exception ex)
                    {
                        if (ex is FormatException || ex is InvalidInput)
                        {
                            Console.WriteLine("Test: " + ex.Message);
                        }
                    }

                    rec1 = new Rectangle(tests, t.s1, t.s2);

                    Console.WriteLine(rec1.GetArea());
                    Console.WriteLine(rec1.GetPerimeter());
                }
                else if (input == "C")
                {
                    Console.WriteLine("Please enter required values");
                    Console.WriteLine("Please enter a colour");
                    tests = Console.ReadLine();
                    Console.WriteLine("Side 1: ");
                    try
                    {
                        t.t1 = float.Parse(Console.ReadLine());
                    }
                    catch (Exception ex)
                    {
                        if (ex is FormatException || ex is InvalidInput)
                        {
                            Console.WriteLine("Test: " + ex.Message);
                        }
                    }

                    eq1 = new EquilateralTriangle(tests, t.t1);

                    Console.WriteLine(eq1.GetArea());
                }
                else if (input == "D")
                {
                    Console.WriteLine("Please enter required values");
                    Console.WriteLine("Please enter a colour");
                    tests = Console.ReadLine();
                    try
                    {
                        Console.WriteLine("Side 1: ");
                        t.t1 = float.Parse(Console.ReadLine());
                        Console.WriteLine("Side 2: ");
                        t.t2 = float.Parse(Console.ReadLine());
                    }
                    catch (Exception ex)
                    {
                        if (ex is FormatException || ex is InvalidInput)
                        {
                            Console.WriteLine("Test: " + ex.Message);
                        }
                    }

                    ra1 = new RightAngleTriangle(tests, t.t1, t.t2);

                    Console.WriteLine(ra1.GetArea());
                    Console.WriteLine(ra1.GetPerimeter());
                }
                else
                {
                    Console.WriteLine("Invalid option, press any key to exit.");
                    Console.ReadKey(true);
                }
            }


            Console.WriteLine("test");
            Console.ReadKey(true);
        }