Unit5.java.xhtml 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * <https://y.st./>
  4. * Copyright © 2017 Alex Yst <mailto:copyright@y.st>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org./licenses/>.
  18. **/
  19. $xhtml = array(
  20. 'title' => 'Unit5.java',
  21. 'subtitle' => 'Written in <span title="Programming 1">CS 1102</span> of <a href="http://www.uopeople.edu/">University of the People</a>, finalised on 2017-05-10',
  22. 'copyright year' => '2017',
  23. 'body' => <<<END
  24. <h2>Source code:</h2>
  25. <blockquote>
  26. <pre><code>/*
  27. * Copyright (C) 2017 Alex Yst
  28. *
  29. * This program is free software: you can redistribute it and/or modify
  30. * it under the terms of the GNU General Public License as published by
  31. * the Free Software Foundation, either version 3 of the License, or
  32. * (at your option) any later version.
  33. *
  34. * This program is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. * GNU General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU General Public License
  40. * along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;.
  41. */
  42. package unit5;
  43. /**
  44. * Alex Yst&apos;s CS 1102 (Programming 1) Unit 5 submission
  45. *
  46. * This class was programmed to fill the requirements on the Unit 5 programming
  47. * assignment in University of the People&apos;s CS 1102 (Programming 1) course.
  48. *
  49. * @author Alex Yst
  50. */
  51. public class Unit5 {
  52. /**
  53. * This method defines a class used for stats calculation, then uses it to
  54. * compute some statistics.
  55. *
  56. * @param arguments This parameter is unused.
  57. */
  58. public static void main(String[] arguments) {
  59. /*
  60. * An object of class StatCalc can be used to compute several simple statistics
  61. * for a set of numbers. Numbers are entered into the dataset using
  62. * the enter(double) method. Methods are provided to return the following
  63. * statistics for the set of numbers that have been entered: The number
  64. * of items, the sum of the items, the average, and the standard deviation
  65. */
  66. class StatCalc {
  67. private int count; // Number of numbers that have been entered.
  68. private double sum; // The sum of all the items that have been entered.
  69. private double squareSum; // The sum of the squares of all the items.
  70. /**
  71. * Add a number to the dataset. The statistics will be computed for all
  72. * the numbers that have been added to the dataset using this method.
  73. */
  74. public void enter(double num) {
  75. count++;
  76. sum += num;
  77. squareSum += num*num;
  78. }
  79. /**
  80. * Return the number of items that have been entered into the dataset.
  81. */
  82. public int getCount() {
  83. return count;
  84. }
  85. /**
  86. * Return the sum of all the numbers that have been entered.
  87. */
  88. public double getSum() {
  89. return sum;
  90. }
  91. /**
  92. * Return the average of all the items that have been entered.
  93. * The return value is Double.NaN if no numbers have been entered.
  94. */
  95. public double getMean() {
  96. return sum / count;
  97. }
  98. /**
  99. * Return the standard deviation of all the items that have been entered.
  100. * The return value is Double.NaN if no numbers have been entered.
  101. */
  102. public double getStandardDeviation() {
  103. double mean = getMean();
  104. return Math.sqrt( squareSum/count - mean*mean );
  105. }
  106. } // end class StatCalc
  107. StatCalc myStatCalc = new StatCalc();
  108. myStatCalc.enter(5);
  109. myStatCalc.enter(7);
  110. myStatCalc.enter(12);
  111. myStatCalc.enter(23);
  112. myStatCalc.enter(3);
  113. myStatCalc.enter(2);
  114. myStatCalc.enter(8);
  115. myStatCalc.enter(14);
  116. myStatCalc.enter(10);
  117. myStatCalc.enter(5);
  118. myStatCalc.enter(9);
  119. myStatCalc.enter(13);
  120. System.out.println(&quot;The following number set has the following statistics.&quot;);
  121. System.out.println(&quot;Number Set: 5, 7, 12, 23, 3, 2, 8, 14, 10, 5, 9, 13&quot;);
  122. System.out.println(&quot;Count: &quot; + myStatCalc.getCount());
  123. System.out.println(&quot;Mean: &quot; + myStatCalc.getMean());
  124. System.out.println(&quot;Standard Deviation: &quot; + myStatCalc.getStandardDeviation());
  125. }
  126. }</code></pre>
  127. </blockquote>
  128. <h2>Output:</h2>
  129. <blockquote>
  130. <pre>run:
  131. The following number set has the following statistics.
  132. Number Set: 5, 7, 12, 23, 3, 2, 8, 14, 10, 5, 9, 13
  133. Count: 12
  134. Mean: 9.25
  135. Standard Deviation: 5.539629951540085
  136. BUILD SUCCESSFUL (total time: 0 seconds)</pre>
  137. </blockquote>
  138. END
  139. );