Hi,
i have a problem here , i have made a class which has three methods , first method is to get size of the array and assign a random number to every element of array. Second method is to just print the whole array , while the last method needs to add up all the elements in array. When i run the code i am not getting desired result and i instead get all zero's for my array element hence getting sum of zero. Here is my code , the actual class and main program, if anyone can help , i will b very tthankful. : )
/****Class****/
class ArrayTest {
public int[] ArraySize(int size)
{
int[] Array1= new int[size];
for(size = 0;size <=(size-1);size++)
{
Array1[size]=(int)Math.random();
}
return Array1;
}
public void printArray(int[] array)
{
System.out.print("\n[");
for(int size = 0; size<array.length; size++)
{
System.out.print(" "+array[size]+",");
}
System.out.print("]\n");
}
public int addArray (int[] array)
{
int sum=0;
for(int size =0;size<array.length;size++)
{
sum = sum + array[size];
}
System.out.print("\nThe sum of the array is : "+sum);
return sum;
}
}
/******Main program in which i used class**************/
public class javalabsheets {
public static void main(String[] args) {
int[] array;
ArrayTest test= new ArrayTest();
array = test.ArraySize(10);
test.printArray(array);
test.addArray(array);
}
}
And the output is :
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,]
The sum of the array is : 0
Process completed.