Create a class containing a method name createArray and a ma

Create a class containing a method name createArray and a main method. The method createArray creates an array where each element contains the square of its index. The size of the array is determined by the method’s parameter. createArray then returns this array.
The main methods calls createArray with the right parameter to get the squares of the numbers from 0 to 12, it then prints the values of the array.

The output should be similar to this:

The square of 0 is 0 The square of 1 is 1 The square of 2 is The square of 3 is 9 The square of 4 is 16 The square of 5 is 25 The square of 6 is 36 The square of 7 is 49 The square of 8 is 64 The square of 9 is 81 The square of 10 is: 100 The square of 11 is: 121 The square of 12 is 144

Solution

CreateArrayTest.java


import java.util.Scanner;

public class CreateArrayTest {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println(\"Enter the size of an array: \");
       int n = scan.nextInt();
       createArray(n);
   }
   public static void createArray(int size){
       int a[] = new int[size+1];
       for(int i=0; i<=size; i++){
           a[i] = i * i;
       }
       for(int i=0; i<=size; i++){
           System.out.println(\"The square of \"+i+\" is: \"+a[i]);
       }
   }

}

Output:

Enter the size of an array:
12
The square of 0 is: 0
The square of 1 is: 1
The square of 2 is: 4
The square of 3 is: 9
The square of 4 is: 16
The square of 5 is: 25
The square of 6 is: 36
The square of 7 is: 49
The square of 8 is: 64
The square of 9 is: 81
The square of 10 is: 100
The square of 11 is: 121
The square of 12 is: 144

Create a class containing a method name createArray and a main method. The method createArray creates an array where each element contains the square of its ind

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site