C# Exercise: 152 3D point

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#.

 Exercise

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.

 Example Code

// 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 override 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");
        }
    }
}

More C# Exercises of OOP More On Classes

 Array of objects: table
Create a class named "Table". It must have a constructor, indicating the width and height of the board. It will have a method "ShowData" which will wr...
 House
Create a class "House", with an attribute "area", a constructor that sets its value and a method "ShowData" to display "I am a house, my area is 200 m...
 Table + coffetable + array
Create a project named "Tables2", based on the "Tables" project. In it, create a class "CoffeeTable" that inherits from "Table". Its method "ShowDa...
 Encrypter & Decrypter
Create a class "Encrypter" to encrypt and decrypt text. It will have a "Encrypt" method, which will receive a string and return another string. It ...
 Complex numbers
A complex number has two parts: the real part and the imaginary part. In a number such as a+bi (2-3i, for example) the real part would be "a" (2) and ...
 Table + coffetable + leg
Extend the example of the tables and the coffee tables, to add a class "Leg" with a method "ShowData", which will write "I am a leg" and then it will ...
 Catalog
Create the classes diagram and then, using Visual Studio, a project and the corresponding classes for a catalog utility: It will be able to store i...
 Random number
Create a class RandomNumber, with three static methods: - GetFloat will return a number between 0 and 1 using the following algorithm: seed = (s...
 Text to HTML
Create a class "TextToHTML", which must be able to convert several texts entered by the user into a HTML sequence, like this one: Hola Soy yo Ya ...
 Class ScreenText
Create a class ScreenText, to display a certain text in specified screen coordinates. It must have a constructor which will receive X, Y and the strin...
 Enhanced ComplexNumber class
Improve the "ComplexNumber" class, so that it overloads the operators + and - to add and subtract numbers....
 Catalog + Menu
Improve the Catalog program, so that "Main" displays a menu to allow entering new data of any kind, as well as displaying all the data stored....