Vote count:
0
I have the working code, the set and get are supposed to not allow negative input. However, when this is run, negative input still returns results, what am I missing that is causing the negative values to still be calculated? Also, how would I introduce a function that allow the user to calculate for more than one circle?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace console
{
class Program
{
static void Main(string[] args)
{
Console.Write("What is the circle’s radius: ");
double radius = Convert.ToDouble(Console.ReadLine());
Circle ans = new Circle(radius);
Console.WriteLine("The area of the circle is " + ans.getArea);
Console.WriteLine("The Diameter of the circle is " + ans.getDiameter);
Console.WriteLine("The Circumference of the circle is " + ans.getCircumference);
Console.Write("Enter any character to quit program. ");
double stop = Console.Read();
}
}
}
That's the method and the class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace console
{
class Circle
{
public double radius;
public const double PI = 3.14159;
public Circle(double r)
{
radius = r;
}
public Circle()
{
radius = 0.0;
}
public double getDiameter
{
get
{
return radius * 2;
}
}
public double getArea
{
get
{
return PI * radius * radius;
}
}
public double getCircumference
{
get
{
return 2 * PI * radius;
}
}
// property Radius
public double Radius
{
get
{
return radius;
}
set
{
// ensure non-negative radius value
if (value >= 0)
radius = value;
}
}
}
}
asked 23 secs ago
Aucun commentaire:
Enregistrer un commentaire