jeudi 1 mai 2014

c# - cercle set et get - Stack Overflow


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;
}
}
}
}



In your constructor you are changing value of your public field not the property so your setter method is never executed.I think you should make your field private, then change your constructor like this:


private double radius;
public Circle(double r)
{
Radius = r;
}

Also you might want to display a message or throw an exception in your setter method when the user try to assign negative value to radius.



Also, how would I introduce a function that allow the user to calculate for more than one circle?



You can use a loop for that.Put your code inside of a loop and declare a counter.


for(int i = 0; i <= 5; i++)
{
/* put your code here, get the input perform the calculation
and display the message */
}

Another way might be usin a while loop:


while(true)
{
/* put your code here */

Console.WriteLine("Would you like to continue ? (y/n)");
var userChoice = Console.ReadKey().KeyChar;
if(char.ToLower(userChoice) != 'y') break;
}


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;
}
}
}
}


In your constructor you are changing value of your public field not the property so your setter method is never executed.I think you should make your field private, then change your constructor like this:


private double radius;
public Circle(double r)
{
Radius = r;
}

Also you might want to display a message or throw an exception in your setter method when the user try to assign negative value to radius.



Also, how would I introduce a function that allow the user to calculate for more than one circle?



You can use a loop for that.Put your code inside of a loop and declare a counter.


for(int i = 0; i <= 5; i++)
{
/* put your code here, get the input perform the calculation
and display the message */
}

Another way might be usin a while loop:


while(true)
{
/* put your code here */

Console.WriteLine("Would you like to continue ? (y/n)");
var userChoice = Console.ReadKey().KeyChar;
if(char.ToLower(userChoice) != 'y') break;
}

0 commentaires:

Enregistrer un commentaire