Question
Java math project (Updated with comments) I have a project to read data from a file and output the information using some math. I am
Java math project (Updated with comments)
I have a project to read data from a file and output the information using some math. I am nearly done I just have to complete median, mode and percentiles
--- Main.java file ---
package project1; import java.io.IOException; import java.util.Scanner; public class Project1 { public static void main(String[] args) throws IOException { Scanner input = new Scanner(System.in); System.out.println("Enter file: "); //String file = input.next(); String file = "data.tsv"; System.out.println("Enter column name: "); //String col = input.next(); String col = "Alpha"; System.out.println("Reading column " + col + " from " + file); project1.Stats s = new project1.Stats(file); s.display(); s.display(col); } } |
--- Stats.java ---
package project1; import java.io.*; import java.util.Arrays; import java.util.Scanner; public class stats { private String[] headers; private int[][] data; public stats(String file) throws FileNotFoundException// read TSV { Scanner scanner = new Scanner(new File(file)); // Scanner object to read in the file // Setup the headers String[] _headers = scanner.nextLine().split("\t"); // Setup the data int row = 0; // Keeps track of what data row we are at int[][] newData = new int[_headers.length][20]; // Instatiate a new array to store the data in // Loop through the file while (scanner.hasNextLine()){ String[] line = scanner.nextLine().split("\t"); // Split the current line based on tabs in the file // For all the data on this line, store it in our array, we know it's an integer so parseInt is safe here for (int i = 0; i < line.length; i++){ newData[i][row] = Integer.parseInt(line[i]); } // Increase the col count since we are moving to the next column row++; } // Now that we know how much data we need to store, we can copy over the data into a new array of the correct size data = new int[_headers.length][row]; // Instantiate the instance variable this time // This is literally just copying over the old array to the new instance variable for (int i = 0; i < _headers.length; i++){ System.arraycopy(newData[i], 0, data[i], 0, row); } // Set the instance variable headers headers = _headers; } public void display() // For debugging { System.out.println(Arrays.toString(this.headers)); for (int[] x:this.data) { System.out.println(Arrays.toString(x)); } } private int indexAt(String col) throws IllegalArgumentException { for (int i = 0; i < headers.length; i++){ if (col.equalsIgnoreCase(headers[i])){ return i; } } throw new IllegalArgumentException(col + " is not a valid header!"); } public void display(String col) { if (this.indexAt(col) < 0) { System.out.println("No such column"); } else { System.out.println(""); this.summary(col); System.out.println("count = " + this.count(col)); System.out.println("sum = " + this.sum(col)); System.out.println("min = " + this.min(col)); System.out.println("max = " + this.max(col)); System.out.println("mean = " + this.mean(col)); System.out.println("stdev = " + this.stdev(col)); System.out.println("median = " + this.median(col)); System.out.println("mode = " + this.mode(col)); this.displayPercentiles(col); } } public void summary(String col) // Overall summary { System.out.println("Summary (value: count)"); } public int count(String col) { int index = indexAt(col); int[] colData = data[index]; return colData.length; } public int sum(String col) // sum { int total = 0; int index = indexAt(col); int[] colData = data[index]; for (int currentValue: colData){ total += currentValue; } return total; } public double mean(String col) // Mean { return sum(col) * 1.0 / count(col); } public double stdev(String col) // Standard deviation { int index = indexAt(col); int[] colData = data[index]; double total = 0; double avg = mean(col); for (int currentValue: colData){ double difference = currentValue - avg; total += Math.pow(difference, 2); } double sqrt = Math.sqrt(total/(count(col)-1)); return sqrt; } private void sortArray(String col){ int index = indexAt(col); int[] array = data[index]; boolean arraySorted = true; do { arraySorted = true; // Reset this checker, if we pass all if checks, this stays true allowing us to break out of the loop // Loop through the array, do not check the last value though, as our if check is a +1 to the index for (int i = 0; i < array.length - 1; i++){ // If the index after our current index is greater than our current index, swap the positions of the elements if (array[i] > array[i + 1]){ int temp = array[i]; array[i] = array[i + 1]; array[i + 1] = temp; arraySorted = false; // We made a modification, the array isn't sorted yet } } } while (!arraySorted); } public double median(String col) // Median feature that is unfinished { sortArray(col); int index = indexAt(col); int[] colData = data[index]; if (colData.length % 2 == 0){ } return 0; } // Mode determine most common value // If ties, return smallest among most common values public int mode(String col) // Mode feature that is unfinished { return (int) 0; } public int min(String col) // Outputs minimum value { int index = indexAt(col); int[] colData = data[index]; int min = colData[0]; for (int currentValue: colData){ min = Math.min(min, currentValue); } return min; } public int max(String col) // Outputs maximum value { int index = indexAt(col); int[] colData = data[index]; int max = colData[0]; for (int currentValue: colData){ max = Math.max(max, currentValue); } return max; } public void displayPercentiles(String col) // Percentiles { System.out.print(" 0th percentile = "); System.out.println(getPercentile(col, 0)); System.out.print(" 25th percentile = "); System.out.println(getPercentile(col, 25)); System.out.print(" 50th percentile = "); System.out.println(getPercentile(col, 50)); System.out.print(" 75th percentile = "); System.out.println(getPercentile(col, 75)); System.out.print("100th percentile = "); System.out.println(getPercentile(col, 100)); } public double getPercentile(String col, double perc) // Percentile feature that is unfinished { return 0; } } |
--- data.tsv file that Main.java will read from ---
Alpha Beta 1 6 2 7 2 8 3 7 5 6 |
--- Current output --- I need help calculating Median, Mode, and the percentiles
Enter file: Enter column name: Reading column Alpha from data.tsv [Alpha, Beta] [1, 2, 2, 3, 5] [6, 7, 8, 7, 6] Summary (value: count) count = 5 sum = 13 min = 1 max = 5 mean = 2.6 stdev = 1.5165750888103102 median = 0.0 mode = 0 0th percentile = 0.0 25th percentile = 0.0 50th percentile = 0.0 75th percentile = 0.0 100th percentile = 0.0 Process finished with exit code 0 |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started