-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sphere.cs
84 lines (76 loc) · 2.61 KB
/
Sphere.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
namespace DObjectCalculator
{
public class Sphere : CalculatorUI , ICalculations
{
//Private variables for radius of a Sphere object
private double s_radius;
public Sphere(double radius)
{
s_radius = radius;
}
/*
* Creates a Sphere object based on user input
* Also clears console to tidy up
*/
static public Sphere CreateNewSphere()
{
Console.Clear();
Console.WriteLine("Enter Radius: ");
double radius = Convert.ToInt32(Console.ReadLine()); //User input for radius
return new Sphere(radius);
}
/*
* Uses CalculateSurfaceArea declared in interface to return Surface Area of Sphere
* Uses HelperCalculate() method to derive Surface Area formula
*/
public double CalculateSurfaceArea()
{
return (4) * HelperCalculate();
}
/*
* Uses CalculateVolume declared in interface to return Volume of Sphere
* Uses HelperCalculate() method to derive Surface Area formula
*/
public double CalculateVolume()
{
return (4 / 3) * HelperCalculate();
}
/*
* A helper function to be implemented in SurfaceArea and Volume methods
* Calculates slant height
*/
private double HelperCalculate()
{
return ((Math.PI) * (Math.Pow(s_radius, 2))); //Product of pi and radius to power of 2 for surface area and volume
}
/*
* Uses polymorhpism to override GetSurfaceArea() method in CalculatorUI class
* Returns Surface Area as calculated by method CalculateSurfaceArea()
*/
public override double GetSurfaceArea()
{
Console.Clear();
Console.WriteLine("The surface area of the 3D Sphere - ");
return CalculateSurfaceArea();
}
/*
* Uses polymorphism to override GetVolume() method in CalculatorUI class
* Returns Surface Area as calculated by method CalculateVolume()
*/
public override double GetVolume()
{
Console.Clear();
Console.WriteLine("The volume of the 3D Sphere - ");
return CalculateVolume();
}
/*
* Returns radius of current object
* Not used in any implmentation; used if I ever want to extend this program as a future project
*/
public double GetRadius()
{
return s_radius;
}
}
}