Guest User

Untitled

a guest
Jan 22nd, 2018
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. /*--------------------------
  2. | Tyson Holub |
  3. | CSCI 111 |
  4. | Computing Deviation |
  5. | 03/29/11 |
  6. | Problem 6.11 |
  7. | tjholub@student.stcc.edu |
  8. --------------------------*/
  9.  
  10. //Numbers Numbers Numbers
  11.  
  12. import java.util.Scanner;
  13. import java.util.ArrayList;
  14.  
  15. public class Deviation {
  16.  
  17. public static void main(String[] args) {
  18. Numbers numbers = getNumbers();
  19. numbers.printNumbers();
  20. System.out.println("\n"+numbers.size()+" values were read from scanner.");
  21. System.out.println("The largest number is "+numbers.largestNum);
  22. System.out.printf("The standard deviation is %.4f\n",numbers.deviation());
  23. System.out.printf("The mean is %.4f\n",numbers.mean());
  24. }
  25.  
  26. private static Numbers getNumbers(){
  27. Scanner input = new Scanner(System.in);
  28. //Numbers class extends ArrayList. Ignore first size setting input
  29. //if inputting via keyboard, enter first value twice.
  30. input.nextInt();
  31. Numbers numbers = new Numbers();
  32. //break input with any non numeric char if not reading from file
  33. while(input.hasNextDouble())
  34. numbers.nextNumber(input.nextDouble());
  35. return numbers;
  36. }
  37. }
  38.  
  39. //this class does not support a full extension
  40. //given ArrayList has methods that could alter
  41. //the List. Just spicing things up for practice.
  42. @SuppressWarnings("rawtypes")
  43. class Numbers extends ArrayList{
  44. private static final long serialVersionUID = 3527688495657167072L;
  45. private int count = 0;
  46. private double sum = 0;
  47. private double mean = 0;
  48. public double largestNum = 0;
  49.  
  50. //in case largest value is removed.
  51. private void findLargest(){
  52. largestNum = 0;
  53. for(int i=0;i<count;i++){
  54. if(Double.parseDouble(get(i).toString()) > largestNum)
  55. largestNum = Double.parseDouble(get(i).toString());
  56. }
  57. }
  58. public Numbers(){
  59. super();
  60. }
  61. public void printNumbers(){
  62. int spacing = String.valueOf(largestNum).length();
  63. for(int i=0;i<count;i++){
  64. System.out.printf("%"+spacing+".1f "+((i+1) % 15 == 0 ? "\n" : ""),get(i));
  65. }
  66. }
  67. @SuppressWarnings("unchecked")
  68. public void nextNumber(double number){
  69. add(number);
  70. if(number > largestNum)
  71. largestNum = number;
  72. sum += number;
  73. count++;
  74. }
  75. //for good measure
  76. public void removeLast(){
  77. if(count != 0)
  78. {
  79. count--;
  80. sum -= Double.parseDouble(get(count).toString());
  81. if(Double.parseDouble(get(count).toString()) == largestNum)
  82. {
  83. remove(count);
  84. findLargest();
  85. }
  86. else
  87. remove(count);
  88. }
  89. }
  90. public double mean(){
  91. if(count != 0)
  92. mean = sum / count;
  93. else
  94. mean = 0;
  95. return mean;
  96. }
  97. public double deviation(){
  98. double deviation = 0;
  99. if(count > 1)
  100. {
  101. mean();
  102. for(int i=0;i<count;i++)
  103. deviation += (Math.pow(Double.parseDouble(get(i).toString()) - mean,2));
  104. deviation = Math.sqrt(deviation / (count - 1));
  105. }
  106. return deviation;
  107. }
  108. }
Add Comment
Please, Sign In to add comment