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.

Can you edit the code and give it proper indentations and formatting? Nested statements should be indented 3-4 spaces and should not all start in the first column. Proper formatting makes it a lot easier to read and understand code.

the test program but never allows me to input 10 number

How many numbers does it read a number? What logic in the code controls how many times it will try to read a number from the user? Is that code working correctly?

Nto sure of what your response was getting at could you please explain more because at one point when i ran my program it allowed me to input the ten numbers but it gave me the wrong output. Aleo the test program i can't touch.

the test program i can't touch.

What is the code that is posted? What part of it can you change? Why isn't the code properly formatted? Please format the part of the code that you are allowed to change.

WHat happens when you execute the code? How many numbers can you enter?
Can you copy and paste here the console from when you execute the code?

On windows:To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

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();

                    }

                }

            }


       This is the part of the program that i wrothe the other is a test program. This code is suppose to promo user to enter 10 double nunbers and be output the standard deviation and the mean. it is supposed to be tested against the other code but i only get the other code output and it never prompts me sorry if i am everywhere.

You left off the console contents from when you execute the program so I can see what it does when you execute it.

why do i have to input the console information it. it just output the standard deviation -0 etc
you can just copy and paste it into your NetBean.

In your test program, try instantiating the statistics class like so:

Statistics stats = new Statistics();

Then you can refer to the methods using 'stats.xxx'

Starstreak, thanks for being helpful and pointing me in a direction, but I am unable to touch the test program. is it something in my original statics program that is wrong.

Please post the console from when you execute the program that shows what command you entered to execute the program, that shows what the program printed on the screen and that shows what you typed in. I don't know how you are executing the program. I don't know what command you are entering to start the program or what the program is printing out. I need to see that.

I still don't know why you can't just place the code in your NetBean and see what comes out. Anyways NormR1 you are not making sense to me, sorry.

I don't use netbeans. I need to see the console from when you execute the program so I know what you are doing. What is the problem with copying the console and pasting it here?

What program are you executing? What does that program write on the console?
You have posted code for two classes. Which one are you executing? If you copy the console from when you execute the program I would know which one you are executing.

I am excuting the program Statistic and The test is a program that is the main program. Which mean I am to use both. The test program is need so that the method program has something to run from you can't have a program with no main method so the Test program is my main program.

This is what i get in the console

The computed mean is 0.05848465036620792 and the expected mean is 0.05848465036 for a difference of 6.2079161256001214E-12
Test of mean on genData1 passed
Enter 10 numbers: 1.0 2.0
The mean is 0.1
Enter 10 numbers:

What is the problem with posting the console from when you execute the program?
If I can't see what you are doing how do I recommend any changes?

I posted it above. I am just wondering why it does not give me the deviation and the mean correctly.

try debugging the code by adding some println methods to the methods that the testing class's main() method calls to show what is happening in the code.

I think the problem is in line 11 of your revised code. Your for loop is for (int i = 0; i < 1; i++), when it needs to be for (int i = 0; i < 10; ++i). The value in the condition is 10 not the 1 you used. Also, I like ++i over i++ because of the way the side-effecting is handled, even when you are not using the side-effect.

I saw the < 1 in the for loop as well, but assumed that it was intended just to restrict the input to 1 for testing purposes ( I do this as well). He should put a //comment as reminder.

@Starstreak: More inline comments, especially where things have been changed for testing purposes, are pretty much always helpful.

@LoyalOne2: You are calculating the mean multiple times; try ordering things to reduce the number of times something has to happen. I recommend calculating the mean before you calculate the standard deviation. This way you can use the value you calculate as the mean in the calculation of the standard deviation without having to determine the mean again. Even though you are calling the mean(array) function, that function basically has to do the same thing that you are doing in your function.

Just as a point of clarity: you may want to avoid naming variables with the same name as functions you intend to call. It makes it more challenging to follow things and may, in some cases, prevent your program from compiling.

Thanks Nutser. I ended up getting the program right thank you so much your method of doingthe mean first help a lot and it was a error in the line you reference to actually i did not need it. and i did not need to prompt the user to enter value I just had to crdate the mean and deviation to test against the other program. They both passed once i went back and corrected everything like you said thanks soooo much.

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.