|
-
Dec 30th, 2002, 03:10 PM
#1
Thread Starter
New Member
is my calculation right ??
Hi I am learning java and I am doing a assignment, which I am stuck on, one of the calculations in it is the sum of XY as X and Y are 2 arrays is it. The total sum of each array I multiply ,as I have done in my code or is it each element of array X * Y . ie
ARRAY X ARRAY Y
2 2
3 5
4 6
5 3
my answer would be 15*16 but my mate says its 2*2 + 3*5 + 4*6 + 5*3
which is right as the calculation is
m = sum of XY minus (Sum of X *average Y) divided by
sum of Xsquared minus (sum of X * average of X)
here is my full code if any one can tell me if I am on the right track
import java.io.*;
class LinearLeast1
{ // start of new class
//defines the sum method
public static double SumA (double arr[])
{
double SumA =0; //sum of array
if (arr.length >=1)
{
for(int i=0; i< arr.length; i++)
{
SumA +=arr[i];
}
}
else
{
SumA =0; // insuficient data
}
return SumA;
}
//defines the avarage method
public static double AverageB (double arr[])
{
double SumA =0; //sum of array
double AverageB =0; // average of array
if (arr.length >=1)
{
for(int i=0; i< arr.length; i++)
{
SumA +=arr[i];
}
AverageB = SumA / arr.length;
}
else
{
AverageB =0; // insuficient data
}
return AverageB;
}
//defines array to the power of 2 method
public static double SumB (double arr[])
{
double SumB = 0; // sum of squares of array
double SumA =0; //sum of array
double Square =0; // array to the power of 2
if (arr.length >=2)
{
for(int i=0; i< arr.length; i++)
{
SumA +=arr[i];
SumB += arr[i] * arr[i];
}
Square =Math.sqrt((arr.length* SumB - SumA*SumA) / (arr.length*(arr.length-1)));
}
else
{
Square =0; // insuficient data
}
return SumB;
}
public static void main(String[]args)
{ //start of main method
//values for array x
double [] arrX ={-4.91,-3.84,-2.41,-2.62,-3.78,-0.52,-1.83,-2.01,0.28,1.08,
-0.94,0.59,0.69,3.04,1.01,3.60,4.53,5.13,4.43,4.12};
// values for array y
double [] arrY ={-8.18,-7.49,-7.11,-6.15,-5.62,-3.30,-2.05,-2.83,-1.16,0.52,
0.21,1.73,3.96,4.26,5.75,6.67,7.70,7.31,9.05,10.05};
double XY = LinearLeast1.SumA(arrX)*LinearLeast1.SumA(arrY);
double m = (XY-(SumA(arrX)*AverageB(arrY)))/ (SumB(arrX)-(SumA(arrX)*AverageB(arrX)));
System.out.println(" m = "+ m );
System.out.println(" array X * array Y = "+ XY );
System.out.println(" sum ofarray x "+ LinearLeast1.SumA(arrX));
System.out.println(" sum ofarray y "+ LinearLeast1.SumA(arrY));
System.out.println(" avarage of array x " + LinearLeast1.AverageB(arrX));
System.out.println(" avarage of array y " + LinearLeast1.AverageB(arrY));
System.out.println(" power of 2 array x " + LinearLeast1.SumB(arrX));
} //end of main method
} // end of class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|