PLEASE MAKE ALL 6 OF THESE METHODS AS INSTRUCTED!!! Write a Java class with meth
ID: 3889390 • 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.
RULES
(Use Array Lists of capital-D Doubles instead of arrays of doubles.)
(In the method that takes user input, ask the user how many Doubles should be in the list, take input for that number of Doubles, and add the values to the list.)
Explanation / Answer
package org.students;
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayListOfDoubles {
public static void main(String[] args) {
// Creating an ArrayList which hold Double values
ArrayList < Double > arl = null;
//calling the methods
arl = createArray(arl);
print(arl);
calSqrt(arl);
replaceValueExceed500(arl);
print(arl);
arl = reciprocal(arl);
print(arl);
}
//Method which will display the elements in the ArrayList
private static void print(ArrayList < Double > arl) {
System.out.println(" :: Displaying the Elements in the ArrayList ::");
for (int i = 0; i < arl.size(); i++) {
System.out.println(arl.get(i));
}
}
//Method which will replace each elements with its reciprocal in the ArrayList
private static ArrayList < Double > reciprocal(ArrayList < Double > arl) {
for (int i = 0; i < arl.size(); i++) {
arl.set(i, new Double((double) 1 / (double) arl.get(i)));
}
return arl;
}
//Method which will replace each element in the ArrayList which exceed 500 with 500
private static void replaceValueExceed500(ArrayList < Double > arl) {
for (int i = 0; i < arl.size(); i++) {
if (arl.get(i) > 500) {
arl.set(i, new Double(500));
}
}
}
//Method which will display the square root of each element in the ArrayList
private static void calSqrt(ArrayList < Double > arl) {
System.out.println(" :: Displaying the Square root of each value in the array ::");
for (int i = 0; i < arl.size(); i++) {
System.out.printf("%.2f ", Math.sqrt(arl.get(i)));
}
}
//Method which will create an ArrayList and getting the inputs entered by the user
private static ArrayList < Double > createArray(ArrayList < Double > arl) {
int no;
double val;
/* Creating an Scanner class object which is used
* to get the inputs entered by the user
*/
Scanner sc = new Scanner(System.in);
System.out.print("How many doubles you want to enter :");
no = sc.nextInt();
arl = new ArrayList < Double > (no);
for (int i = 0; i < no; i++) {
System.out.print("Enter the value#" + (i + 1) + ":");
val = sc.nextDouble();
arl.add(val);
}
return arl;
}
}
______________________
OUTPUT:
How many doubles you want to enter :10
Enter the value#1:100
Enter the value#2:900
Enter the value#3:230
Enter the value#4:450
Enter the value#5:890
Enter the value#6:500
Enter the value#7:210
Enter the value#8:794
Enter the value#9:234
Enter the value#10:122
:: Displaying the Elements in the ArrayList ::
100.0
900.0
230.0
450.0
890.0
500.0
210.0
794.0
234.0
122.0
:: Displaying the Square root of each value in the array ::
10.00
30.00
15.17
21.21
29.83
22.36
14.49
28.18
15.30
11.05
:: Displaying the Elements in the ArrayList ::
100.0
500.0
230.0
450.0
500.0
500.0
210.0
500.0
234.0
122.0
:: Displaying the Elements in the ArrayList ::
0.01
0.002
0.004347826086956522
0.0022222222222222222
0.002
0.002
0.004761904761904762
0.002
0.004273504273504274
0.00819672131147541
________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.