PLEASE MAKE ALL 6 OF THESE METHODS AS INSTRUCTED!!! Write a Java class with meth
ID: 2246882 • Letter: P
Question
PLEASE MAKE ALL 6 OF THESE METHODS AS INSTRUCTED!!!
Write a Java class with methods that perform various operations on an array of doubles.
Follow the instructions carefully. In particular, write methods where directed; do not stuff main() with code that should be elsewhere.
Write a method that creates an array of ten doubles, initializes it with ten values taken from console input (ie, a Scanner from System.in), and returns a reference to the array.
Write a method that uses Math.sqrt() to find the square root of each value in an array of doubles and shows each result. This method should not change the values in the array.
Write a method that replaces any value exceeding 500 in an array of doubles with the value 500. This method should be void; in other words it should not return anything. Be sure you understand how array references are sent to methods.
Write a method that copies an array of doubles, replaces each value with its reciprocal (that is, replaces x with 1/x) and returns the new array. Be sure you understand the difference between copying an array and copying an array reference, or you will not do this part correctly.
Write a method that prints out all the values in an array of doubles
main() should call the input method, then send the array to the print method, then run each of the other methods, using the print method to print the current values after each method is done. After running the method that creates a new array, print out both the original array and the one returned by the method.
Explanation / Answer
/**
*
* @author Sam
*/
public class Standalone {
public static double[] method1() {
double[] arr = new double[10];
Scanner s = new Scanner(System.in);
System.out.println("Enter 10 elements");
for (int i = 0; i < 10; i++)
arr[i] = s.nextDouble();
return arr;
}
public static void method2(double[] arr) {
for (int i = 0; i < arr.length; i++)
System.out.println(Math.sqrt(arr[i]));
}
public static void method3(double[] arr) {
for (int i = 0; i < arr.length; i++)
if (arr[i] > 500)
arr[i] = 500;
}
public static double[] method4(double[] arr) {
double[] arr1 = new double[arr.length];
for (int i = 0; i < arr.length; i++)
arr1[i] = arr[i];
for (int i = 0; i < arr1.length; i++)
arr1[i] = 1/arr1[i];
return arr1;
}
public static void method5(double[] arr) {
for (int i = 0; i < arr.length; i++)
System.out.println(arr[i]);
}
public static void main(String[] args) {
double[] x = method1();
method5(x);
method2(x);
method5(x);
method3(x);
method5(x);
double[] newX = method4(x);
System.out.println(""OLD "");
System.out.println(""New "");
method5(newX);
}
}
this will help you....
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.