Advertisement
Crenox

Standard Deviation Java Program

May 13th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. // Sammy Samkough
  2. // Standard Deviation Static
  3.  
  4. import java.util.Scanner;
  5. import java.util.ArrayList;
  6.  
  7. public class StandardDeviationStatic
  8. {
  9. public static void main(String args[])
  10. {
  11. /*Scanner sc = new Scanner(System.in);
  12. ArrayList values = new ArrayList();
  13. String b = "";
  14. boolean exit = true;
  15.  
  16. while (exit != false)
  17. {
  18. System.out.print("Please enter a value (number): ");
  19. values = sc.nextInt();
  20. System.out.println("\nWould you like to continue? Please say yes or no.");
  21. b = sc.next();
  22.  
  23. if (b.equalsIgnoreCase("no"))
  24. {
  25. exit = false;
  26. }
  27. else
  28. {
  29. exit = true;
  30. }
  31. }*/
  32.  
  33. int std = 0;
  34. int[] valuesTest = new int[]{1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 8};
  35. int sumOfValues = 0;
  36. int numberOfValues = valuesTest.length;
  37. int average = 0;
  38.  
  39. // getting the average
  40. for (int i = 0; i < valuesTest.length; i++)
  41. {
  42. average += valuesTest[i];
  43. }
  44.  
  45. average /= valuesTest.length;
  46.  
  47. for (int i = 0; i < valuesTest.length; i++)
  48. {
  49. sumOfValues += valuesTest[i];
  50. }
  51.  
  52. for (int i = 0; i < valuesTest.length; i++)
  53. {
  54. std += (int) Math.sqrt(sumOfValues * (Math.pow((valuesTest[i] - average), 2)) / numberOfValues);
  55. }
  56.  
  57. System.out.println("The Standard Deviation is: " + std);
  58. }
  59. }
  60. /*
  61.  
  62. */
  63. -------------------------------------------------------------------------------------------------------------------------------
  64. // Sammy Samkough
  65. // Standard Deviation OOP
  66.  
  67. public class StandardDeviation
  68. {
  69. private int[] values; // each value
  70. private int average; // average of all values
  71. private int numberOfValues; // number of all values
  72. private int sumOfValues; // sum of (values - average)^2
  73.  
  74. public StandardDeviation()
  75. {
  76. values = new int[0];
  77. average = 0;
  78. numberOfValues = 0;
  79. sumOfValues = 0;
  80. }
  81.  
  82. public StandardDeviation(int[] v, int a, int nov, int sov)
  83. {
  84. values = v;
  85. average = a;
  86. numberOfValues = nov;
  87. sumOfValues = sov;
  88. }
  89.  
  90. public void setValues(int v[])
  91. {
  92. values = v;
  93. }
  94.  
  95. public int formula()
  96. {
  97. // formula
  98. int std = 0;
  99.  
  100. for (int i = 0; i < values.length; i++)
  101. {
  102. std = (int) Math.sqrt(sumOfValues * (Math.pow((values[i] - average), 2)) / numberOfValues);
  103. }
  104.  
  105. return std;
  106. }
  107. }
  108. -------------------------------------------------------------------------------------------------------------------------------
  109. // Sammy Samkough
  110. // Standard Deviation OOP
  111.  
  112. import java.util.Scanner;
  113.  
  114. public class StandardDeviationClient
  115. {
  116. public static void main(String args[])
  117. {
  118. StandardDeviation std = new StandardDeviation();
  119. Scanner sc = new Scanner(System.in);
  120. boolean ifEnter = true;
  121. int a = 0;
  122.  
  123. System.out.println("Welcome to the Standard Deviation Class! We're going to have you input the values! Please enter: ");
  124.  
  125. while (ifEnter == true)
  126. {
  127. a = sc.nextInt();
  128. std.setValues(a);
  129. }
  130. }
  131. }
  132. /*
  133.  
  134. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement