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