Question
I need help fixing my code. The program is about finding the mean and standard deviation. Sample output. Using the following data files sort1.txt:100 90
I need help fixing my code. The program is about finding the mean and standard deviation.
Sample output. Using the following data files
sort1.txt:100 90 75 80
sort2.txt:99 92 60 75 70
sort3.txt:99 92 60 75 70 50
Your output (with a couple of debug statements thrown in) might be:
Enter the name of the first filesort1.txt
Enter the name of the second filesort2.txt
Enter the name of the third filesort3.txt
task1 returns count = 4, sum = 345
task2 returns count = 5, sum = 396
task3 returns count = 6, sum = 446
Driver newMean = 79.133333
Driver sumDiffsSq: 571.337778 1026.822222 1875.573333
Totals sum = 1187, count = 15, sumDiffsSq = 3473.733333
Average = 79.13
Standard Deviation = 15.75
My code:
import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; import java.io.*; import java.text.DecimalFormat; public class HadoopSim implements Runnable { private final int SIZE = 100; private int [] arrayData = new int [SIZE]; private int count = 0; private int sum = 0; private double sumDiffsSq = 0.0; private String fileName; private Scanner scan; private double newMean = 0.0; //Constructor to help read the file using Scanner public HadoopSim(String filename) { scan = new Scanner(System.in); try { File file = new File(filename); scan = new Scanner(file); int i=0; while(scan.hasNextLine()) { arrayData[i] = scan.nextInt(); count++; sum += arrayData[i]; i++; } scan.close(); } catch (FileNotFoundException e) { System.out.println("File " + filename + " could not be found."); System.exit(1); } } @Override public void run() { } //count is set when reading the file public int getCount() { return count; } //sum is set ina thread by tun() method public int getSum() { return sum; } //call after all threads have been completed public void setNewMean(double m) { newMean=m; } //method to compute each task;s sum of differneces quared using new mean public double setSumDiffSqs() { // System.out.println(newMean); double x=0.0; for(int i=0;i// System.out.println(arrayData[i]); if(arrayData[i]!=0) x+= (Math.abs(newMean-arrayData[i])* Math.abs(newMean-arrayData[i])); //System.out.println(x); } sumDiffsSq+=x; return x; } //returns the sum of differences squared for the data in each task's array public double getSumDiffSqs() { return sumDiffsSq; } }
import java.util.Scanner; import java.io.*; import java.text.DecimalFormat; public class HadoopDriver { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter the name of first file "); String file1 = scan.nextLine(); System.out.println("Enter the name of second file "); String file2 = scan.nextLine(); System.out.println("Enter the name of third file "); String file3 = scan.nextLine(); //File file3 = new File(input.next()); scan.close(); HadoopSim h=new HadoopSim(file1); Thread thread = new Thread(h); thread.start(); int c1=h.getCount(); int sum1=h.getSum(); System.out.println(" Task1 return count = "+c1 +" and sum = "+sum1); HadoopSim h2=new HadoopSim(file2); Thread thread2 = new Thread(h2); thread2.start(); int c2=h2.getCount(); int sum2=h2.getSum(); System.out.println("Task2 return count = "+c2 +" and sum = "+sum2); HadoopSim h3=new HadoopSim(file3); Thread thread3 = new Thread(h3); thread3.start(); int c3=h3.getCount(); int sum3=h3.getSum(); System.out.println("Task3 return count = "+c3 +" and sum = "+sum3); double final_sum=sum1+sum2+sum3; double final_count=c1+c2+c3; h.setNewMean(final_sum/final_count); double mean=final_sum/final_count; double sd= h.setSumDiffSqs(); h2.setNewMean(final_sum/final_count); double sd2=h2.setSumDiffSqs(); h3.setNewMean(final_sum/final_count); double sd3= h3.setSumDiffSqs(); System.out.println(" Driver newMean is = "+mean); System.out.println("Driver sumDiffsSq = "+sd +" "+sd2+" "+sd3); System.out.println("Total Sum = "+ final_sum+" , count = "+final_count+" , sumDiffsSq = "+(sd+sd2+sd3)); double avg=final_sum/final_count; DecimalFormat df = new DecimalFormat("#.##"); System.out.println("Average = "+df.format(avg)); System.out.println("Standard Deviation = "+ Math.sqrt((sd+sd2+sd3)/final_count)); } }
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