Below is my code and information of how this code or project
Below is my code and information of how this code or project should work. I don\'t know why it does not work so please someone help me, but make sure explain as well. I\'ll appreciate it.
====================================================================================
Information:
For this project, you will develop a Java program that will read a file of numbers, and do some elementary statistical calculations.
Calculate the mean
Calculate the standard deviation
Your program should prompt the user for a filename, read data from the file, and use java methods to do the calculations.
See the sample program interaction below . . .
Submission:
You will submit your \".java\" file, as usual . . .
Input data: Your program will read a filename from the user, and then read data from the file.
The format of the file will consist of ascii (text) numbers. The first number will specify how many (data) numbers follow.
Example data file:
Output: Your program will perform the appropriate calculations (see below).
Scoring: The maximum possible score for the project will be 100 points. There will be up to 50 points for correct operation of your program, and up to 50 points for style, comments, etc.
Examples:
First example:
Second example:
====================================================================================
Code;
import java.util.Scanner;
import java.io.*;
public class NumberStatistics
{
public static void main(String args[]) throws FileNotFoundException
{
System.out.print(\"Please enter a file name: \");
Scanner console = new Scanner(System.in);
String filename = console.next();
Scanner reader = new Scanner (new File(filename));
int length = reader.nextInt();
double[] numbers = new double[length];
for(int i=0; i<length; i++)
{
numbers[i] = reader.nextDouble();
}
double sum = 0;
for (int i=0; i<length; i++)
{
sum = numbers[i] + length;
}
double mean = sum/length;
System.out.println(\"The mean of the numbers is: \" + sum/length);
double[] temp = new double[length];
{
for (int i = 0; i < length; i++)
temp[i] = Math.pow(numbers[i] - mean, 2);
}
for (int i = 0; i < length; i++)
{
System.out.println(temp[i]);
}
double SDsum= 0;
{
for (int i = 0; i < length; i++)
{
SDsum = temp[i] + SDsum;
}
double SD = Math.sqrt(SDsum/length);
System.out.println(\"The Standard Deviation is: \");
reader.close();
}
}
}
Solution
Hi,
I have modified the code and highlighted the code changes below.
NumberStatistics.java
import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.Scanner;
public class NumberStatistics
{
public static void main(String args[]) throws FileNotFoundException
{
System.out.print(\"Please enter a file name: \");
Scanner console = new Scanner(System.in);
String filename = console.next();
Scanner reader = new Scanner (new File(filename));
int length = reader.nextInt();
double[] numbers = new double[length];
for(int i=0; i<length; i++)
{
numbers[i] = reader.nextDouble();
}
double sum = 0;
for (int i=0; i<length; i++)
{
sum = numbers[i] + sum;
}
double mean = sum/length;
System.out.println(\"The mean of the numbers is: \" + sum/length);
double[] temp = new double[length];
{
for (int i = 0; i < length; i++)
temp[i] = Math.pow(numbers[i] - mean, 2);
}
for (int i = 0; i < length; i++)
{
if(i % 10 == 0){
System.out.println();
}
System.out.print(numbers[i]+\" \");
}
System.out.println();
double SDsum= 0;
{
for (int i = 0; i < length; i++)
{
SDsum = temp[i] + SDsum;
}
double SD = Math.sqrt(SDsum/length);
System.out.println();
DecimalFormat df = new DecimalFormat(\"0.00\");
System.out.println(\"The Standard Deviation is: \"+df.format(SD));
reader.close();
}
}
}
OUtput:
Please enter a file name: D:\\\ umbers.txt
The mean of the numbers is: 10.625
1.1 2.0 3.3 4.0 5.5 6.0 7.0 8.5 9.0 10.0
11.0 12.3 13.0 14.0 15.5 16.1 17.0 18.0 19.2 20.0
The Standard Deviation is: 5.75
numbers.txt
20
1.1 2 3.3 4 5.5 6 7 8.5 9 10.0
11 12.3 13 14 15.5 16.1 17 18 19.2 20.0


