Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. public class project8
  5. {
  6. public static void main(String[] args)
  7. throws FileNotFoundException
  8. {
  9. System.out.printf("Fall 2019 Project 8 Due 11/14/19 by Ryan Smith%n");
  10. Scanner input = new Scanner(new File("data8a.txt"));
  11. PrintWriter output = new PrintWriter("output.txt");
  12. double[] data = new double[200];
  13. int num = readArray(input, data);
  14. double mean = calcMean(data, num);
  15. PrntArray(output, data, num, 8);
  16. double middle = calcMedian(data, num);
  17. double stdDev = calcStdDev(data, num, mean);
  18. output.printf("%n There are %d numbers in the array with a mean of %.2f, median of %.2f, and a standard deviation of %.2f%n", num, mean, middle, stdDev);
  19. System.out.printf("Sorted numbers%n");
  20. sort(data, num);
  21. PrntArray(output, data, num, 9);
  22. output.close();
  23. }
  24. public static int readArray(Scanner file, double data[])
  25. {
  26. int num = 0;
  27. while (file.hasNextLine())
  28. {
  29. double next = file.nextDouble();
  30. data[num] = next;
  31. num++;
  32. }
  33. return num;
  34. }
  35. public static double calcMean(double a[], int num)
  36. {
  37. double mean = 0;
  38. int sum = 0;
  39. for (int i = 0; i < num; i++)
  40. {
  41. sum += a[i];
  42. }
  43. mean = sum/num;
  44.  
  45. return mean;
  46. }
  47. public static void PrntArray(PrintWriter output, double a[], int num, int ls)
  48. {
  49. for (int i = 0; i < num; i++)
  50. {
  51. output.printf("%.2f ", a[i]);
  52. if (i % ls == 0)
  53. {
  54. output.printf("%n");
  55. }
  56. }
  57. }
  58. public static double calcMedian(double a[], int num)
  59. {
  60. int middle = num/2;
  61. return a[middle];
  62. }
  63. public static double calcStdDev(double a[], int num, double mean)
  64. {
  65. double sum = 0;
  66. double dev = 0;
  67. for (int i = 0; i < num; i++)
  68. {
  69. sum += a[i];
  70. }
  71. for (int i = 0; i < num; i++)
  72. {
  73. dev += (a[i] - mean) * (a[i] - mean);
  74. }
  75. return Math.sqrt(dev-1);
  76. }
  77. public static void sort(double a[], int num)
  78. {
  79. double placeholder = 0;
  80. for (int i = 0; i < num; i++)
  81. {
  82. for (int k = 0; k < num-1; k++)
  83. {
  84. if (a[k] > a[k+1])
  85. {
  86. placeholder = a[k+1];
  87. a[k + 1] = a[k];
  88. a[k] = placeholder;
  89.  
  90. }
  91. }
  92. }
  93. for (int z = 0; z < num; z++)
  94. {
  95. System.out.printf("%.2f %n", a[z]);
  96. }
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement