ArrayList of Points C# Exercise - C# Programming Course

 Exercise

ArrayList of Points

 Objetive

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.

 Example Code

// 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
    }
}

More C# Exercises of Dynamic Memory Management

 Implementing a queue using array
Implementing a queue...
 Implementing a stack using array
Implementing a stack...
 Queue Collections
Create a string queue using the Queue class that already exists in the DotNet platform....
 Queue Stack Reverse Polish Notation
Create a program that reads a Reverse Polish Notation expression from a text file, for example: 3 4 6 5 - + * 6 + (Result 21) Each item will be...
 ArrayList
Create a string list using the ArrayList class that already exists in the .NET platform. Once created, display all the items stored in the list. In...
 ArrayList duplicate a text file
Create a program that reads from a text file and stores it to another text file by reversing the order of lines. For example, an input text file li...
 Unlimited sum
Create a program to allow the user to enter an unlimited amount of numbers. Also, they can enter the following commands: "sum", to display the sum of...
 ArrayList - Text file reader
provide your basic text file reader here, which displays 21 lines of text and allows the user to navigate using the up and down arrow keys, and exit u...
 Hast Table - Dictionary
Submit your dictionary here using a hash table....
 Parenthesis
Implement a function to check if a sequence of open and closed parentheses is balanced. In other words, check if each open parenthesis corresponds to ...
 Mix and sort files
Create a program that reads the contents of two different files, merges them, and sorts them alphabetically. For example, if the files contain: "Dog C...
 Search in file
Create a program that reads a text file, saves its content to an ArrayList, and asks the user to enter sentences to search within the file. The pro...

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