Click here to Skip to main content
14,579,188 members
Rate this:
Please Sign up or sign in to vote.
See more:
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 using the following formulas:
Area of a circle = pi * radius2
Area of a rectangle = length * width
Surface area of a cylinder = 2 * pi * radius * height + 2 * pi * radius2
Write a modularized solution that includes class methods for inputting data and performing calculations.


What I have tried:

using System;
using static System.Console;

namespace myShape
{
    class Program
    {
        const double PI = 3.14;
        static void Main(string[] args)
        {
            double  length, width;
            string  inputValue;
            double perimeter, area;
            WriteLine("Please input the length of the rectangel: ");
            inputValue = ReadLine();
            length = double.Parse(inputValue);

            WriteLine("Please input the width of the rectangel: ");
            inputValue = ReadLine();
            width = double.Parse(inputValue);

            perimeter = Perimeter(length, width);
            area      = Area(length, width);


            WriteLine("The perimter of rectangle is {0:F2}", Perimeter(length, width));
            WriteLine("The area of rectangle is {0:F4}", area);



            double radius;
            WriteLine("Please input the radius of the circle: ");
            inputValue = ReadLine();
            radius = double.Parse(inputValue);
            WriteLine("The perimter of circle is {0:F2}", Perimeter(radius));
        }

        public static double Perimeter(double length, double width)
        {
            double perimeter = 0;
            perimeter  = 2 * (length + width);
            return perimeter;
        }

        public static double Perimeter(double radius)
        {
            double perimeter = 0;
            perimeter = 2*PI*radius;
            return perimeter;
        }
    

        public static double Area(double length,double width)
        {
            return length * width;
        }
    }
}
Posted
Updated 30-Jun-20 21:36pm
Comments
Patrice T 30-Jun-20 23:42pm
   
What is the problem with this code ?
t778987 30-Jun-20 23:56pm
   
is it correct?
Rate this:
Please Sign up or sign in to vote.

Solution 1

Quote:
is it correct?

That is part of your task: testing and debugging.
So try it.
Give it each valid menu option, and make sure that works.
Give it invalid menu options, and make sure that it handles them gracefully.
Then for each valid menu option try it with a range of values - valid and invalid, and make sure that valid ones give the right results, and invalid ones are detected.

When it doesn't, debug it to find out why, and fix it. Then start testing again from the beginning.

But ... it will fail the first test, as you forgot some of the specification.
Testing against the spec will show you that.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Let's start with
using System;
using static System.Console;

namespace myShape
{
  interface IShape
  {
    public double getArea();
  }

  class Circle: IShape
  {
    private double r;
    public Circle(double r){this.r = r;}
    public double getArea(){return Math.PI * r * r;}
  }


  class Program
  {
    public static void Main()
    {
      IShape shape;
      do
      {
        shape = null;
        WriteLine("please choose the shape (1.Circle, 2.Rectangle, 3.Cylinder, other values to exit)");
        string sel = ReadLine();

        switch( sel )
        {
        case "1":
          {
            WriteLine("please enter the radius of the circle");
            string s = ReadLine();
            double r = double.Parse(s);
            shape = new Circle(r);
          }
          break;
        case "2":
            WriteLine("t778987 is expected to do this...");
          break;
        case "3":
            WriteLine("t778987 is expected to do this...");
          break;
        default:
          break;
        }

        if ( shape != null )
        {
          double a = shape.getArea();
          WriteLine("The area of the shape is {0}", a);
        }
      } while ( shape != null );
    }
  }
}
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

Quote:
is it correct?

When you run the program, do you get correct result ?
Quote:
Surface area of a cylinder = 2 * pi * radius * height + 2 * pi * radius2

Where is the code for cylinder ?
Quote:
Write a modularized solution that includes class methods for inputting data and performing calculations.

Did you respect this constraint ?
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100