Question: JAVA /** * It has two examples of methods for scaling a numeric array. * The task is to: * 1. Describe the difference between

JAVA

/**

* It has two examples of methods for scaling a numeric array.

* The task is to:

* 1. Describe the difference between the scale1 and scale2 methods in at most two sentences.

* 2. Identify which one accurately scales the array and explain why.

*/

public class ArrayScale {

public static void scale1(double[] data, double factor) {

for (double val : data)

val *= factor;

}

public static void scale2(double[] data, double factor) {

for (int j=0; j < data.length; j++)

data[j] *= factor;

}

/**

* Print method that uses StringBuilder to display every element of an array

* @param data

*/

public static void print(double[] data) {

StringBuilder sb = new StringBuilder();

for (double val : data)

sb.append(" " + val);

sb.append(" ");

System.out.println(sb);

}

public static void main(String[] args) {

double[] sample = {1.0, 2.0, 3.0, 4.0};

print(sample);

scale1(sample, 2);

print(sample);

scale2(sample, 3);

print(sample);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!