?
Solved

Passing arrays in java

Posted on 2005-03-24
5
Medium Priority
?
2,780 Views
Last Modified: 2012-08-13
Here is my code which calculates some values. I have  problem with passing arrays to function and constructor. can any one fix it.
code..........
import java.io.*;
public class correlate
{
      double x[];
      double y[];

      public correlate (double a[],double b[],int n)
      {
            for (int i=0;i<n;i++)
                  {
                        x[i]=a[i];
                        y[i]=b[i];
                  }
      }
      public double SumOfProduct_X_Y();
      {
            double sumofproduct_x_y =0;
            for (int i=0;i<n;i++)
                  {
                   sumofproduct_x_y = sumofproduct + ( x[i] * y[i]);
                  }

            return (n * sumofproduct_x_y );
      }

      public double ProductOfSum_X_Y();
      {
            double sum_x = 0, sum_y =0, productofsum_x_y = 0;
            for (int i=0;i<n;i++)
                  {
                   sum_x = sum_x + x[i];
                   sum_y = sum_y + y[i];
                  }

            productofsum_x_y = (sum_x * sum_y);
            return productofsum_x_y;
      }

      public double SumOf_X_Squares();
      {
            double sumof_x_squares = 0;
            for (int i=0;i<n;i++)
                  {
                  sumof_x_squares = sumof_x_squares + (x[i]*x[i]) ;
                  }
            return  (n * sumof_x_squares);
      }
      public double SumOf_Y_Squares();
      {
            double sumof_y_squares = 0;
                  for (int i=0;i<n;i++)
                  {
                  sumof_y_squares = sumof_y_squares + (y[i]*y[i]) ;
                  }

            return (n * sumof_y_squares);
      }
      public double SquareOf_Sum_X();
      {
            double squareof_sum_x = 0;
            for (int i=0;i<n;i++)
                        {
                        squareof_sum_x = squareof_sum_x + (x[i]) ;
                        }
                  return squareof_sum_x;
      }
      public double SquareOf_Sum_Y();
    {
                  double squareof_sum_y = 0;
                  for (int i=0;i<n;i++)
                              {
                              squareof_sum_y = squareof_sum_y + (y[i]) ;
                              }
                  return squareof_sum_y;

      }

      public static void main(String args[])
      {
            double A,B,C,D,E,F;
            correlate c1 = new correlate();
            double a[] = { 12, 13, 14, 15};
            double b[] = { 23, 24, 25, 26};

            c1(a[],b[]);

            A = c1.SumOfProduct_X_Y();
            B = c1.ProductOfSum_X_Y();
            C = c1.SumOf_X_Squares();
            D = c1.SquareOf_Sum_X()
            E = c1.SumOf_Y_Squares();
            F = c1.SquareOf_Sum_Y()
            System.out.println("  ",A+B+C+D+E+F);
      }

}
0
Comment
Question by:sharath123in
  • 2
4 Comments
 
LVL 15

Expert Comment

by:aozarov
ID: 13627937
c1(a[],b[]) -> c1(a,b);
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 375 total points
ID: 13628499
Should be

c1(a, b, a.length);

But your ctor should be something like:

public correlate (double a[],double b[],int n)
{
      x = new double[n];
      y = new double[n];
      for (int i=0;i<n;i++)
      {
            x[i]=a[i];
            y[i]=b[i];
      }
}


0
 
LVL 37

Expert Comment

by:zzynx
ID: 13628848
CEHJ is right. This is why:

public class correlate
{
     double x[];     // <<< Here you only define x as being a (yet undefined) reference to a double array (x = null)
     double y[];     // <<< Here you only define x as being a (yet undefined) reference to a double array (y = null)

     Before you start using x and y you'll have to specify what they reference:

     public correlate (double a[],double b[],int n) {
        x = new double[n];     // <<<< Here you (1) create a new double array of length n in memory and (2) let x reference it
        y = new double[n];     // <<<< Here you (1) create a new double array of length n in memory and (2) let y reference it
                                           // From now on, you can safely use x and y (since they are no longer null)
        for (int i=0;i<n;i++) {
           x[i]=a[i];
           y[i]=b[i];
       }
     }


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13635387
:-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Don’ts and Dos are two important end products of software testing basics that a tester needs to regard. This article attempts to explain the principles of both.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question