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
C# Exercise Example
// 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
}
}