Using Java to read data file, and summary statistcs such MAX,MIN, MEAN AND STAND
ID: 3804365 • Letter: U
Question
Using Java to read data file, and summary statistcs such MAX,MIN, MEAN AND STANDARD DEVIATION:
r f m t c 2 50 12500 98 1 0 13 3250 28 1 1 16 4000 35 1 2 20 5000 45 1 1 24 6000 77 0 4 4 1000 4 0 2 7 1750 14 1 1 12 3000 35 0 2 9 2250 22 1 5 46 11500 98 1 4 23 5750 58 0 0 3 750 4 0 2 10 2500 28 1 1 13 3250 47 0 2 6 1500 15 1 2 5 1250 11 1 2 14 3500 48 1 2 15 3750 49 1 2 6 1500 15 1 2 3 750 4 1 2 3 750 4 1 4 11 2750 28 0 2 6 1500 16 1 2 6 1500 16 1 9 9 2250 16 0 4 14 3500 40 0 4 6 1500 14 0 4 12 3000 34 1 4 5 1250 11 1 4 8 2000 21 0 1 14 3500 58 0 4 10 2500 28 1 4 10 2500 28 1 4 9 2250 26 1 2 16 4000 64 0 2 8 2000 28 1 2 12 3000 47 1 4 6 1500 16 1 2 14 3500 57 1 4 7 1750 22 1 2 13 3250 53 1 2 5 1250 16 0 2 5 1250 16 1 2 5 1250 16 0 4 20 5000 69 1 4 9 2250 28 1 2 9 2250 36 0 2 2 500 2 0 2 2 500 2 0 2 2 500 2 0 2 11 2750 46 0 2 11 2750 46 1 2 6 1500 22 0 2 12 3000 52 0 4 5 1250 14 1 4 19 4750 69 1 4 8 2000 26 1 2 7 1750 28 1 2 16 4000 81 0 3 6 1500 21 0 2 7 1750 29 0 2 8 2000 35 1 2 10 2500 49 0 4 5 1250 16 1 2 3 750 9 1 3 16 4000 74 0 2 4 1000 14 1 0 2 500 4 0 4 7 1750 25 0 1 9 2250 51 0 2 4 1000 16 0 2 4 1000 16 0 4 17 4250 71 1 2 2 500 4 0 2 2 500 4 1 2 2 500 4 1 2 4 1000 16 1 2 2 500 4 0 2 2 500 4 0 2 2 500 4 0 4 6 1500 23 1 2 4 1000 16 0 2 4 1000 16 0 2 4 1000 16 0 2 6 1500 28 1 2 6 1500 28 0 4 2 500 4 0 4 2 500 4 0 4 2 500 4 0 2 7 1750 35 1 4 2 500 4 1 4 2 500 4 0 4 2 500 4 0 4 2 500 4 0 12 11 2750 23 0 4 7 1750 28 0 3 17 4250 86 0 4 9 2250 38 1 4 4 1000 14 1 5 7 1750 26 1 Programing Assignment 02 (100 points Data Explorer version 01 Read a data in Frist row: get the attribute name. From the second row to the last row of data file: For each column (attribute) calculate and display: Column name (attribute name) Minimum value: Maximum value Mean: Standard deviation value: The last column of data: only display the count element in each class Requirement: You have to create a class name Statistic and a main program name Explorer The Statistic class contains 4 methods: Method to compute minimum value Method to compute maximum value Method to compute mean Method to compute standard deviation value. Method to count number of element that has the attribute equal to one particula value. This method will return integer value of count. They all return a double value The Explorer contain a main class, that will: Read the project02Data.txt. This file can be download from Canvas under Program Assignment 02 folder. Call methods in Statistic class Display the results. ***Note: of the last column: count element is each class +5 point if can read in a csv file.Explanation / Answer
Implemented - Explorer class, statistic class - methods - mean,max & min
package assignments2;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Explorer {
public static void main(String[] theArgs) {
Scanner input;
String columns[] = null;
ArrayList<ArrayList<Float>> columnData = new ArrayList<ArrayList<Float>>();
try {
input = new Scanner(new File("D://ravi//Cheg//project02Data.txt"));
int noOfLines = 0;
String line;
String tokens[]= null;
int noOfClms = 0;
while(input != null && input.hasNext()) {
line = input.nextLine();
//Assuming row values are seperated by space
tokens = line.split(" ");
if(noOfLines == 0) {
noOfClms = tokens.length;
columns = tokens;
for(int i = 0; i < columns.length; i++) {
columnData.add(new ArrayList<Float>());
}
} else {
for(int i = 0; i < columns.length; i++) {
columnData.get(i).add(Float.parseFloat(tokens[i].trim()));
}
}
noOfLines++;
}
Statstics stats = new Statstics();
for(int i = 0; i <columns.length; i++) {
System.out.println("Column : "+columns[i]+ " Min : "+stats.getMin(columnData.get(i))
+ " Max : "+stats.getMax(columnData.get(i))
+ " Mean : "+stats.computeMean(columnData.get(i)));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Statstics {
float getMin(ArrayList<Float> data) {
float min = data.get(0);
for(int i = 1; i < data.size();i ++) {
if(min > data.get(i))
min = data.get(i);
}
return min;
}
float getMax(ArrayList<Float> data) {
float max = data.get(0);
for(int i = 1; i < data.size();i ++) {
if(max < data.get(i))
max = data.get(i);
}
return max;
}
float computeMean(ArrayList<Float> data) {
float sum = 0;
for(int i = 0; i < data.size();i ++) {
sum += data.get(i);
}
return sum/data.size();
}
}
---Input----
r f m t c
2 50 12500 98 1
0 12 3250 28 1
1 16 4000 35 1
2 20 5000 45 1
----output----
Column : r Min : 0.0 Max : 2.0 Mean : 1.25
Column : f Min : 12.0 Max : 50.0 Mean : 24.5
Column : m Min : 3250.0 Max : 12500.0 Mean : 6187.5
Column : t Min : 28.0 Max : 98.0 Mean : 51.5
Column : c Min : 1.0 Max : 1.0 Mean : 1.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.