I have to write a program in java to compute the standard deviation and the mean. I am also to write a test program that will prompt the user to enter 10 numbers and display the mean and standard deviation. I have stared on the program and so far this is all I have
import java.util.Scanner;
public class Statistics {
//declaring creating array of 10
double[] list = new double[10];
//Scanner for input
Scanner input = new Scanner(System.in); {
for (int i = 0; i < 1; i++) {
System.out.print("Enter 10 numbers: ");
list[i] = input.nextDouble();
//Print
System.out.println("The mean is " + mean(list));
System.out.println("The standard deviation is " + deviation(list));
}
}
/** Method that compute the deviation of double values */
public static double deviation (double[] x) {
double mean = mean(x);
double squareSum = 0;
for(int i = 0; i < x.length; i++) {
squareSum += Math.pow(x[i]-mean, 2);
}
return Math.sqrt(squareSum) / (x.length - 1);
}
/** Method that compute the mean of double values */
public static double mean(double[] x) {
double sum = 0;
for (int i = 0; i < x.length; i++)
sum+= x[i];
return sum /x.length;
}
//printing the array
public static void printArray(double[] x) {
for (int i = 0; i < x.length; i++) {
System.out.println(x[i] + " ");
System.out.println();
}
}
}
above is the prompt and the mean and standard deviation formula
the test program is below.
public class TestStatics {
/**
* Test the statistics methods for Assignment #1.
* @param args
*/
public static void main(String[] args) {
// how much of a difference between expected and computed
// will be allowed.
double tolerance = 1e-10;
// first, test on an array of mixed numbers
double[] tans = genData1();
double expectedMean = 0.05848465036; // 0.05848465036620792;
double computedMean = Statistics.mean(tans);
double diffMean = Math.abs(expectedMean-computedMean);
System.out.println("The computed mean is " + computedMean +
" and the expected mean is " + expectedMean +
" for a difference of " + diffMean
);
if(diffMean > tolerance) {
System.out.println("Test of mean on genData1 failed");
} else {
System.out.println("Test of mean on genData1 passed");
}
double expectedDeviation = 18.47695685606; // 18.47695685606356
double computedeDeviation = Statistics.deviation(tans);
double diffDeviation = Math.abs(expectedDeviation-computedeDeviation);
System.out.println("The computed deviation is " + computedeDeviation +
" and the expected deviation is " + expectedDeviation +
" for a difference of " + diffDeviation
);
if(diffDeviation > tolerance) {
System.out.println("Test of deviation on genData1 failed");
} else {
System.out.println("Test of deviation on genData1 passed");
}
// now test on an empty array
double [] noData = new double[0];
computedMean = Statistics.mean(noData);
if(!Double.isNaN(computedMean)) {
System.out.println("Test of mean on empty array failed");
} else {
System.out.println("Test of mean on empty array passed");
}
expectedDeviation = 0;
computedeDeviation = Statistics.deviation(noData);
diffDeviation = Math.abs(expectedDeviation-computedeDeviation);
System.out.println("The computed deviation is " + computedeDeviation +
" and the expected deviation is " + expectedDeviation +
" for a difference of " + diffDeviation
);
if(diffDeviation > tolerance) {
System.out.println("Test of deviation on empty array failed");
} else {
System.out.println("Test of deviation on empty array passed");
}
/*
* Comment:
* In the case of the deviation, the running total ends up as 0 and
* that is divided by -1.
*/
}
private static double[] genData1() {
int start = 1234;
int end = 2345;
double[] data = new double[end-start];
for(int i = start; i < end; i++ ) {
data[i-start] = Math.tan(i);
}
return data;
}
}
when i run my program it just gives me the information in the test program but never allows me to input 10 number to check it against. can you please help me. I am sorry if i am all over the place. I am fairly new to java.
Thanks in advance for your help.
LoyalOne2 0 Newbie Poster
NormR1 563 Posting Sage Team Colleague
LoyalOne2 0 Newbie Poster
NormR1 563 Posting Sage Team Colleague
LoyalOne2 0 Newbie Poster
NormR1 563 Posting Sage Team Colleague
LoyalOne2 0 Newbie Poster
Starstreak 13 Light Poster
LoyalOne2 0 Newbie Poster
NormR1 563 Posting Sage Team Colleague
LoyalOne2 0 Newbie Poster
NormR1 563 Posting Sage Team Colleague
LoyalOne2 0 Newbie Poster
LoyalOne2 0 Newbie Poster
NormR1 563 Posting Sage Team Colleague
LoyalOne2 0 Newbie Poster
NormR1 563 Posting Sage Team Colleague
Nutster 58 Newbie Poster
Starstreak 13 Light Poster
Nutster 58 Newbie Poster
LoyalOne2 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.