Help me Its a Java programming Write a static method that al
Help me! It\'s a Java programming.
Write a static method that allocates memory for numElems floats (where numElems is an int parameter) AND initializes each element to a float value (another parameter) to the power of its index (i.e., the index is the exponent), then return the array in a return statement (see the one-dim. array example).
Write another static method that displays the array backwards, BUT DON\'T CHANGE THE ARRAY (the ONLY parameter MUST be the array, nothing else)!
In main, declare an array of floats variable, call the first method passing 9 and 5.0 (assign to the main array variable), then call the 2nd method passing the array variable.
Upload the methods and main in one file.
* example of the first method: if numElems is 5 and the float parameter is 3.0, after you allocate the array, you will assign 1.0 to element 0, 3.0 to element 1, 9.0.0 (3. ^ 2) to element 2, 27.0 (3 ^ 3.0) to element 3, 81.0 (3 ^ 4.0) to element 4.
Solution
ArrayElementSquare.java
public class ArrayElementSquare {
public static void main(String[] args) {
float arr[];
arr = elementPower(9, 5.0f);
displayBackward(arr);
}
public static float[] elementPower(int numElems , float value){
float f[]= new float[numElems];
for(int i=0; i<numElems ;i++){
f[i] = (float) Math.pow(value, i);
}
return f;
}
public static void displayBackward(float f[]){
for(int i=f.length-1; i>=0; i--){
System.out.print(f[i]+\" \");
}
}
}
Output:
390625.0 78125.0 15625.0 3125.0 625.0 125.0 25.0 5.0 1.0
