Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. package com.company;
  2. import java.io.FileNotFoundException;
  3. import java.util.*;
  4. import java.io.File;
  5.  
  6.  
  7. public class Main {
  8.  
  9. public static void main(String[] args) {
  10. String fileName = "Property_Assessment_data_2019.csv";
  11. // We are going to read from a .csv file, Comma Separated Value File
  12. Integer[] assessedValues = getAssessedValues(fileName);
  13. //Now we sort the array
  14. Arrays.sort(assessedValues);
  15. System.out.println("Descriptive statistics of " + fileName);
  16. System.out.println("n = "+ getN(assessedValues));
  17. System.out.println("min = " + getMin(assessedValues));
  18. System.out.println("max = " + getMax(assessedValues));
  19. System.out.println("range = " + getRange(assessedValues));
  20. System.out.println("mean = " + getMean(assessedValues));
  21. System.out.println("sd = " + getsd(assessedValues));
  22. System.out.println("Median = " + getMedian(assessedValues));
  23.  
  24.  
  25.  
  26. }
  27. private static double getMedian(Integer[] args){
  28. double Median = 0;
  29. int totalElements= args.length;
  30. if (totalElements % 2 == 0){
  31. int sumMiddle = args[totalElements/2] + args[totalElements/2 -1];
  32. //now get average of the middle elements
  33. Median = (double) sumMiddle /2;
  34. }
  35. else{
  36. Median = (double) args[totalElements/2];
  37. }
  38. return Median;
  39. }
  40. private static double getsd(Integer[] args){
  41. double sd = 0;
  42. double average = getMean(args);
  43. for (int i=0; i<args.length;i++){
  44. sd = sd + Math.pow(args[i] - average, 2);
  45.  
  46. }
  47. return sd;
  48. }
  49. private static double getMean(Integer[] args){
  50. double sum = 0;
  51. double sd = 0;
  52. for (int i = 0; i< args.length; i++){
  53. sum += args[i];
  54. }
  55. return sum / args.length;
  56. }
  57. private static Integer getRange(Integer[] args){
  58. int max = getMax(args);
  59. int min = getMin(args);
  60. return max-min;
  61. }
  62. private static Integer getMax(Integer[] args){
  63. int last = args.length -1;
  64. return args[last];
  65. }
  66. private static Integer getMin(Integer[] args){
  67. return args[0];
  68. }
  69. private static Integer getN(Integer[] args){
  70. int length = args.length;
  71. return length;
  72. }
  73. //This method opens the file, and gets all the assessed values from the csv file, then stores them inside of an Integer array.
  74. public static Integer[] getAssessedValues(String args){
  75. ArrayList<Integer> assessedValues = new ArrayList<Integer>();
  76. String fileName = "Property_Assessment_Data_2019.csv";
  77. File file = new File(fileName);
  78. try{
  79. Scanner inputStream = new Scanner(file);
  80. inputStream.nextLine(); //We ignore first line.
  81. while (inputStream.hasNext()){
  82. String data = inputStream.nextLine();
  83. String[] values = data.split(",");
  84. //Here we need to parse int, as it is a string, so we store each asssessed value as an int.
  85. int parsed = Integer.parseInt(values[4]);
  86. //Here we add the parsed integer to our array list
  87. assessedValues.add(parsed);
  88. }
  89. //Here we convert the array list to an object array.
  90. inputStream.close();
  91.  
  92. } catch (FileNotFoundException e){
  93. e.printStackTrace();
  94. }
  95. //Here we return the assessedValues with toArray, converting it into a new Integer array.(IDE did this)
  96. return assessedValues.toArray(new Integer[0]);
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement