Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The goal of the program is to compute the position-wise squares of the values of

ID: 3684659 • Letter: T

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, 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

Explanation / Answer

hw5_task3.java

package com.chegg.test;

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);
   }

   private static double[] arraySquare(double[] array) {
       double[] newArray = new double[array.length];
       for(int i=0;i<array.length;i++)
       {
           newArray[i]=array[i]*array[i];
       }
       return newArray;
   }

   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(" ");
   }
}

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

hw5_task4.java

package com.chegg.test;

import java.util.ArrayList;
import java.util.Arrays;

public class hw5_task4 {
   public static void main(String[] args) {
       ArrayList<Double> a = new ArrayList<Double>(Arrays.asList(3.2, 2.1,
               5.3, 8.0, 4.9, 5.7));
       ArrayList<Double> b = new ArrayList<Double>(Arrays.asList(1.1, 2.2,
               3.3, 4.4, 5.5, 6.6));
       ArrayList<Double> result = arraySquare(a);
       printDoubleArray("a", a);
       printDoubleArray("arraySquare(a)", result);
       System.out.printf(" ");
       result = arraySquare(b);
       printDoubleArray("b", b);
       printDoubleArray("arraySquare(b)", result);
   }

   private static ArrayList<Double> arraySquare(ArrayList<Double> a) {
       ArrayList<Double> newArray = new ArrayList<Double>(a.size());
       for (int i = 0; i < a.size(); i++) {
           newArray.add(a.get(i) * a.get(i));
       }
       return newArray;
   }

   public static void printDoubleArray(String name, ArrayList<Double> b) {
       System.out.printf("%20s: ", name);
       if (b == null) {
           System.out.printf("Null array! ");
           return;
       }
       for (int i = 0; i < b.size(); i++) {
           System.out.printf("%7.2f", b.get(i));
       }
       System.out.printf(" ");
   }
}

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

Note:Could you please clearly mentioned the meaning of the following line i didn't understand it.

when printing the ArrayList you must print the elements individually, and NOT with System.out.println(...)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote