Skip to content

Instantly share code, notes, and snippets.

@bobblanchett
Created February 29, 2024 23:17
Show Gist options
  • Save bobblanchett/940d22de02ad3e6982050d4c35f41446 to your computer and use it in GitHub Desktop.
Save bobblanchett/940d22de02ad3e6982050d4c35f41446 to your computer and use it in GitHub Desktop.
Shape Calculator refactored to methods Session4 slide 16
namespace ShapeCalcMethods
{
internal class Program
/*
* Area and Perimeter of Rectangle, Square, Triangle & Circle.
* Bob Blanchett SN100639184 16/2/24
* reusing the code from the previous RSP/Sha exercise, I have added shapes to the switch statement.
* refatored to methods for Session 4 slide 16
*/
{
static void CalcRectangle()
{
Console.WriteLine("Please enter the length of the rectangle:");
double Rlength = double.Parse(Console.ReadLine());
Console.WriteLine("Please enter the width of the rectangle:");
double Rwidth = double.Parse(Console.ReadLine());
double Rarea = Rlength * Rwidth;
double Rperimeter = 2 * (Rlength + Rwidth);
Console.WriteLine($"The area of the rectangle of length {Rlength} is: {Rarea}");
Console.WriteLine($"The perimeter of the rectangle of length {Rlength} and width {Rwidth} is: {Rperimeter}");
}
static void CalcSquare()
{
Console.WriteLine("Please enter the length of the square:");
double Slength = double.Parse(Console.ReadLine());
double Sarea = Slength * Slength;
double Sperimeter = 4 * Slength;
Console.WriteLine($"The area of the square of length {Slength} is: {Sarea}");
Console.WriteLine($"The perimeter of the square of length {Slength} is: {Sperimeter}");
}
static void CalcTriangle()
{
Console.WriteLine("Please enter the base of the Rt Angle triangle:");
double Tbase = double.Parse(Console.ReadLine());
Console.WriteLine("Please enter the height of the Rt Angle triangle:");
double Theight = double.Parse(Console.ReadLine());
double TArea = 0.5 * (Tbase * Theight);
Console.WriteLine($"The area of the Rt Angle triangle of base {Tbase} and height {Theight} is: {TArea}");
Console.WriteLine($"Please enter the length of the first side of the triangle of base length {Tbase}:");
double Tside1 = double.Parse(Console.ReadLine());
Console.WriteLine($"Please enter the length of the second side of the triangle of base length {Tbase}:");
double Tside2 = double.Parse(Console.ReadLine());
double TPerimeter = Tbase + Tside1 + Tside2;
Console.WriteLine($"The perimeter of the triangle of base length {Tbase} and side lengths {Tside1} and {Tside2} is: {TPerimeter}");
}
static void CalcCircle()
{
Console.WriteLine("Please enter the radius of the circle:");
double Cradius = double.Parse(Console.ReadLine());
double Carea = Math.PI * Cradius * Cradius;
double Cperimeter = 2 * Math.PI * Cradius;
Console.WriteLine($"The area of the circle of radius {Cradius} is: {Carea}");
Console.WriteLine($"The perimeter of the circle of radius {Cradius} is: {Cperimeter}");
}
static void Main(string[] args)
{
while (true) // loop to keep the choice alive until exit is chosen.
{
Console.WriteLine("Shape Area and Perimeter Calculator.");
Console.WriteLine("Please enter your choice: rectangle, square, triangle, circle or exit.");
switch (Console.ReadLine())
{
case "rectangle":
CalcRectangle();
break;
case "square":
CalcSquare();
break;
case "triangle":
CalcTriangle();
break;
case "circle":
CalcCircle();
break;
case "exit":
Console.WriteLine("Goodbye!");
return;
default:
Console.WriteLine("Invalid input. Please try again.");
break;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment