Practice Exercises C# Sharp - Learn to program with performing exercises C# Sharp

Practice Exercises C# Sharp

Learn to program with performing exercises C# Sharp


3D point - Practice Exercises C# Sharp


Lesson 7:

More on classes


Exercise 7.12:

3D point


Objetive:

Create a class "Point3D", to represent a point in 3-D space, with coordinates X, Y and Z. It must contain the following methods:

MoveTo, which will change the coordinates in which the point is.
DistanceTo(Point3D p2), to calculate the distance to another point.
ToString, which will return a string similar to "(2,-7,0)"
And, of course, getters and setters.

The test program must create an array of 5 points, get data for them, and calculate (and display) the distance from the first point to the remaining four ones.


Source Code:


using System;
class Point3D
{
    protected double x, y, z;


    public Point3D()
    {
    }

    public Point3D(double nx, double ny, double nz)
    {
        MoveTo(nx, ny, nz);
    }

    public double GetX()
    {
        return x;
    }

    public void SetX(double value)
    {
        x = value;
    }

    public double GetY()
    {
        return y;
    }

    public void SetY(double value)
    {
        y = value;
    }

    public double GetZ()
    {
        return z;
    }

    public void SetZ(double value)
    {
        z = value;
    }

    public void MoveTo(double nx, double ny, double nz)
    {
        x = nx;
        y = ny;
        z = nz;
    }

    public double DistanceTo(Point3D p2)
    {
        return Math.Sqrt((x - p2.GetX()) * (x - p2.GetX()) +
          (y - p2.GetY()) * (y - p2.GetY()) +
          (z - p2.GetZ()) * (z - p2.GetZ()));
    }
}
Exercisey 7.12




Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.