Advertisement
Crenox

SuperStat Java Program

Dec 9th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. // Sammy Samkough
  2. // SuperStat
  3. // Spec: With the set of numbers, the program will give you information about them.
  4.  
  5. import java.util.Arrays;
  6.  
  7. public class SuperStat
  8. {
  9. private String[] strNums;
  10. private int[] intNums;
  11. private int[] modes;
  12.  
  13. /** Default / No arg Constructor - sets both strNums and intNums arrays to zero */
  14. public SuperStat()
  15. {
  16. strNums = new String[0];
  17. intNums = new int[0];
  18. }
  19.  
  20. /** Constructor that takes a String nums and builds both a String[] and an int[] from it */
  21. public SuperStat(String nums)
  22. {
  23. strNums = nums.split(",");
  24. intNums = new int[strNums.length];
  25.  
  26. for(int i = 0; i < strNums.length; i++)
  27. {
  28. intNums[i] = Integer.parseInt(strNums[i]);
  29. }
  30. }
  31.  
  32. /** Calculates the Min for this intNums array */
  33. public int getMin()
  34. {
  35. int min = intNums[0];
  36.  
  37. for(int i = 0; i < intNums.length; i++)
  38. {
  39. if(intNums[i] < min)
  40. {
  41. min = intNums[i];
  42. }
  43. }
  44.  
  45. return min;
  46. }
  47.  
  48. /** Calculates the Max for this intNums array */
  49. public int getMax()
  50. {
  51. int max = intNums[0];
  52.  
  53. for(int i = 0; i <intNums.length; i++)
  54. {
  55. if(intNums[i] > max)
  56. {
  57. max = intNums[i];
  58. }
  59. }
  60.  
  61. return max;
  62. }
  63.  
  64. /** Calculates the Range for this intNums array
  65. /* Note: You should make use of getMax() and getMin() here, don't re-implement code you already have */
  66. public int getRange()
  67. {
  68. int range = getMax() - getMin();
  69.  
  70. return range;
  71. }
  72.  
  73. /** Calculates the Arithmetic Mean for this intNums array */
  74. public double getMean()
  75. {
  76. int sum = 0;
  77. int mean = 0;
  78.  
  79. for(int i = 0; i < intNums.length; i++)
  80. {
  81. sum += intNums[i];
  82. }
  83.  
  84. mean = (sum / intNums.length);
  85.  
  86. return mean;
  87. }
  88.  
  89. //Added a method for myself. Returns the number of modes.
  90. public int getNumberOfModes()
  91. {
  92. Arrays.sort(intNums);
  93.  
  94. int x = 1;
  95. int y = 1;
  96. int mode = -1;
  97. int dx = 0;
  98. int dy = 0;
  99. int numberOfModes = 0;
  100.  
  101. for(int i = 1; i < intNums.length; i++)
  102. {
  103. dx = intNums[i];
  104. dy = intNums[i - 1];
  105.  
  106. if(dx == dy)
  107. {
  108. x++;
  109. }
  110. else
  111. {
  112. x = 1;
  113. }
  114.  
  115. if(x > y)
  116. {
  117. numberOfModes = 1;
  118. y = x;
  119. x = 1;
  120. }
  121. else if(x == y)
  122. {
  123. numberOfModes++;
  124. }
  125. else
  126. {
  127. x = 1;
  128. continue;
  129. }
  130. }
  131.  
  132. return numberOfModes;
  133. }
  134.  
  135. /** Populates an int[] with a mode or modes from the intNums array
  136. /* Returns null if no mode found
  137. /* Note: Be sure to first sort the array (built-in method in Java)
  138. /* Consider using nested for loops (and several vars) to track potential modes */
  139. public int[] getMode()
  140. {
  141. Arrays.sort(intNums);
  142.  
  143. int x = 1;
  144. int y = 1;
  145. int mode = -1;
  146. int dx;
  147. int dy;
  148. int dxy = 0;
  149.  
  150. modes = new int[getNumberOfModes()];
  151.  
  152. if(getNumberOfModes() == 1)
  153. {
  154. for(int i = 1; i < intNums.length; i++)
  155. {
  156. dx = intNums[i];
  157. dy = intNums[i - 1];
  158.  
  159. if(dx == dy)
  160. {
  161. x++;
  162. }
  163. else
  164. {
  165. x = 1;
  166. }
  167.  
  168. if(x > y)
  169. {
  170. modes[0] = intNums[i - 1];
  171. y = x;
  172. x = 1;
  173. }
  174. else
  175. {
  176. x = 1;
  177. continue;
  178. }
  179. }
  180. }
  181. else if(getNumberOfModes() == intNums.length - 1) // this makes the method return 0
  182. {
  183. modes[0] = 0;
  184. }
  185. else if(getNumberOfModes() > 1)
  186. {
  187. for(int i = 1; i < intNums.length; i++)
  188. {
  189. dx = intNums[i];
  190. dy = intNums[i - 1];
  191.  
  192. if(dx == dy)
  193. {
  194. x++;
  195. }
  196. else
  197. {
  198. x = 1;
  199. }
  200.  
  201. if(x > y)
  202. {
  203. dxy = 0;
  204. modes[dxy] = dx;
  205. y = x;
  206. x = 1;
  207. dxy++;
  208. }
  209. else if(x == y)
  210. {
  211. modes[dxy] = dx;
  212. dxy++;
  213. }
  214. else
  215. {
  216. x = 1;
  217. continue;
  218. }
  219.  
  220. }
  221.  
  222. }
  223. else
  224. {
  225. modes[0] = 0;
  226. }
  227.  
  228. return modes;
  229. }
  230.  
  231. /** Calculates the Geometric Mean for this intNums array */
  232. public double getGeometricMean()
  233. {
  234. double product = 1;
  235. double geoMean = 0;
  236.  
  237. for(int i = 0; i < intNums.length; i++)
  238. {
  239. product *= intNums[i];
  240. }
  241.  
  242. geoMean = Math.pow(product, (1.0 / intNums.length));
  243.  
  244. return geoMean;
  245. }
  246.  
  247. /** Calculates the median for this intNums array */
  248. public double getMedian()
  249. {
  250. Arrays.sort(intNums);
  251.  
  252. double median = 0;
  253. int x = 0;
  254.  
  255. x = (int)(intNums.length/2);
  256.  
  257. if(intNums.length % 2 == 1)
  258. {
  259. median = intNums[x++];
  260.  
  261. return median;
  262. }
  263. else
  264. {
  265. median = (intNums[x - 1] + intNums[x]) / 2.0;
  266.  
  267. return median;
  268. }
  269.  
  270. }
  271.  
  272. /** Standard Deviation is calculated
  273. /* std dev = Square root of the sum of the square differences
  274. /* from the mean divided by the number of values */
  275. public double getStdDev()
  276. {
  277. double value = 0.0;
  278. double mean = getMean();
  279. double sum = 0.0;
  280. double[] arr = new double[intNums.length];
  281.  
  282. for(int i = 0; i < intNums.length; i++)
  283. {
  284. arr[i] = intNums[i];
  285. }
  286.  
  287. for(int i = 0; i < intNums.length; i++)
  288. {
  289. arr[i] = Math.pow(arr[i] - mean, 2);
  290. }
  291.  
  292. for(int i = 0; i < intNums.length; i++)
  293. {
  294. sum += arr[i];
  295. }
  296.  
  297. mean = sum / (arr.length - 1);
  298. mean = Math.sqrt(mean);
  299.  
  300. return mean;
  301. }
  302.  
  303. /** Returns a String with each of the Stats on a separate line
  304. /* ie. Number of Values, Mean, Median, Mode, Min, Max, Range and Standard Deviation */
  305. public String toString()
  306. {
  307. String result = "";
  308.  
  309. result += "\nNumber of Values: " + intNums.length;
  310. result += "\nArithmetic Mean: " + getMean();
  311. result += "\nGeometric Mean: " + getGeometricMean();
  312. result += "\nMedian: " + getMedian();
  313. result += "\nMode: " + Arrays.toString(getMode());
  314. result += "\nMin: " + getMin();
  315. result += "\nMax: " + getMax();
  316. result += "\nRange: " + getRange();
  317. result += "\nStandard Deviation: " + getStdDev();
  318.  
  319. return result;
  320. }
  321. }
  322. -------------------------------------------------------------------------------------------------------------------------------------
  323. // Sammy Samkough
  324. // SuperStat
  325. // Spec: With the set of numbers, the program will give you information about them.
  326.  
  327. public class SuperStatClient
  328. {
  329. public static void main(String[] args)
  330. {
  331. SuperStat nums = new SuperStat("12,40,50,55,33,23");
  332.  
  333. System.out.println(nums);
  334. }
  335. }
  336.  
  337. /*
  338. Number of Values: 6
  339. Arithmetic Mean: 35.0
  340. Geometric Mean: 31.632677318956212
  341. Median: 36.5
  342. Mode: [0, 0, 0, 0, 0]
  343. Min: 12
  344. Max: 55
  345. Range: 43
  346. Standard Deviation: 16.291101865742537
  347. Press any key to continue . . .
  348. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement