Initialize an array by two functions. The first function should initialize the a
ID: 3675993 • Letter: I
Question
Initialize an array by two functions. The first function should initialize the array with same value. The second function should initialize the array in which each value is the square of the index. For example, array[i]=i*i; You must define the two functions with the same function name and the parameter in the two functions must be different. In other words, you are required to do the function overloading. You are also required to write a output function for the array. Grading scheme: the first initialize function -5 points; the second initialize function -5 points: the output function - 5 points; main function - 5 points.Explanation / Answer
package assignment;
public class FunctionOverride {
public static void main(String[] args) {
FunctionOverride fo = new FunctionOverride();
int[] array = new int[10];
System.out.println(" Array without calling initialization function");
fo.print(array);
fo.initialize(array);
System.out.println(" Array after calling first initialization function");
fo.print(array);
fo.initialize(array, true);
System.out.println(" Array after calling square initialization function");
fo.print(array);
}
public void initialize(int[] array){
for(int i = 0; i < array.length; i++)
array[i] = i;
}
public void initialize(int[] array, boolean withSquare){
for(int i = 0; i < array.length; i++)
if(withSquare)
array[i] = i * i;
else
array[i] = i;
}
public void print(int[] array) {
for(int i = 0; i < array.length; i++)
System.out.print(array[i]+" ");
System.out.println();
}
}
---output-----
Array without calling initialization function
0 0 0 0 0 0 0 0 0 0
Array after calling first initialization function
0 1 2 3 4 5 6 7 8 9
Array after calling square initialization function
0 1 4 9 16 25 36 49 64 81