Answer to Question #253031 in C# for Israel

Question #253031

Write an application that computes the area of a circle, rectangle, and cylinder. Display a menu showing the three options. Allow users to input which figure they want to see calculated. Based on the value inputted, prompt for appropriate dimensions and perform the calculations and perform the calculations using the following formulas:

Area of a circle = pi*radius2

Area of a rectangle = length*width

Area of a cylinder = pi*radius2*height

Write a modularized solution, which includes class methods for inputting data and performing calculations.



1
Expert's answer
2021-10-18T10:53:24-0400
using System;


namespace Question
{
    abstract class Figure
    {
        public abstract double Area();
    }


    class Circle : Figure
    {
        public double Radius { get; set; }
        public override double Area()
        {
            return Math.PI * Radius * Radius;
        }
    }
    class Rectangle : Figure
    {
        public double Length { get; set; }
        public double Width { get; set; }


        public override double Area()
        {
            return Length * Width;
        }
    }
    class Cylinder : Figure
    {
        public double Radius { get; set; }
        public double Height { get; set; }


        public override double Area()
        {
            return Math.PI * Radius * Radius * Height;
        }
    }
    class Program
    {
        static void DisplayMenu()
        {
            Console.WriteLine("Please select a figure");
            Console.WriteLine("1 - Circle");
            Console.WriteLine("2 - Rectangle");
            Console.WriteLine("3 - Cylinder");
            Console.WriteLine();
        }
        static int SelectFigure()
        {
            return int.Parse(Console.ReadLine());
        }
        static Figure GetFigure(int selectedFigure)
        {
            if (selectedFigure == 1)
                return new Circle();
            if (selectedFigure == 2)
                return new Rectangle();
            return new Cylinder();
        }
        static void InputCircleData(Circle circle)
        {
            Console.Write("radius = ");
            circle.Radius = double.Parse(Console.ReadLine());
        }
        static void InputRectangleData(Rectangle rectangle)
        {
            Console.Write("length = ");
            rectangle.Length = double.Parse(Console.ReadLine());


            Console.Write("width = ");
            rectangle.Width = double.Parse(Console.ReadLine());
        }
        static void InputCylinderData(Cylinder cylinder)
        {
            Console.Write("radius = ");
            cylinder.Radius = double.Parse(Console.ReadLine());


            Console.Write("height = ");
            cylinder.Height = double.Parse(Console.ReadLine());
        }
        static void InputFigureData(Figure figure)
        {
            if (figure is Circle)
                InputCircleData((Circle)figure);
            else if (figure is Rectangle)
                InputRectangleData((Rectangle)figure);
            else
                InputCylinderData((Cylinder)figure);
        }
        static void Main(string[] args)
        {
            DisplayMenu();
            Console.Write("Your choice: ");
            int selectedFigure = SelectFigure();


            if (selectedFigure >= 1 && selectedFigure <= 3)
            {
                Figure figure = GetFigure(selectedFigure);
                InputFigureData(figure);
                Console.WriteLine();


                Console.WriteLine(string.Format("Area = {0}", figure.Area()));
            }
            else
                Console.WriteLine("Invalid choice");




            Console.WriteLine();


            
            Console.WriteLine();
            Console.ReadKey();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS