3D Point - C# Programming Exercise

In this exercise of C#, you need to create a class called Point3D to represent a point in 3-D space, with coordinates X, Y, and Z. The class must include the following methods:

A MoveTo method, which will change the coordinates of the point. A DistanceTo method, which will calculate the distance to another point, receiving a Point3D object as a parameter. The ToString method should return a string in the format "(2,-7,0)", representing the coordinates of the point. Additionally, the class should include getters and setters for the X, Y, and Z coordinates.

The test program must create an array of 5 points, get data for each of them, and then calculate (and display) the distance from the first point to the remaining four. This exercise is great for practicing working with classes, methods, and calculating distances in 3-D space, as well as using arrays in C#.

 Category

OOP More On Classes

 Exercise

3D Point

 Objective

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.

 Write Your C# Exercise

// Importing the System namespace to handle basic functionalities and console output
using System;

// Creating the class to represent a point in 3D space
public class Point3D
{
    // Declaring the coordinates of the point (X, Y, Z) in 3D space
    private double x;
    private double y;
    private double z;

    // Constructor to initialize the point with given coordinates (X, Y, Z)
    public Point3D(double x, double y, double z)
    {
        this.x = x;  // Setting the X coordinate
        this.y = y;  // Setting the Y coordinate
        this.z = z;  // Setting the Z coordinate
    }

    // Getter for the X coordinate
    public double X
    {
        get { return x; }  // Returning the X coordinate value
        set { x = value; } // Setting the X coordinate value
    }

    // Getter for the Y coordinate
    public double Y
    {
        get { return y; }  // Returning the Y coordinate value
        set { y = value; } // Setting the Y coordinate value
    }

    // Getter for the Z coordinate
    public double Z
    {
        get { return z; }  // Returning the Z coordinate value
        set { z = value; } // Setting the Z coordinate value
    }

    // Method to move the point to a new location by changing its coordinates
    public void MoveTo(double newX, double newY, double newZ)
    {
        x = newX;  // Updating the X coordinate to the new value
        y = newY;  // Updating the Y coordinate to the new value
        z = newZ;  // Updating the Z coordinate to the new value
    }

    // Method to calculate the distance from this point to another point
    public double DistanceTo(Point3D p2)
    {
        // Calculating the difference in the X coordinates between this point and the other point
        double dx = x - p2.X;
        // Calculating the difference in the Y coordinates between this point and the other point
        double dy = y - p2.Y;
        // Calculating the difference in the Z coordinates between this point and the other point
        double dz = z - p2.Z;

        // Using the 3D distance formula to calculate the distance between the two points
        return Math.Sqrt(dx * dx + dy * dy + dz * dz); // Returning the calculated distance
    }

    // Method to return a string representation of the point in the format (X, Y, Z)
    public string ToString()
    {
        return $"({x},{y},{z})";  // Returning the string with the X, Y, Z coordinates in parentheses
    }
}

// Auxiliary class containing the Main method to test the functionality of the Point3D class
public class Program
{
    public static void Main()
    {
        // Creating an array of 5 Point3D objects to store the points
        Point3D[] points = new Point3D[5];

        // Loop to gather input for each point from the user
        for (int i = 0; i < 5; i++)
        {
            // Asking the user to input the coordinates for the current point
            Console.WriteLine($"Enter the coordinates for Point {i + 1}:");

            // Asking for the X coordinate and storing the input as a double
            Console.Write("X: ");
            double x = Convert.ToDouble(Console.ReadLine());

            // Asking for the Y coordinate and storing the input as a double
            Console.Write("Y: ");
            double y = Convert.ToDouble(Console.ReadLine());

            // Asking for the Z coordinate and storing the input as a double
            Console.Write("Z: ");
            double z = Convert.ToDouble(Console.ReadLine());

            // Creating a new Point3D object with the entered coordinates and storing it in the array
            points[i] = new Point3D(x, y, z);
        }

        // Storing the first point from the array to calculate distances from it
        Point3D firstPoint = points[0];

        // Displaying a message about the distances
        Console.WriteLine("\nDistances from the first point to the others:");

        // Loop to calculate and display the distance from the first point to the remaining points
        for (int i = 1; i < points.Length; i++)
        {
            // Calling the DistanceTo method to calculate the distance from the first point to the current point
            double distance = firstPoint.DistanceTo(points[i]);

            // Displaying the calculated distance from the first point to the current point
            Console.WriteLine($"Distance from Point 1 to Point {i + 1}: {distance:F2} units");
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of OOP More On Classes

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Catalog + Menu

    In this exercise of C#, you need to improve the catalog program so that the Main method displays a menu allowing the user to enter new data of any kind, as wel...

  •  Array of objects: table

    In this exercise, you need to create a class named "Table". This class should have a constructor that accepts the width and height of the table. ...

  •  House

    In this exercise, you need to create a class called "House" with an attribute called "area". The class should have a constructor to set the value of this attri...

  •  Table + coffetable + array

    In this exercise, you need to create a project named "Tables2", based on the "Tables" project. In this new project, you should create a class called "CoffeeTable" tha...

  •  Encrypter & Decrypter

    In this exercise, you need to create a class called "Encrypter" to encrypt and decrypt text. The class will have an "Encrypt" method, whic...

  •  Complex numbers

    In this exercise of C#, the concept of complex numbers is introduced, which consist of two parts: the real part and the imaginary part. In an expression like a+bi (fo...