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; }
}
}
}
Loading
VulpesPosted Jan 31, 2012, 4:54 AM
1. Your formulas for the diameter and area are wrong in the 'setter' for the Radius property.
The diameter is twice the radius rather than the square of the radius.
The area is 'pi' multiplied by the square of the radius rather than 'pi' multiplied by the diameter.
Also there's no need to calculate the area again in the 'getter' for the Area property, just return 'area'.
2. I guess so that you'd know immediately what the class does if you look at it again subsequently. Naming the class containing the Main() method, 'Program', is usually done if you can't think of a better name or in simple examples (such as mine :)).
3. To display the area to 2 decimal places include the F2 format (which means fixed-point 2 places) in the placeholder in Console.WriteLine i.e. instead of just {2} use {2:F2}.
Prime bPosted Feb 1, 2012, 12:19 PM
VulpesPosted Feb 1, 2012, 9:00 AM
This isn't your fault - you're just doing what you were told to do.
Prime bPosted Jan 31, 2012, 7:01 PM
private double radius;
private double area;
private double diameter;
public Circle()
{
radius = 1;
}
public double Radius
{
get { return radius; }
set { radius = value; area = Math.PI * radius * radius; diameter = 2 * radius ; }
}
public double Area
{
get { return area; }
}
public double Diameter
{
get { return diameter; }
}