Question: Write a program with each of the following methods. You can assume this program is saved in a file called MultiMethods.java . A sample main
Write a program with each of the following methods. You can assume this program is saved in a file calledMultiMethods.java. A sample main method is provided at the end of this section. All arrays created are ofchar type (except for the value returned from locateLargest (#3 below; that will beint).
- Write a method to declare, initialize, and populate a two-dimensional array. Using the following header:
public static char[ ][ ] declare2D(Scanner scnr)
The user will indicate the dimensions of the array (validate to be positive integers), and you can populate the elements with random characters between a and z (lowercase).
- Write a method to declare, initialize, and populate a three-dimensional array. Using the following header:
public static char[ ][ ][ ] declare3D(Scanner scnr)
The user will indicate the dimensions of the array, and you can populate the elements with random characters between a and z (lowercase).
- Write the following method that returns thelocation of the largest element in a two-dimensional array.
public static int[ ] locateLargest(char[ ][ ] a)
The return value is a one-dimensional array that contains two elements. These two elements indicate the row and column indices of the largest element in the two-dimensional array.The inputted array may be rectangular or ragged.
- Write the following method that returns the memory location of the longest array in a three-dimensional array.
public static char[ ] locateLongest(char[ ][ ][ ] a)
The return value is a one-dimensional array that contains the longest array. Even though the 3D array you generated in #2 is *likely* rectangular, this code would be most meaningful when tested with a 3D ragged array.Code has been added to the main method (below) to test this function.
- Write a method that shuffleselements within the rows in a two-dimensional rectangularchararray using the following header:
public static voidshuffle(char[ ][ ] m)
- Write a method to print the rows of a two-dimensional array, using the following header:
public static voidprint2D(char[ ][ ] m)
This array may be rectangular or ragged.
- Write a method to print the rows of a three-dimensional array, using the following
header:
public static voidprint3D(char[ ][ ][ ] m)
This array may be rectangular or ragged.
- Finally, in your main method, using an array initializer, declare a two-dimensional ragged array (there are no restrictions on size, but it should be ragged, not rectangular). Using yourprint2D() method, print this ragged array.
Notes:
- You shouldnot create a new scanner in each method
- you may need to add additional print statements to the main method below in order to test your code.
publicstaticvoid main(String[] args) {
Scanner myScanner =new Scanner(System.in);
char[][] user2D =declare2D(myScanner);
char[][][] user3D =declare3D(myScanner);
int[] largestElem =locateLargest(user2D);
char[] longestRow =locateLongest(user3D);
System.out.println("The largest element(2D) is at ["+
largestElem[0] +", "+largestElem[1]+"]" and is: "+
user2D[largestElem[0]][largestElem[1]]);
System.out.println("The longest row is at location: "+longestRow);
System.out.println(Arrays.toString(longestRow));
print2D(user2D);
shuffle(user2D);
print2D(user2D);
print3D(user3D);
char[][] myRaggedArray = ...// TODO: Declare ragged array
shuffle(myRaggedArray);
print2D(myRaggedArray);
char[][][] myRaggedArray3d =
{{{'b','d','f','z','z','y'},{'k','r'},{'h','i'},{'a','t'},{'j','x'}},
{{'a', 'p'}, {'q', 's'}, {'v', 'e'}, {'m', 'r'}, {'g', 'o'}},
{{'t', 'u'}, {'d','o', 'g'}, {'c', 'a','t'}}};
char[] longestRaggedRow =locateLongest(myRaggedArray3d);
System.out.println("The longest row is at location: "+
longestRaggedRow);
System.out.println(Arrays.toString(longestRaggedRow));
}
publicstaticchar[][] declare2D(Scanner scnr){
/*Your code goes here*/
}
publicstaticchar[][][] declare3D(Scanner scnr){
/*Your code goes here*/
}
publicstaticint[] locateLargest(char[][] a) {
/*Your code goes here*/
}
publicstaticchar[] locateLongest(char[][][] a) {
/*Your code goes here*/
}
publicstaticvoid shuffle(char[][] m) {
/*Your code goes here*/
}
publicstaticvoid print2D(char[][] m) {
/*Your code goes here*/
}
publicstaticvoid print3D(char[][][] m) {
/*Your code goes here*/
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
