Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.FileNotFoundException;
- import java.util.*;
- import java.io.File;
- public class Main {
- public static void main(String[] args) {
- String fileName = "Property_Assessment_data_2019.csv";
- // We are going to read from a .csv file, Comma Separated Value File
- Integer[] assessedValues = getAssessedValues(fileName);
- //Now we sort the array
- Arrays.sort(assessedValues);
- System.out.println("Descriptive statistics of " + fileName);
- System.out.println("n = "+ getN(assessedValues));
- System.out.println("min = " + getMin(assessedValues));
- System.out.println("max = " + getMax(assessedValues));
- System.out.println("range = " + getRange(assessedValues));
- System.out.println("mean = " + getMean(assessedValues));
- System.out.println("sd = " + getsd(assessedValues));
- System.out.println("Median = " + getMedian(assessedValues));
- }
- private static double getMedian(Integer[] args){
- double Median = 0;
- int totalElements= args.length;
- if (totalElements % 2 == 0){
- int sumMiddle = args[totalElements/2] + args[totalElements/2 -1];
- //now get average of the middle elements
- Median = (double) sumMiddle /2;
- }
- else{
- Median = (double) args[totalElements/2];
- }
- return Median;
- }
- private static double getsd(Integer[] args){
- double sd = 0;
- double average = getMean(args);
- for (int i=0; i<args.length;i++){
- sd = sd + Math.pow(args[i] - average, 2);
- }
- return sd;
- }
- private static double getMean(Integer[] args){
- double sum = 0;
- double sd = 0;
- for (int i = 0; i< args.length; i++){
- sum += args[i];
- }
- return sum / args.length;
- }
- private static Integer getRange(Integer[] args){
- int max = getMax(args);
- int min = getMin(args);
- return max-min;
- }
- private static Integer getMax(Integer[] args){
- int last = args.length -1;
- return args[last];
- }
- private static Integer getMin(Integer[] args){
- return args[0];
- }
- private static Integer getN(Integer[] args){
- int length = args.length;
- return length;
- }
- //This method opens the file, and gets all the assessed values from the csv file, then stores them inside of an Integer array.
- public static Integer[] getAssessedValues(String args){
- ArrayList<Integer> assessedValues = new ArrayList<Integer>();
- String fileName = "Property_Assessment_Data_2019.csv";
- File file = new File(fileName);
- try{
- Scanner inputStream = new Scanner(file);
- inputStream.nextLine(); //We ignore first line.
- while (inputStream.hasNext()){
- String data = inputStream.nextLine();
- String[] values = data.split(",");
- //Here we need to parse int, as it is a string, so we store each asssessed value as an int.
- int parsed = Integer.parseInt(values[4]);
- //Here we add the parsed integer to our array list
- assessedValues.add(parsed);
- }
- //Here we convert the array list to an object array.
- inputStream.close();
- } catch (FileNotFoundException e){
- e.printStackTrace();
- }
- //Here we return the assessedValues with toArray, converting it into a new Integer array.(IDE did this)
- return assessedValues.toArray(new Integer[0]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement