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

import java.util.Random; public class Lab7c { static Random rand = new Random(23

ID: 3573208 • Letter: I

Question


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
C-16 bjective Wite functions that take arrays as arguments or return anrays Background reading ZyBooks Chapters 5 and 6 Assignment Wrne a program that has methods to do the following with arrays: 1. average(doubloD array Finds the mean of values in a double amay and returns a double containing the mean laverage) 2. swapString array, int int y) "Swaps array elements in a String array at indexes xand y Returns void 3 getRandomlndexint nj "Given n, retums a random integer between 0 andn-1 using rand.nextint(n). 4, shuffle(StringD array) "Shumes a String array elements. Loops ntmes, where n is the number of dements in the array. For each iteration, cals getRandomlndex for one index, and again for another index, then cals swap with the two random indexes. "Prints a 1Darray of String obocts. Returns void. 6 printDouble(double0 array) "Prints a 1D double array Returns void. 7. printDouble(double00 amay) "Prints a 2D double array. Returns void. n, int m) "Creates and returns a double array given rows nand columns m. Using nested for loops, f the array with value of i +i "Cals the above methods. Provided. Testing The main method is written for you. This wil test some of the methods nyour program. In this program, the methods wil be tested by "unt tests" which call your methods and test the return values or output. If you hava army trouble testing your methods, ploase soo the lab TAs or me. Witng these in an DE wil halp you debug them,

Explanation / Answer

import java.util.Random;
import java.util.Scanner;
public class HelloWorld{
  
public static void main(String[] args) {


double[][] d2 = createDouble2D(2,3);
printDouble(d2);
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 row, int col){
double [][] data = new double [row][col];
Scanner in = new Scanner(System.in);
for(int i = 0; i< row; i++){
for(int j = 0 ;j< col ; j++){
System.out.println("enter the elementss for the Matrix");
data[i][j] = in.nextDouble();
} System.out.println();
}
return data;
}
public static double average( double [] d1){
double sum=0;
double average=0.0;
for(int i=0; i< d1.length ; i++){
sum= sum + d1[i];
}
average = sum / d1.length;
return average;
}
public static int getRandomIndex(int length){
Random ran = new Random();
int x = ran.nextInt(length+1);
return x;
}
public static String [] swap(String [] functions, int x, int y ){
  
String temp = functions[x];
functions[x] = functions[y];
functions[y] = temp;
  
return functions;
}
public static void shuffle(String[] functions) {
Random rgen = new Random();

for (int i = 0; i < functions.length; i++) {
int randPos = rgen.nextInt(functions.length);
String tmp = functions[i];
functions[i] = functions[randPos];
functions[randPos] = tmp;
}
}
public static void printDouble(double [][] array)
{

   for (int i = 0; i < array.length; i++) {

   // Loop and display sub-arrays.
   double [] sub = array[i];
   for (int j = 0; j < sub.length; j++) {
       System.out.print(sub[j] + " ");
   }
   System.out.println();
   }
}
public static void printStrings(String [] array){
for (int i=0; i< array.length; i++){
System.out.println(array[i]);
System.out.println();
}
}
}