Answer to Question #210134 in C# for Faiq

Question #210134

Modify the Point class as follows:

  • add a static LengthPoints() method to calculate the distance between two points. The method should receive instances of type Point as parameters.

In the main() function, demonstrate the call to the static LengthPoints() method.Modify the Point class as follows:

  • add a static LengthPoints() method to calculate the distance between two points. The method should receive instances of type Point as parameters.

In the main() function, demonstrate the call to the static LengthPoints() method.


1
Expert's answer
2021-06-23T17:05:30-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;




namespace Q210134
{


    class Point
    {
        private double x;
        private double y;
        public static int count;


        static Point()
        {
            count = 0;
        }
        public Point()
        {
            x = y = 1.0;
            count++;
        }
        public Point(double x, double y)
        {
            this.x = x;
            this.y = y;
            Point.count++;
        }




        public double GetX() { return x; }
        public double GetY() { return y; }
        public void SetX(double x) { this.x = x; }
        public void Set(double y) { this.y = y; }


        /// <summary>
        /// static LengthPoints() method to calculate the distance between two points. The method should receive instances of type Point as parameters.
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        public static double LengthPoints(Point p1, Point p2)
        {
            return Math.Sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
        }
        public void Print()
        {
            Console.WriteLine("Fields of instance: ");
            Console.WriteLine("x = {0}, y = {1}", x, y);
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-us");


            Point pt1 = new Point();
            Point pt2 = new Point(2, 3.5);




            Console.Write("Object pt1. ");
            pt1.Print();
            Console.Write("Object pt2. ");
            pt2.Print();




            Console.WriteLine("Static field Point.count = {0}", Point.count);


            Point[] ptArray = new Point[5];
            Console.WriteLine("Static field Point.count = {0}", Point.count);




            for (int i = 0; i < ptArray.Length; i++)
            {


                ptArray[i] = new Point(i, i * i);
            }


            Console.WriteLine("Static field Point.count = {0}", Point.count);
            Console.WriteLine("Length between pt1 and pt2 is: {0:f3}", Point.LengthPoints(pt1, pt2));
            Console.ReadLine();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS