Home > Net >  C# Program can't view the result of triangle area using heron's formula
C# Program can't view the result of triangle area using heron's formula

Time:09-14

I've been trying to solve this problem about my program about printing the final result of Area of Triangle using Heron's Formula with user input

static void Main(string[] args)
    {
            //variable/s declaration 
            double a, b, c, Area;
            //Ask for the values of the three sides of the triangle.
            Console.Write("Enter first side: ");
            a = double.Parse(Console.ReadLine());
            Console.Write("Enter second side: ");
            b = double.Parse(Console.ReadLine());
            Console.Write("Enter third side: ");
            c = double.Parse(Console.ReadLine());
            //instantiate a triangle
            Triangle KD = new Triangle();
            Area =KD.FindingA();
            //display the values of the three sides and the area of the triangle
            Console.WriteLine("First Side = {0:R}", a);
            Console.WriteLine("Second Side = {0:R}", b);
            Console.WriteLine("Third Side = {0:R}", c);
            Console.WriteLine("Area of triangle = {0:R}", Area);
    }
    public class Triangle
    {
        //fields
        private double A, B, C, s, Area;
        public double a, b, c;
        //constructors
        public double GetASide()
        { return this.a; }

        public void SetASide(double value)
        { this.a = value; }

        public double GetBSide()
        { return this.b; }

        public void SetBSide(double value)
        { this.b = value; }

        public double GetCSide()
        { return this.c; }

        public void SetCSide(double value)
        { this.c = value; }

        //create a method that will compute the area of the triangle
        public double FindingA()
        {
            s = (a   b   c)/2;
            Area = Math.Sqrt(s * (s - a) * (s - b) * (s - c));
            return Area;

        }

where for the output should be

First Side = 5.1
Second Side = 5.1
Third Side = 3.2
Area of triangle = 7.7480

But when I input it, it shows

First Side = 5.1
Second Side = 5.1
Third Side = 3.2
Area of triangle = 0

Please help me with this problem guys, I can't think any solution of the problem

CodePudding user response:

You are not creating your triangle correctly, the a inside your triangle class is an entirely different a from the one in your main method. You would need to set all the sides of the triangle after you created it.

I would recommend defining your triangle like

public class Triangle
{
    public double A {get;}
    public double B {get;}
    public double C {get;}
    public Triangle(double a, double b, double c){
        A = a; B = b; C = c;
   }
   public double GetArea(){
     var s = (a   b   c)/2;
     var area = Math.Sqrt(s * (s - a) * (s - b) * (s - c));
     return area;
   }
}
...
var myTriangle = new Triangle(a, b, c);
var area = myTriangle.GetArea();

This does a few things:

  1. It makes it impossible to create an area without the necessary parameters
  2. It makes it impossible for the Area and sides-properties to get out of sync
  3. It removes redundant getter/setter methods and uses auto-properties instead
  4. It uses a method to hint to the user that the calculation can actually take some (very small in this case) amount of time. Properties are generally expected to be very cheap to get.

You can chose if you want to pre-calculate the area in the constructor and store it in another get-only property, or calculate it on demand, each have some advantages. I tend to prefer to calculate it on demand since calculations are quite cheap, and if you store millions of triangles the memory cost can add up.

If you actually want to change the sides of the triangle once created, you could consider using a record instead of a class since that allows for with expressions

var myTriangle = new Triangle(3, 4, 5);
var newTriangle = myTriangle with{ A = 6 }

You could however replicate the functionality for a regular class using regular methods.

CodePudding user response:

You are declaring and assigning your variables in the main part of your program. Like this de variables (the sides a,b and c) in your main program are 5.1, 5.1 and 3.2. But the variables in your object Triangle KD are still empty. The function FindingA calculates the area with sides a,b and c of the object and these are still 0. Hope this is an answer.

CodePudding user response:

This works

using System;
                    
public class Program
{
    public static void Main()
    {
        var a = 5.1D;
        var b = 5.1D;
        var c = 3.2D;
        var s = (a   b   c)/2.0D;
        var area = Math.Sqrt(s * (s - a) * (s - b) * (s - c));
        Console.WriteLine("First Side = {0:R}", a);
        Console.WriteLine("Second Side = {0:R}", b);
        Console.WriteLine("Third Side = {0:R}", c);
        Console.WriteLine("Area of triangle = {0:R}", area);
    }
}
  • Related