System.out.println("Enter your value(s).");
for (int n = 0; n < array.length; n++)
{
array[n] = console.nextInt();
}
this is going to ask the user to enter the values only once, but expect them to enter it array.length times.
for (int n = 0; n < array.length; n++)
{
System.out.println("Enter your value."); // since it's only asking for one at a time, enter change it
// to singular. True, they could enter them all at once with a space, but still.
array[n] = console.nextInt();
}
this asks them to enter values array.length times and expects array.length inputs.
public Standard_Deviation(int size) //constructor
{
System.out.println("How many values?"); //prompts input from the user.
size = console.nextInt(); //stores the input
System.out.println();
array = new int[size]; //declared array
System.out.println("Enter your value(s)."); //prompts more input.
for (int n = 0; n < array.length; n++)
{
array[n] = console.nextInt(); //stores input.
}
System.out.println();
}
Why would a constructor ask for the size value? Couldn't your test program do that instead?
public Standard_Deviation(int size) //constructor
{
size = this.size; // hopefully this statement doesn't make it equal to 0.
array = new int[size]; //declared array
System.out.println("Enter your value(s)."); //prompts more input.
for (int n = 0; n < array.length; n++)
{
array[n] = console.nextInt(); //stores input.
}
System.out.println();
}
import java.lang.*;
public class Standard_Deviation_Driver
{
public static void main(String[] args)
{
int size = 0;
System.out.println("How many values?"); //prompts input from the user.
size = console.nextInt(); //stores the input
System.out.println();
Standard_Deviation sd = new Standard_Deviation(size); // unless you wanted a 2, but the way it is
// now will work with constructor and every time you need to use the value size.
double a = sd.getSums();
double b = sd.getMean();
double c = sd.getSquare();
double d = sd.getSquareRoot();
double e = sd.getDivide();
System.out.println("Sum: " + a);
System.out.println("Mean: " + b);
System.out.println("Squared: " + c);
System.out.println();
System.out.println("Standard deviation: " + d);
}
}
public double getSums() //getSums method.
{
for (int n = 0; n < array.length; n++)
{
sums += array[n]; //adds data.
}
return(sums);
I think this will go through the loop and only return the value it has at the end of the loop.
public double getMean() //getMean method.
{
mean = sums / array[size]; //divides data to get the average.
return(mean);
}
this is dividing sums, or getSums() if you changed it, by the value in the index size of your array.
However, arrays start at 0, so your max index is array[size-1]. What you're doing right now is dividing by the value in an index that isn't there!
public double getDivide() //getDivide method.
{
divided = squared / array[size]; //divides data.
return(divided);
}
Again, that index doesn't exist.
I'm not sure if you're hoping to divide by the element at array[size -1 ] or if you're hoping to get the value
divided by array.length, so I can't be more specific in that area.
public double getSquare() //getSquare method.
{
for (int n = 0; n < array.length; n++)
{
subtracted = array[n] - mean; //subtracts mean from the array
squared = subtracted * subtracted; //multiplys results.
}
return(squared);
}
n = 0;
subtracted = array[0] - mean;
squared = Math.pow(subtracted,2);
n = 1;
subtracted = array[1] - mean;
squared = subtracted * subtracted; // same as Math.pow(subtracted, 2)
n = 2;
subtracted = array[1] - mean;
squared = Math.pow(subtracted,2);
n = array.length - 1;
subtracted = array[array.length - 1] - mean;
squared = Math.pow(subtracted, 2);
end loop
return value for last squared value calculated.
I don't think it's saving your values before that.
if you're hoping to get all the values, you could do this:
public void getSquare() //getSquare method.
{
for (int n = 0; n < array.length; n++)
{
subtracted = array[n] - mean; //subtracts mean from the array
squared = subtracted * subtracted; //multiplys results.
System.out.println(squared);
}
}
However, if you do this, you'll have to update your method call in Driver and anywhere else you might have called getSquare().
I'm not sure what the code below will do:
public double getSquare() //getSquare method.
{
for (int n = 0; n < array.length; n++)
{
subtracted = array[n] - mean; //subtracts mean from the array
squared = subtracted * subtracted; //multiplys results.
return(squared);
}
}
but it's very possible your return value will be lost when the loop ends so I wouldn't recommend writing it that way.
Also,
for (int n = 0; n < array.length; n++)
{
System.out.println("Enter your value."); // since it's only asking for one at a time, enter change it
// to singular. True, they could enter them all at once with a space, but still.
array[n] = console.nextInt();
}
should be in the Driver class.
If you made the array public, or at least default (visible to all in package but none outside package), put two classes inside same package.
default looks like this
int array[];
protected is for classes that are in the same package, that can access protected as well I've heard, or for classes that extend, or inherit methods from other classes.
You could do this:
import java.lang.*;
public class Standard_Deviation_Driver
{
public static void main(String[] args)
{
int size = 0;
System.out.println("How many values?"); //prompts input from the user.
size = console.nextInt(); //stores the input
System.out.println();
Standard_Deviation sd = new Standard_Deviation(size); // unless you wanted a 2, but the way it is
// now will work with constructor and every time you need to use the value size.
int [] array2 = new int[sd.array.length];
for (int n = 0; n < array2.length; n++)
{
System.out.println("Enter your value."); // since it's only asking for one at a time, enter change it
// to singular. True, they could enter them all at once with a space, but still.
array2[n] = console.nextInt();
array2[n] = sd.array[n];
System.out.println();
}
// that may work. If not, you still need to have that line asking for each of the values in the Driver class. //You shouldn't be asking for input directly from the user using console.next Whatever or //Whatever.parseWhatever.
double a = sd.getSums();
double b = sd.getMean();
double c = sd.getSquare();
double d = sd.getSquareRoot();
double e = sd.getDivide();
System.out.println("Sum: " + a);
System.out.println("Mean: " + b);
System.out.println("Squared: " + c);
System.out.println();
System.out.println("Standard deviation: " + d);
}
}
Also, when using the variables in the regular, non-Driver Standard_Deviation class, try to use getValue() instead of value.
Like this:
mean = getSums() / array[size]; //divides data to get the average.
return(mean);
divided = getSquare() / array[size]; //divides data.
return(divided);
Also, if you get the message: non-static method cannot be referenced from a static context, or something like that, change your methods to static in first class.