简体   繁体   中英

C# Triangle type calculation and area calculation

I am trying to solve or be pointed in the right direction. I am having difficulty determining where to place my Area formula in the Triangle Class (not the main). Area can only have a 'get' and not a 'set'.

Next issue is identifying the type of triangle based on the inputed side and if it is a 'right' triangle, appending the 'type' with '-right' for example (isosceles-right). I have an enum for the triangle types.

I'm not looking for the direct answer to solve this but rather some help and coaching to help better build my skills

Here is the class structure I have generated so far in C#, please keep in mind it is not complete.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TriangleCheck;

namespace TriangleCheck
{
    public class Triangle
    {
        private StringBuilder _ErrorMsg;
        private int[] _Sides;
        private const int _nSides = 3;
        private int _Area;

        public Triangle(int[] Sides)
        {
            //Track amunt of errors recieved.
            int nErrors = 0;

            //Make sure ErrorMsg is cleared
            _ErrorMsg = new StringBuilder();

            //Did I get _nSides? If not, append to ErrorMsg and throw exception
            if(Sides.Length != _nSides)
            {
                _ErrorMsg.Append(string.Format("Expected {0} sides but recieved {1}", _nSides, Sides.Length));
                nErrors += 1;
            }

            //Is each side positive? If not, append to ErrorMsg and throw exception
            for (int i = 0; i < Sides.Length; i++)
            {
                if (Sides[i] <= 0)
                {
                    _ErrorMsg.Append(string.Format("{0} side is not a postive integer", Sides[i]));
                    nErrors += 1;
                }
            }

            //Set input from user to private property _Sides
            _Sides = Sides;
            _Area = Area;
        }

        public int Area
        {
            get { return _Area; }
            private set
            {
                int parameter = 
            }
        }


        public string ErrorMsg
        {
            get
            { return ErrorMsg.ToString();}
        }

        public bool IsRight
        {
            get
            {
                return ;
            }
        }

        public int Sides
        {
            get
            { return _Sides; }

            set
            {
                if (value > 0)
                {
                    _Sides = value;
                }
                else
                    throw new ArgumentOutOfRangeException("Value must be postive!");
            }
        }

        public TriangleTypes TriangleTypes
        {
            get
            {
                throw new System.NotImplementedException();
            }

            set
            {
            }
        }

        public void ScaleUp(int[] ScaleFactor)
        {
            throw new System.NotImplementedException();
        }

        public override string ToString()
        {
            return "A Triangle with sides " + _Sides + " is Type: " + TriangleTypes + " with Area:" + Area;
        }
    }
}

You mention that you can't set the Area property... it looks like you're trying to enforce that by making a private set , but why not just exclude the set leaving it as a read-only property?

The Area formula could go a couple places; the key is that it is derived from the sides but only matters when someone asks for it. So you could reasonably:

  • Apply the formula and update internal state every time sides changes
  • Apply the formula and return the value every time someone does a get operation on Area

Remember the point of getter and setter being functions is that they could contain logic to execute (to fully update internal state in setter, or to calculate the value of a read-only derived property).

More sophisticated patterns exist if performance of the area calculation were very worrisome, but I wouldn't get into that at this point.

As for determining if the triangle is right... if it is, which side must be the hypotenuse? What relationship do you know between the length of the hypotenuse and the lengths of the other sides, if the triangle is right?

using System;

namespace ConsoleApp
{
    class Program
    {
        static void Main()
        {
            var t = new Triangle(2, 3, 5);

            //var Triangle = new Triangle(2);          // won't compile as no Triangle constructor can be found that takes 1 integer
            //var Triangle = new Triangle(2, 3, 5, 7); // won't compile as no Triangle constructor can be found that takes 4 integers
            //var Triangle = new Triangle(2, -3, 5);   // won't compile as the 2nd value is negative - and we've asked for unsigned for all 3 values

            Console.WriteLine("The triangle ({0}, {1}, {2}) has an area of {3}.", t.A, t.B, t.C, t.area());

            Console.ReadKey();
        }
    }

    public class Triangle
    {
        public uint A { get; set; }
        public uint B { get; set; }
        public uint C { get; set; }

        public Triangle(uint a, uint b, uint c)
        {
            this.A = a;
            this.B = b;
            this.C = c;
        }

        public uint area()
        {
            return A * B * C; // this needs fixing ...
        }
    }
}

Isn't this roughly what you are trying to achieve with your Triangle class - a way of stopping it being used incorrectly with too few or incorrect types of arguments. This one only allows 3 positive (uint) integers. Nothing else will comple - which is what you want. Sorry if I have misunderstood.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM