Question
The goal of the program is to compute the position-wise squares of the values of an array. Complete that program, by defining an array_square function,
The goal of the program is to compute the position-wise squares of the values of an array. Complete that program, by defining an array_square function, that satisfies the following specs: Function arraySquare takes one argument, called A, that is an array of double numbers. The function should return an array called result, with length equal to the length of A, such that the value at position i of result is square of the value at position i of A.
public class hw5_task3 {
public static void main(String[] args) {
double[] a = {3.2, 2.1, 5.3, 8.0, 4.9, 5.7};
double[] b = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6};
double[] result = arraySquare(a);
printDoubleArray("a", a);
printDoubleArray("arraySquare(a)", result);
System.out.printf(" ");
result = arraySquare(b);
printDoubleArray("b", b);
printDoubleArray("arraySquare(b)", result); }
public static void printDoubleArray(String name, double[] a) {
System.out.printf("%20s: ", name); if (a == null) {
System.out.printf("Null array! ");
return; }
for (int i = 0; i < a.length; i++) {
System.out.printf("%7.2f", a[i]); }
System.out.printf(" "); } }
Write a file called hw5_task4.java. It should do the same as hw5_task3.java, but must hold the data in an ArrayList of Double: ArrayList instead ofdouble[]. Note that you must match the output perfectly: when printing the ArrayList you must print the elements individually, and NOT with System.out.println(...). The complete program should produce this output:
a: 3.20 2.10 5.30 8.00 4.90 5.70
arraySquare(a): 10.24 4.41 28.09 64.00 24.01 32.49
b: 1.10 2.20 3.30 4.40 5.50 6.60
arraySquare(b): 1.21 4.84 10.89 19.36 30.25 43.56
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started