I see a couple of things to point out here.
public test1 (int x [], int y [])
{
int [] a = x;
int [] b = y;
}
First of all, remember that each test1
object - that is, each instance of your test1
class - has variables named a
and b
. I'm guessing that in the constructor, you want to take the arrays x
and y
which were passed as parameters and store them into a
and b
of the object. To do that, all you have to do is write
a = x;
b = y;
You don't have to write int[]
again, not when you just want to access an existing array-type variable. You only write that when you're creating a new array-type variable. In this case, when Java sees that you've written int[] a
in the constructor, it thinks you want to create yet another array-type variable named a
, separate from the one in the test
instance, and that's the one that gets set equal to x
. The thing is, that local variable gets lost at the end of the constructor. So you're left with a test1
instance that has variables a
and b
that still refer to nothing, i.e. they're null
.
By the way, since you're going to be using the array final23
later on, you should initialize it. Right now, it refers to null
because you never set it to equal anything else. You'll need to create a new array and store it in that variable in order to be able to use it later on. So put this line in your constructor:
final23 = new int[Math.min(a.length, b.length)];
That creates the new array with a length equal to the shorter of the two arrays passed in.
Moving on:
public int [] sum(int [] x, int[] y)
{
int [] a = x;
int [] b = y;
In this bit of code, you have the same issue as in the constructor: you create two new array-type variables a
and b
that get used instead of the ones in the test1
object. I don't think that's what you meant to do. So I'd say get rid of those last two lines entirely.
There's another problem, though: if you think about it, you still have two arrays stored in the test1
object. Assuming you've fixed your constructor, those are the same two arrays that were passed to the constructor. And now you're getting two new arrays under the names x
and y
. So you have four arrays total. Which ones did you want to sum up? I'm guessing that you meant to sum the two arrays that were passed to the constructor. In that case, your sum
method doesn't need to - and shouldn't - accept more arrays as parameters. Get rid of the parameters x
and y
, so your sum
method just looks like
public int [] sum()
{
Now you have to change the rest of that method to use a
and b
, starting with the for
loop. Change its opening line to this:
for (int i = 0; i < Math.min(a.length, b.length); i++)
{
I notice you were wondering how to get your sum
method to take an instance of test1
. Well, in a way it actually does. There's a special hidden parameter passed to all methods (except static
ones) that contains the object the method was called on - in fact, using your main program as an example you could kind of think of X.sum(k,l);
as actually calling test1.sum(X,k,l);
, where X
is the special hidden parameter. You can access it inside the method using the name this
(so you could write this.a
instead of just a
), but Java is generally smart enough to do that for you.
In the body of the for
loop, you have another problem. What you want to do is add up corresponding elements of the arrays, i.e. a[0] + b[0]
goes into final23[0]
, a[1] + b[1]
goes into final23[1]
, and so on. But inside the for loop, you only ever add up element 0 of each array. You need to use the loop index variable i
, because that runs through all the values from 0
to the length of the shorter array minus 1.
final23 [i] = a[i] + b[i];
}
return final23;
}
So the first time the loop runs, i
will be 0
, and you'll set final23[0] = a[0] + b[0]
. The next time it runs, i
will be 1
, and you'll set final23[1] = a[1] + b[1]
. And so on.
The same problem occurs in your print
method. Each time through the loop, you always print out final23[0]
, when you really should be printing out final23[i]
because i
changes each time you go through the loop. Change it to
public void print()
{
for (int i = 0; i < final23.length; i++)
{
System.out.println(final23[i]);
}
}
At this point your program should be working, I think, but there are still some improvements you could make to its design. For one thing, every time you create an object of test1
, you know you're immediately going to call sum
on it. So why not just put the summing-up code right into the constructor? That way you know that the sum will be computed right when you create the object, and you don't have to call sum
explicitly.
Of course, once you do that, you'll have no way to access the array final23
from your main class - except that if you want to print it, you can call the print
method. But what if you want to write a main class that, say, adds up two arrays, and then adds the result to a third array? It'd be nice to have a way to get the result from the test1
instance. So you can add an accessor method, possibly named getFinal23
, that just returns the sum array final23
.
In practice, this operation of adding two arrays would probably be implemented as a static method. So if you want, you could try starting over, and writing it as a static method. (Remember that a static method is one which doesn't receive a special hidden parameter) Inside the static method, you'd have to create the final23
array, go through the loop to compute the sums, and then return the array you created. You'll need to enclose the static method in a class, of course, but that class doesn't have to have a constructor since you never really use it for anything. It'd look something like this:
public class SumClass { // pun intended ;-)
public static int[] sum(int[] x, int[] y) {
// you fill in this part
}
}