Arraylist Of Points - C# Programming Exercise

In this exercise, you need to create a structure named "Point3D" to represent a point in 3D space with X, Y, and Z coordinates. The structure should allow storing and manipulating the positions of points in 3D.

The program should have an interactive menu where the user can:

• Enter data for a point, specifying its X, Y, and Z coordinates.
• Display all the points entered so far.
• Exit the program.

It is important that instead of using arrays, the program should use the ArrayList class to store the points, providing greater flexibility for adding and managing the points without the need to define a fixed size for the container.

This exercise will help you practice the use of custom structures and collections in C#, such as ArrayList, and managing user interaction through simple menus.

 Category

Dynamic Memory Management

 Exercise

Arraylist Of Points

 Objective

Create a structure named "Point3D" to represent a point in 3D space with coordinates X, Y, and Z.

Create a program that has a menu where the user can:

Add data for one point
Display all the entered points
Exit the program
The program should use ArrayLists instead of arrays.

 Write Your C# Exercise

// Import necessary namespaces for using ArrayList and other basic operations
using System;  // Basic namespace for console input/output and fundamental operations
using System.Collections;  // For using ArrayList

// Define the structure for representing a point in 3D space
struct Point3D
{
    // Properties to store the X, Y, and Z coordinates of the point
    public double X;  // X-coordinate
    public double Y;  // Y-coordinate
    public double Z;  // Z-coordinate

    // Constructor to initialize the coordinates of the point
    public Point3D(double x, double y, double z)
    {
        X = x;  // Set X-coordinate
        Y = y;  // Set Y-coordinate
        Z = z;  // Set Z-coordinate
    }

    // Method to display the point in a readable format
    public void DisplayPoint()
    {
        // Display the point's coordinates as (X, Y, Z)
        Console.WriteLine($"Point: ({X}, {Y}, {Z})");
    }
}

class Program
{
    // Main method to execute the program
    static void Main(string[] args)
    {
        // Create an ArrayList to store points
        ArrayList points = new ArrayList();  // ArrayList to hold Point3D objects
        string choice;  // Variable to store user's menu choice

        // Display the program menu
        do
        {
            // Display menu options to the user
            Console.Clear();  // Clear the console for a fresh display
            Console.WriteLine("3D Point Manager");
            Console.WriteLine("1. Add a point");
            Console.WriteLine("2. Display all points");
            Console.WriteLine("3. Exit");
            Console.Write("Enter your choice: ");
            choice = Console.ReadLine();  // Get the user's menu choice

            // Perform actions based on the user's choice
            switch (choice)
            {
                case "1":
                    // Add a new point
                    Console.Write("Enter X coordinate: ");
                    double x = Convert.ToDouble(Console.ReadLine());  // Read X-coordinate
                    Console.Write("Enter Y coordinate: ");
                    double y = Convert.ToDouble(Console.ReadLine());  // Read Y-coordinate
                    Console.Write("Enter Z coordinate: ");
                    double z = Convert.ToDouble(Console.ReadLine());  // Read Z-coordinate

                    // Create a new Point3D object with the provided coordinates
                    Point3D newPoint = new Point3D(x, y, z);

                    // Add the point to the ArrayList
                    points.Add(newPoint);  // Store the new point in the ArrayList
                    Console.WriteLine("Point added successfully.");
                    break;

                case "2":
                    // Display all entered points
                    Console.WriteLine("Displaying all points:");
                    foreach (Point3D point in points)  // Iterate through each point in the ArrayList
                    {
                        point.DisplayPoint();  // Call DisplayPoint method to show the point
                    }
                    break;

                case "3":
                    // Exit the program
                    Console.WriteLine("Exiting the program...");
                    break;

                default:
                    // Handle invalid input
                    Console.WriteLine("Invalid choice, please try again.");
                    break;
            }

            // Wait for the user to press a key before showing the menu again
            if (choice != "3")
            {
                Console.WriteLine("\nPress any key to continue...");
                Console.ReadKey();
            }

        } while (choice != "3");  // Repeat the menu until the user chooses to exit
    }
}

 Share this C# exercise

 More C# Programming Exercises of Dynamic Memory Management

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

  •  Search in file

    In this exercise, you need to create a program that reads the contents of a text file, saves the content into an ArrayList, and prompts the user to enter sentences to...

  •  Implementing a queue using array

    In this exercise, you need to implement a queue in C#. A queue is a data structure that follows the FIFO (First In, First Out) principle, meaning the first ele...

  •  Implementing a stack using array

    In this exercise, you need to implement a stack in C#. A stack is a data structure that follows the LIFO (Last In, First Out) principle, meaning the last eleme...

  •  Queue Collections

    In this exercise, you need to create a string queue using the Queue class that already exists in the DotNet platform. The Queue class is an implementation of t...

  •  Queue Stack Reverse Polish Notation

    In this exercise, you need to create a program that reads a Reverse Polish Notation (RPN) expression from a text file, such as: 3 4 6 5 - + * 6 + (Result 21). In...

  •  ArrayList

    In this exercise, you need to create a string list using the ArrayList class that already exists in the .NET platform. The ArrayList class allows storing items...