Класс Ball. Вычисление объема и площади сферы - C#
Формулировка задачи:
9
В программе должно быть объявлено не менее 3-х объектов класса с вызовом для них соответствующих методов.
Решение задачи: «Класс Ball. Вычисление объема и площади сферы»
textual
Листинг программы
using System;
class Program
{
class Ball
{
private double r;
public Ball(double R)
{
this.r = R;
}
/// <summary>
/// По умолчанию создается шар с единичным радиусом
/// </summary>
public Ball() : this(1) { }
/// <summary>
/// Площадь поверхности
/// </summary>
public double Area()
{
return 4 * Math.PI * this.r * this.r;
}
/// <summary>
/// Объем
/// </summary>
public double Volume()
{
return 4 * Math.PI * this.r * this.r * this.r / 3;
}
}
static void Main()
{
Ball b1 = new Ball(), b2 = new Ball(3), b3 = new Ball(5);
Console.WriteLine("b1 area: {0}, volume: {1}", b1.Area(), b1.Volume());
Console.WriteLine("b2 area: {0}, volume: {1}", b2.Area(), b2.Volume());
Console.WriteLine("b3 area: {0}, volume: {1}", b3.Area(), b3.Volume());
Console.Read();
}
}