Exceptions - basic exercise
I have the following exercise: Define a class named circle that accepts objects of type point, and calculates their distance from the center of the circle. If the point is outside the circle should be sent notices of exception.
This is my code:
class Point
{
protected int x,y;
public Point(int x,int y)
{
this.x = x;
this.y = y;
}
}
class Circle : Point
{
public Circle(Point p,int radius):base(3,5)
{
}
}
class Program
{
static void Main(string[] args)
{
}
}开发者_Python百科
I don't know what I have to do in the circle class, how can I know if the point is in the circle or not? Thanks everyone.
Define a class named circle that accepts objects of type point, and calculates their distance from the center of the circle. If the point is outside the circle should be sent notices of exception.
Inheritance vs. composition: First, it seems wrong that your class Circle
derives from Point
, just because you need an x
and y
coordinate pair for your circle, too. Remember that inheritance usually models "is-a" relationships. But circles are not points. Rather, it could be said that they can be defined through a point (the center) and a radius. Thus it would be more logical to use composition ("has-a" relationship):
class Circle
{
Point center;
double radius;
}
When to use exceptions (and when not to): Second, I hope that whoever gave you the exercise didn't actually mean to throw
an exception just because your distance-calculating method got a Point
that lies outside the circle. I would consider this use of exceptions invalid. Exceptions ought to be used in circumstances where some condition occurs that your code cannot properly deal with. However, calculating a distance between two points can never fail (unless perhaps for weird floating-point issues like overflow or underflow):
double DistanceFromCentreTo(Point p)
{
// See e.g. http://en.wikipedia.org/wiki/Pythagorean_theorem:
return Math.Sqrt((center.x - p.x) * (center.x - p.x) +
(center.y - p.y) * (center.y - p.y));
}
If suddenly such a method threw an exception, many users of your code might consider this unlogical. Why should that method go beyond what its name suggests, and throw an exception just because the distance is greater than radius
?
It would be more advisable IMO to introduce a second method:
bool LiesWithinCircle(Point p)
{
return DistanceFromCentreTo(p) <= radius;
}
Now you have two methods that both do just what most people would expect, don't throw unexpected exceptions, and still offer all the functionality that you'll most likely need.
P.S.: Reading my answer many days later, it suddenly strikes me that it would be better still to define the two methods shown above on the Point
class instead, e.g.:
double DistanceToCenterOf(this Point p, Circle c) { … }
bool LiesWithin (this Point p, Circle c) { … }
... which results in easier-to-understand code; e.g. somePoint.LiesWithin(someCircle)
instead of someCircle.LiesWithinCircle(somePoint)
.
how can I know if the point is in the circle or not?
- Calculate distance between the point and the center.
- If it is greater than the radius, then it's outside.
This will tell you if you are in the circle or not:
class Point
{
protected int x,y;
public Point(int x,int y)
{
this.x = x;
this.y = y;
}
public int X { get { return x; } }
public int Y { get { return y; } }
}
in circle's constructor :
public Circle(Point p,int radius):base(3,5)
{
if ( Math.Sqrt( Math.Pow( p.X - this.X, 2.0) + Math.Pow( p.Y - this.Y, 2.0)) > radius )
throw new ArgumentException("This point is outside the circle");
}
Should be something like this, assuming you ask only about exceptions and already know the logic involved in calculating distance:
class Circle
{
public Circle(Point p,int radius)
{
//code to construct the circle here..
}
public double DistanceFromCenter(Point p)
{
if (p is outside circle)
throw new Exception("Point " + p + " is outside the circle");
//calculate the distance and return it...
}
}
Circle with radius R on the origin is x^2+y^2=R^2
the point is in circle if x^2+y^2<=R^2
精彩评论