import java.util.Random; public class Lab7c { static Random rand = new Random(23
ID: 3573053 • 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
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;
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:*/
void average(double d[])
{
double avg=0.0;
for(int i=o;i<d.length; i++)
{
avg+=d[i];
}
System.out.println("The average is %f ", avg/d.length);
}
double[][] createDouble2D(int x, int y)
{
double data[][];
for(int i=0;i<x;i++)
for(int j=0;j<y;j++)
data[i][j]=i+j;
return data;
}
void printStrings(string s[])
{
for(int i=o;i<s.length;i++)
System.out.printf(s[i] + ", ");
}
void printDouble(double data[])
{
for(int i=0;i<data.length;i++) System.out.printf("%f"+data[i] + ", ");
}
void printDouble(double data[][])
{
for(int i=0;i<data.length;i++)
{for(int j=0;j<data[i].length;j++) System.out.printf("%f" + data[i][j] + " ");
System.out.printf(" ");
}
}
* void swap(String s[], int x,int y)
{
String t1,t2;
t1=s[x];
t2=s[y];
s[x]=t2;
s[y]=t1;
printstrings(s);
}
void shuffle(String s[])
{
for(int n=0; n<s.length: n++)
{
int x= getRandomIndex(s.length);
int y= getRandomIndex(s.length);
swap(s,x,y);
}
printStrings(s);
}
int getRandomIndex(int n)
{
int index;
Random rand = new Random();
index = rand.nextInt(n);
return index;
}
} // end class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.