Prime b

Prime b

  • NA
  • 810
  • 345.4k

Another problem with Classes

Jan 30 2012 9:09 PM
Create a class named Circle with fi elds named radius, area,
and diameter. Include a constructor that sets the radius to
1. Also include public properties for each fi eld. Th e Radius
property should have get and set accessors, but Area and
Diameter should be read-only. Th e set accessor for the radius
should also provide values for the diameter and area. (Th e
diameter of a circle is twice its radius; the area is pi multiplied
by the square of the radius. You can use the public Math class
property Math.PI for the value of pi.) Create a class named
TestCircles whose Main() method declares three Circle
objects. Assign a small radius value to one Circle and assign
a larger radius value to another Circle. Do not assign a value
to the radius of the third circle; instead, retain the value
assigned at construction. Display the radius, diameter, and
area for each Circle. (Display the area to two decimal places.)
Save the program as TestCircles.cs.


I just want you to check and see how I did, and also answer my questions, don't actually give me an answer, thank you!

My questions are:
1. Why my third circle doesn't display diameter and area?( I am pretty sure my Circle class is a little wrong, that's why it doesn't display), but give me hint , don't give me actual  solution =), you spoiled me enough Vulpes!
2. Why did they ask to create class TestCircles? Just to be difficult? I could have just used the Program class?
3. How do I display area of a circle to two decimal places?
4.

namespace TestCircles
{
    class TestCircles
    {
        static void Main(string[] args)
        {
            Circle firstCircle = new Circle();
            Circle secondCircle = new Circle();
            Circle thirdCircle = new Circle();

            firstCircle.Radius = 2;
            secondCircle.Radius = 4;


            Console.WriteLine("The radius {0}, diameter {1}, and area {2} of the first circle ", firstCircle.Radius, firstCircle.Diameter, firstCircle.Area);
            Console.WriteLine("The radius {0}, diameter {1}, and area {2} of the second circle ", secondCircle.Radius, secondCircle.Diameter, secondCircle.Area);
            Console.WriteLine("The radius {0}, diameter {1}, and area {2} of the third circle ", thirdCircle.Radius, thirdCircle.Diameter, thirdCircle.Area);
        }
    }
}



namespace TestCircles
{
    class Circle
    {
        private double radius;
        private double area;
        private double diameter;

        public Circle()
        {
            radius = 12;
        }
        public double Radius
        {
            get { return radius; }
            set { radius = value; area = Math.PI * diameter; diameter = radius * radius ; }
       
        }
        public double Area
        {
            get { return area = Math.PI * diameter; }
        }
        public double Diameter
        {
            get { return diameter; }
        }
    }
}





Answers (4)