Code Explanation
Code Explanation
csharp
Copy code
using System;
Explanation: This line imports the System namespace, which provides fundamental classes and functions
for performing various operations like input/output (Console operations) and mathematical calculations
(Math functions).
csharp
Copy code
namespace GeometryCalculator
{
Explanation: Here, we define a namespace GeometryCalculator. Namespaces are used to organize code
into logical groups and prevent naming conflicts. In this case, all classes related to geometry calculations
will reside within this namespace.
csharp
Copy code
class Program
{
static void Main(string[] args)
{
Explanation: The Program class contains the Main method, which serves as the entry point of the
application. It's marked as static because it belongs to the type itself (Program) rather than an instance. void
indicates that it doesn't return any value.
Object Instantiation
csharp
Copy code
Cube cube = new Cube();
Rectangle arectangle = new Rectangle();
Cylinder cylinder = new Cylinder();
Explanation: Here, we instantiate objects of the Cube, Rectangle, and Cylinder classes. This allows us to
use these objects to perform calculations specific to each geometric shape.
Reason: Using new creates new instances of these classes, allocating memory and initializing them. Each
instance (cube, arectangle, cylinder) now has its own set of properties and methods, which can be accessed
and manipulated independently.
csharp
Copy code
int choice;
do
{
Console.WriteLine("GEOMETRY CALCULATOR");
Console.WriteLine();
Console.WriteLine("1) VOLUME OF A CUBE");
Console.WriteLine("2) SURFACE AREA OF A RECTANGLE");
Console.WriteLine("3) VOLUME OF A CYLINDER");
Console.WriteLine("4) EXIT");
Console.WriteLine();
Explanation: This block displays a menu to the user, prompting them to choose an option from 1 to 4. It
uses Console.WriteLine() to output text to the console and Console.ReadLine() to read the user's input as a
string. int.Parse() converts this string input into an integer (choice).
csharp
Copy code
switch (choice)
{
case 1:
// Volume of a Cube calculation
break;
case 2:
// Surface Area of a Rectangle calculation
break;
case 3:
// Volume of a Cylinder calculation
break;
case 4:
Console.WriteLine("Exiting...");
break;
default:
Console.WriteLine("Invalid choice. Please enter a number between 1 and 4.");
break;
}
Explanation: The switch statement is used to execute different blocks of code based on the value of
choice. It evaluates choice and performs actions accordingly:
o Case 1: Calculates the volume of a cube.
o Case 2: Calculates the surface area of a rectangle.
o Case 3: Calculates the volume of a cylinder.
o Case 4: Displays a message and exits the program.
o Default: Handles any input that doesn't match cases 1-4 by displaying an error message.
csharp
Copy code
case 1:
Console.Clear();
Console.WriteLine("VOLUME OF A CUBE");
Console.WriteLine();
Console.Write("Enter side length: ");
double cubeSide = double.Parse(Console.ReadLine());
cube.Side = cubeSide;
double cubeVolume = cube.ComputeVolume();
Console.WriteLine("Volume: {0:F2}", cubeVolume);
break;
Explanation:
o Console.Clear() clears the console screen for a clean display.
o Console.WriteLine() statements output messages to the user.
o Console.ReadLine() reads the user's input (side length of the cube) as a string.
o double.Parse() converts the string input to a double (cubeSide).
o cube.Side = cubeSide; assigns cubeSide to the Side property of the cube object.
o cube.ComputeVolume() calculates the volume of the cube using the formula side3\
text{side}^3side3.
o Console.WriteLine("Volume: {0:F2}", cubeVolume); outputs the calculated volume formatted to
two decimal places ({0:F2}).
csharp
Copy code
case 2:
Console.Clear();
Console.WriteLine("SURFACE AREA OF A RECTANGLE");
Console.WriteLine();
Console.Write("Enter length: ");
double rectLength = double.Parse(Console.ReadLine());
Console.Write("Enter width: ");
double rectWidth = double.Parse(Console.ReadLine());
Console.Write("Enter height: ");
double rectHeight = double.Parse(Console.ReadLine());
arectangle.Length = rectLength;
arectangle.Width = rectWidth;
arectangle.Height = rectHeight;
double rectArea = arectangle.ComputeArea();
Console.WriteLine("Surface Area: {0:F2}", rectArea);
break;
Explanation:
o Similar structure as Case 1, with specific prompts and input readings for the dimensions (length,
width, height) of the rectangle.
o Assigns these values to properties of the arectangle object (Length, Width, Height).
o Calculates the surface area using the formula 2(lw+lh+wh)2(lw + lh + wh)2(lw+lh+wh) in
arectangle.ComputeArea().
o Outputs the calculated surface area formatted to two decimal places.
csharp
Copy code
case 3:
Console.Clear();
Console.WriteLine("VOLUME OF A CYLINDER");
Console.WriteLine();
Console.Write("Enter radius: ");
double cylRadius = double.Parse(Console.ReadLine());
Console.Write("Enter height: ");
double cylHeight = double.Parse(Console.ReadLine());
cylinder.Radius = cylRadius;
cylinder.Height = cylHeight;
double cylVolume = cylinder.ComputeVolume();
Console.WriteLine("Volume: {0:F2}", cylVolume);
break;
Explanation:
o Clears the console and prompts the user to input the radius and height of the cylinder.
o Reads these inputs as strings, converts them to doubles (cylRadius, cylHeight).
o Assigns these values to properties of the cylinder object (Radius, Height).
o Calculates the volume of the cylinder using the formula πr2h\pi r^2 hπr2h in
cylinder.ComputeVolume().
o Outputs the calculated volume formatted to two decimal places.
Case 4: Exit
csharp
Copy code
case 4:
Console.WriteLine("Exiting...");
break;
Explanation:
o Simply displays "Exiting..." to indicate the program is terminating.
o break; exits the switch statement and subsequently the do-while loop because choice is now 4.
csharp
Copy code
} while (choice != 4);
Explanation:
o The do-while loop ensures the program continues to display the menu and perform calculations
until the user chooses to exit (selects option 4).
o After each iteration, it prompts the user to press any key to continue (Console.ReadKey()) and
clears the console for the next menu display (Console.Clear()).
Cube Class
csharp
Copy code
public class Cube
{
private double side;
Explanation:
o Defines the Cube class with a private field side to store the side length.
o Side property allows getting and setting of side, providing controlled access to the private field.
o ComputeVolume() method calculates and returns the volume of the cube using the formula side3\
text{side}^3side3.
Rectangle Class
csharp
Copy code
public class Rectangle
{
private double length;
private double width;
private double height;
Explanation:
o Defines the Rectangle class with private fields for length, width, and height.
o Properties (Length, Width, Height) provide controlled access to these fields.
o ComputeArea() method calculates and returns the surface area of the rectangle using the formula
2(lw+lh+wh)2(lw + lh + wh)2(lw+lh+wh).
Cylinder Class
csharp
Copy code
public class Cylinder
{
private double radius;
private double height;
Explanation:
o Defines the Cylinder class with private fields for radius and height.
o Properties (Radius, Height) provide controlled access to these fields.
o ComputeVolume() method calculates and returns the volume of the cylinder using the formula
πr2h\pi r^2 hπr2h (where Math.PI provides the value of π).