Java program import java.util.Random; public class Lab7c { static Random rand =
ID: 3573207 • Letter: J
Question
Java programimport 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
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);
}
/* Add the following methods here:
* average
* createDouble2D
* printStrings
* printDouble // 1D
* printDouble // 2D
* getRandomIndex
* swap
* shuffle
*/
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;
}
} // end class
Output:
0.0 1.0 2.0
1.0 2.0 3.0
1.0 2.0 3.0
13.0
absolute random cosine floor sine power
sine power floor cosine random absolute
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.