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

in java, Write a program that has methods to do the following with arrays: avera

ID: 3572900 • Letter: I

Question

in java, Write a program that has methods to do the following with arrays:

average(double[] array)
*Finds the mean of values in a double array and returns a double containing the mean (average).

swap(String[] array, int x, int y)
*Swaps array elements in a String array at indexes x and y. Returns void.

getRandomIndex(int n)
*Given n, returns a random integer between 0 and n-1 using rand.nextInt(n).

shuffle(String[] array)
*Shuffles a String array elements. Loops n times, where n is the number of elements in the array. For each iteration, calls getRandomIndex for one index, and again for another index, then calls swap with the two random indexes.

printStrings(String[] array)
*Prints a 1D array of String objects. Returns void.

printDouble(double[] array)
*Prints a 1D double array. Returns void.

printDouble(double[][] array)
*Prints a 2D double array. Returns void.

createDouble2D(int n, int m)
*Creates and returns a double array given rows n and columns m. Using nested for loops, fill the array with value of i + j.

main()
*Calls the above methods. Provided in this format below.

import java.util.Random;

public class Lab7c {

static Random rand = new Random(234L);

public static void main(String[] args) {


double[][] d2 = createDouble2D(2,3);
printDouble(d2);
System.out.println();
  
printDouble(d2[1]);
System.out.println();
  
double[] d1 = {10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0};
System.out.println(average(d1));
  
String[] functions = { "absolute", "random", "power", "floor", "sine", "cosine" };
int x = getRandomIndex(functions.length);
int y = getRandomIndex(functions.length);
swap(functions, x, y);
printStrings(functions);
shuffle(functions);
printStrings(functions);

}

/* Add the following methods here:
* average
* createDouble2D
* printStrings
* printDouble // 1D
* printDouble // 2D
* getRandomIndex
* swap
* shuffle
*/
  
} // end class

Explanation / Answer

Lab7c.java


import java.util.Random;
public class Lab7c {

static Random rand = new Random(234L);
public static void main(String[] args) {

double[][] d2 = createDouble2D(2,3);
printDouble(d2);
System.out.println();
  
printDouble(d2[1]);
System.out.println();
  
double[] d1 = {10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0};
System.out.println(average(d1));
  
String[] functions = { "absolute", "random", "power", "floor", "sine", "cosine" };
int x = getRandomIndex(functions.length);
int y = getRandomIndex(functions.length);
swap(functions, x, y);
printStrings(functions);
shuffle(functions);
printStrings(functions);
}
    public static double[][] createDouble2D(int n, int m){
        double a[][] = new double[n][m];
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                a[i][j] = i+j;
            }
        }
        return a;
    }
    public static void printDouble(double[][] array) {
        for(int i=0; i<array.length; i++){
            for(int j=0; j<array[i].length; j++){
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }
    }
    public static void shuffle(String[] array) {
        for(int i=0; i<array.length;i++){
            int x = getRandomIndex(array.length);
    int y = getRandomIndex(array.length);
    swap(array, x, y);
        }
    }
    public static void printDouble(double[] array) {
        for(int i=0; i<array.length; i++){
        System.out.print(array[i]+" ");
        }
        System.out.println();
    }
    public static void printStrings(String[] array) {
        for(int i=0; i<array.length; i++){
           System.out.print(array[i]+" ");
           }
           System.out.println();
    }
    public static int getRandomIndex(int n) {
        Random r = new Random();
        return r.nextInt(n);
    }
    public static void swap(String[] array, int x, int y) {
        String temp ="";
        temp = array[x];
        array[x] = array[y];
        array[y] = temp;
    }
    public static double average(double[] array) {
        double sum = 0;
        for(int i=0; i<array.length; i++){
            sum = sum + array[i];
        }
        return sum/array.length;
    }
   
/* Add the following methods here:
* average
* createDouble2D
* printStrings
* printDouble // 1D
* printDouble // 2D
* getRandomIndex
* swap
* shuffle
*/
  
} // end class

Output:

0.0 1.0 2.0
1.0 2.0 3.0

1.0 2.0 3.0

13.0
absolute power random floor sine cosine
absolute power cosine floor sine random