3D point Learn programming Visual Basic (VB.net)

Lesson:

OOP More On Classes


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.


Code:

Imports System
Class Point3D
    Protected x, y, z As Double

    Public Sub New()
    End Sub

    Public Sub New(ByVal nx As Double, ByVal ny As Double, ByVal nz As Double)
        MoveTo(nx, ny, nz)
    End Sub

    Public Function GetX() As Double
        Return x
    End Function

    Public Sub SetX(ByVal value As Double)
        x = value
    End Sub

    Public Function GetY() As Double
        Return y
    End Function

    Public Sub SetY(ByVal value As Double)
        y = value
    End Sub

    Public Function GetZ() As Double
        Return z
    End Function

    Public Sub SetZ(ByVal value As Double)
        z = value
    End Sub

    Public Sub MoveTo(ByVal nx As Double, ByVal ny As Double, ByVal nz As Double)
        x = nx
        y = ny
        z = nz
    End Sub

    Public Function DistanceTo(ByVal p2 As Point3D) As Double
        Return Math.Sqrt((x - p2.GetX()) * (x - p2.GetX()) + (y - p2.GetY()) * (y - p2.GetY()) + (z - p2.GetZ()) * (z - p2.GetZ()))
    End Function
End Class

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.