Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6089973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:09:38+00:00 2026-05-23T12:09:38+00:00

I have the following exercise: Define a class named circle that accepts objects of

  • 0

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)
    {

    }
}

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:09:39+00:00Added an answer on May 23, 2026 at 12:09 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following classes. class A { public: void fun(); } class B: public
As a programming exercise, I've written a Ruby snippet that creates a class, instantiates
Have following setup: MainActivity class - extends activity MyLayout class - extends View Prefs
I have following situation: I have loged user, standard authentication with DB table $authAdapter
I have following string String str = replace :) :) with some other string;
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I have following situation. A main table and many other tables linked together with
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following text in a file 23456789 When I tried to replace the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.