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

Write a Java class with methods that perform various operations on an array of d

ID: 3784459 • Letter: W

Question

Write a Java class with methods that perform various operations on an array of doubles.

1)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.

2)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.

3)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.

4)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.

5)Write a method that prints out all the values in an array of doubles

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

//Array.java


import java.util.Arrays;
import java.util.Scanner;

public class Array
{


   public static void main(String[] args)
   {
       // TODO Auto-generated method stub
       double arr[] = input();
       System.out.println("Input Array Elements: ");
       printArray(arr);
       getSquareRoot(arr);
       System.out.println("After Square root method called:");
       printArray(arr);
       replaceValue(arr);
       System.out.println("After replace method called:");
       printArray(arr);
       double arr1[] = copyArray(arr);
       System.out.println("Original Array:");
       printArray(arr);
       System.out.println("New Array:");
       printArray(arr1);
    
   }

   public static double[] input()
   {
       double arr[] = new double[10];
       Scanner scan = new Scanner(System.in);
       for(int i=0; i<arr.length; i++)
       {
           System.out.println("Enter Element: ");
           arr[i] = scan.nextDouble();
       }
       return arr;
   }

   public static void getSquareRoot(double arr[])
   {
       for(int i=0; i<arr.length; i++)
       {
           System.out.println("Square Root of "+arr[i]+ " is " + Math.sqrt(arr[i]));
       }
   }
   public static void replaceValue(double arr[])
   {
       for(int i=0; i<arr.length; i++)
       {
           if(arr[i] > 500)
           arr[i] = 500;
       }
   }
   public static double[] copyArray(double arr[])
   {
       double arr1[] = new double[arr.length];
       for(int i=0; i<arr1.length; i++)
       {
           arr1[i] = 1/arr[i];
       }
       return arr1;
   }
   public static void printArray(double arr[])
   {
       System.out.println("Array Elements are : "+Arrays.toString(arr));
   }
}

/*
Output:

Enter Element:
510
Enter Element:
444
Enter Element:
333
Enter Element:
222
Enter Element:
700
Enter Element:
212.2
Enter Element:
111
Enter Element:
999
Enter Element:
987
Enter Element:
3
Input Array Elements:
Array Elements are : [510.0, 444.0, 333.0, 222.0, 700.0, 212.2, 111.0, 999.0, 987.0, 3.0]
Square Root of 510.0 is 22.58317958127243
Square Root of 444.0 is 21.071307505705477
Square Root of 333.0 is 18.24828759089466
Square Root of 222.0 is 14.89966442575134
Square Root of 700.0 is 26.457513110645905
Square Root of 212.2 is 14.567086187704115
Square Root of 111.0 is 10.535653752852738
Square Root of 999.0 is 31.606961258558215
Square Root of 987.0 is 31.416556144810016
Square Root of 3.0 is 1.7320508075688772
After Square root method called:
Array Elements are : [510.0, 444.0, 333.0, 222.0, 700.0, 212.2, 111.0, 999.0, 987.0, 3.0]
After replace method called:
Array Elements are : [500.0, 444.0, 333.0, 222.0, 500.0, 212.2, 111.0, 500.0, 500.0, 3.0]
Original Array:
Array Elements are : [500.0, 444.0, 333.0, 222.0, 500.0, 212.2, 111.0, 500.0, 500.0, 3.0]
New Array:
Array Elements are : [0.002, 0.0022522522522522522, 0.003003003003003003, 0.0045045045045045045, 0.002, 0.004712535344015081, 0.009009009009009009, 0.002, 0.002, 0.3333333333333333]

*/

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