VUsolutions Transferred to AchiKhasi.com

From December 2011, this blog www.VUsolutions.blogspot.com is transferred to http://achikhasi.com/vu/ . So, you may visit http://achikhasi.com/vu/ for latest study related help.

Back to home VUsolutions

VUsolutions Fans Club [join us for MORE solutions]

VUsolutions on Facebook

CS506 Assignment No. 1 Fall 2011 Solution

Sunday, October 30, 2011 Posted In Edit This
Question: 
Write a program that computes the average and standard deviation of the text floating point numbers in a file. Use the following formulas for the average and the standard deviation of N values. The formulas compute the sum, the sum of squares, the average, the average square, the variance, and finally, the standard deviation.
sum = x1 + x2 + x3 + ... + xN-1 + xN 

sumSQ = x12 + x22 + x32 + ... + xN-12 + xN2 

avg = sum/N

avgSQ = sumSQ/N

var = avgSQ - avg2

sd = var(1/2)
The input file will contain any number text floating point numbers, similar to the following:
10.5
12.9
9.67
12.05
8.23
10.08
10.23
7.7
10.4
11.34
Numbers could be several, or none per line, and negative numbers are perfectly OK. 

Solution:

Input Text:

10.0
20.0
30.0
40.0
50.0
............

SAVE THIS FILE IN .JAVA

import java.io.*;
import java.util.*;
import javax.swing.*;

public class assignment1{

public static void main( String args[] ){

try{
FileReader fr = null;
BufferedReader br = null;

fr = new FileReader ("input.txt");
br = new BufferedReader (fr);
String line = br.readLine();

double sum =0 ;


while (line != null) {
double num = Double.parseDouble(line);
sum += num;
line = br.readLine();
}

JOptionPane.showMessageDialog(null, "Sum = " + sum);
br.close();
fr.close();
}
catch(NumberFormatException nfEx) {
System.out.println(nfEx);
}
catch(FileNotFoundException fnfEx){
System.out.println(fnfEx);
}
catch(IOException ioEx){
System.out.println(ioEx);
}
}
}

Back to home VUsolutions

Shaadi.com: Just create ur account & find ur partner or EARN money, its reall & EASY

VUsolutions Followers (Join NOW and Get Extra Benefits)

Install LATEST toolbar having lot of features - GET solutions on Desktop

toolbar powered by Conduit
Caliplus 300x250 NoFlam VitoLiv 468x60 GlucoLo