So I am practicing c# and I recently studied classes and methods. I wanted to practice what I studied, so I decided I write a small program that allows the user to enter a set of coordinates that would represent a couple of circle centers. The user would also enter the radius. And then the computer would determine if the circles overlap by comparing the distance between the centers and lengths of both radius' combined. But no matter what I enter, the program always states that the total length of the radius' is 0 and that the circles are overlapping. MS Visual Studio has not shown any errors in code. This is my code. Where did I go wrong?
namespace practice
{
class Circle//This defines exactly what a circle is
{
public int x, y;//these are the coordinates on a graph where the center of the circle exists
public double radius;
public double Area()//method to determining a circles area
{
double theArea = 3.14159 * radius * radius;
return theArea;
}
public double Circumference()//method to determining a circle's circumference
{
double theCircum = 3.14159 * radius * 2;
return theCircum;
}
}
class Line
{
public double asqrd, bsqrd;//asqrd and bsqrd will be the difference in the x and y coordinates of each circle
public double Length()//method determines the length of the line drawn between the circles centers
{
double theLength = Math.Sqrt(asqrd + bsqrd);//a^2 + b^2 = c^2. theLength will be the square root of c^2.
return theLength;//the length will represent the distance between the 2 circle centers
}
}
class CircleApp
{
static void Main()
{
Circle Circle1 = new Circle();
Circle Circle2 = new Circle();
Line theLine = new Line();
//the user determines where the circles centers are located
Console.Write("Enter circle 1 center x coordinate: ");
Circle1.x = int.Parse(Console.ReadLine());
Console.Write("Enter circle 1 center y coordinate: ");
Circle1.y = int.Parse(Console.ReadLine());
Console.Write("Enter circle 2 center x coordinate: ");
Circle2.x = int.Parse(Console.ReadLine());
Console.Write("Enter circle 2 center y coordinate: ");
Circle2.y = int.Parse(Console.ReadLine());
theLine.asqrd = ((Circle1.x - Circle2.x) * (Circle1.x - Circle2.x));
theLine.bsqrd = ((Circle1.y - Circle2.y) * (Circle1.y - Circle2.y));
Console.Write("What is circle 1 radius? ");
Circle1.radius = double.Parse(Console.ReadLine());
Console.Write("What is circle 2 radius? ");
Circle2.radius = double.Parse(Console.ReadLine());
//this will add up the lengths of both radi
double totalRadius = Circle1.radius + Circle2.radius;
Console.WriteLine("The area of circle 1 is : {0}", Circle1.Area());
Console.WriteLine("The Circumference of circle 1 is : {0}", Circle1.Circumference());
Console.WriteLine("The area of circle 2 is : {0}", Circle2.Area());
Console.WriteLine("The Circumference of circle 2 is : {0}", Circle2.Circumference());
Console.WriteLine("The distance between the 2 circles centers is {0}", theLine.Length());
Console.WriteLine("The total radius is : {0}", totalRadius);
//if the totalRadius is greater or equal to the distance between the centers, then the circles will overlap.
if (theLine.Length() >= totalRadius)
{
Console.WriteLine("These 2 circles are overlapping.");
}
else
{
Console.WriteLine("These 2 circles do not overlapp.");
}
Console.ReadLine();
}
}
}
Aucun commentaire:
Enregistrer un commentaire