Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. # Here is the code:
  2.  
  3. ```c#
  4. namespace BKTest
  5. {
  6. public class Pie
  7. {
  8. /// <summary>
  9. /// Estimates pi based on the number of fractions we desire it to estimate by.
  10. /// The way I view it: you basically have 4 * (element0 + element1 + element2 etc.)
  11. /// where element0, element1 etc are instances of the Element class.
  12. /// I use a factory method to instantiate the element types and use polymorphism to differentiate
  13. /// between the two different types of elements that are currently out there: Element0 and the others: Element1, Element2 etc .
  14. /// Element zero is different because you can't divide by zero! (This probably won't make any sense)
  15. /// Till you attempt the problem yourself.
  16. ///
  17. /// </summary>
  18. /// <param name="elementCount"></param>
  19. /// <returns></returns>
  20. public double Estimate(int elementCount)
  21. {
  22. ElementCollection ec = new ElementCollection(elementCount);
  23. return 4 * ec.AddAllElements();
  24. }
  25. }
  26.  
  27. public class ElementCollection
  28. {
  29. private int elementCount;
  30.  
  31. public ElementCollection(int elementCount)
  32. {
  33. this.elementCount = elementCount;
  34. }
  35.  
  36. public double AddAllElements()
  37. {
  38. double result = 0.0;
  39. for (int i = 0; i < elementCount + 1; i++)
  40. {
  41. ElementN element = ElementFactory(i);
  42. result += element.Value();
  43. }
  44.  
  45. return result;
  46. }
  47.  
  48. public ElementN ElementFactory(int i)
  49. {
  50. if (i == 0)
  51. {
  52. return new Element0(i);
  53. }
  54. else
  55. {
  56. return new ElementN(i);
  57. }
  58. }
  59.  
  60. public class Element0 : ElementN
  61. {
  62. public Element0(int elementCount)
  63. : base(elementCount)
  64. {
  65. }
  66.  
  67. public override int Sign()
  68. {
  69. return 1;
  70. }
  71.  
  72. public override double PosivitveValue()
  73. {
  74. return 1.0;
  75. }
  76. }
  77.  
  78. public class ElementN
  79. {
  80. private int elementCount;
  81.  
  82. public ElementN(int elementCount)
  83. {
  84. this.elementCount = elementCount;
  85. }
  86.  
  87. virtual public double Value()
  88. {
  89. return Sign() * PosivitveValue();
  90. }
  91.  
  92. virtual public double PosivitveValue()
  93. {
  94. return ((1.0) / (2.0 * elementCount + 1));
  95. }
  96.  
  97. /// <summary>
  98. /// Either the sign is positive or negative
  99. /// We could probably put this into its own class
  100. /// and have a factory method but we'll keep it like this for the moment
  101. /// till one day change requires us to change it. After all, a sign can only
  102. /// either be positive or negative. (apparently you can also multiple by the
  103. /// square root of (-1) but that's another matter.
  104. /// </summary>
  105. /// <returns></returns>
  106. virtual public int Sign()
  107. {
  108. if (elementCount % 2 == 0)
  109. {
  110. return 1;
  111. }
  112. else
  113. {
  114. return -1;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. ```
  121.  
  122.  
  123. # Here are the tests:
  124.  
  125. ```c#
  126. using NUnit.Framework;
  127.  
  128. namespace BKTest
  129. {
  130. [TestFixture]
  131. internal class PieTest
  132. {
  133. [Test]
  134. [TestCase(0, 4)]
  135. [TestCase(1, 4 * (1 - 1.00 / 3.0))]
  136. [TestCase(2, 4 * (1 - 1.00 / 3.0 + 1 / 5.0))]
  137. [TestCase(3, 4 * (1 - 1.00 / 3.0 + 1 / 5.0 - 1 / 7.0))]
  138. [TestCase(4, 4 * (1 - 1.00 / 3.0 + 1 / 5.0 - 1 / 7.0 + 1 / 9.0))]
  139. public void Estimate_1Parameter_expect_fourMinusOneThird(int input, double output)
  140. {
  141. // set up
  142. double result = new Pie().Estimate(input);
  143. Assert.AreEqual(output, result);
  144. }
  145. }
  146. }
  147. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement