57.5k views
18 votes
how do you call the squares method to compute the first five squares and store the result in an array numbers

1 Answer

8 votes

Answer:

The method can be called using:

int[] returnarray = Squares();

Where returnarray represents the name of the array

Step-by-step explanation:

To answer this, I will make an assumption that the Square method is an int array method (i.e. it returns array).

So, to call the method from main, an array has to be declared to get the values:

int[] returnarray = Squares();

Then an iteration is created to iterate through the array in order to print the array elements.

for (int i = 0; i < returnarray.length; i++)

System.out.print(returnarray[i]+ " ");

-------------------------------------------------------------------------------------------

The answer ends here

-------------------------------------------------------------------------------------------

As an addition, the complete program that includes the method and the main is:

public class Main{

public static int [] Squares(){

int [] arr = new int[5];

for(int i = 1;i<6;i++){

arr[i-1] = i*i;

}

return arr;

}

public static void main(String[] args) {

int[] returnarray = Squares();

for (int i = 0; i < returnarray.length; i++)

System.out.print(returnarray[i]+ " ");

}

}

User Jbryer
by
5.1k points