Question

Assignment: Write a program with each of the following methods. You can assume this program is...

Assignment: Write a program with each of the following methods. You can assume this program is saved in a file called multiArrays.java. A sample main method and sample output is provided at the end of this section. All arrays created are of integer-type.

  1. Write a method to declare, initialize, and populate a two-dimensional array. Using the following header:

                        public static int[ ][ ] declare2D(Scanner scnr)

The user will indicate the size of the array.

  1. Write a method to declare, initialize, and populate a three-dimensional array. Using the following header:

                        public static int[ ][ ][ ] declare3D(Scanner scnr)

The user will indicate the size of the array.

  1. Write the following method that returns the location of the largest element in a two-dimensional array.

public static int[ ] locateLargest(int[ ][ ] 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.

  1. Write the following method that returns the location of the longest array in a three-dimensional array.

public static int[ ] locateLongest(int[ ][ ][ ] a)

The return value is a one-dimensional array that contains the longest array.

  1. Write a method that shuffles the rows in a two-dimensional rectangular int array using the following header:

public static void shuffle(int[ ][ ] m)

  1. Write a method to print the rows of a two-dimensional array, using the following header:

                        public static void print2D(int[ ][ ] m)

This array may be rectangular or ragged.                             

  1. Write a method to print the rows of a three-dimensional array, using the following header:

public static void print3D(int[ ][ ][ ] 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 your print2D() method, print this ragged array.

I only need locateLargest, locateLongest, and shuffle

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;
class multiArrays{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int _2DArray[][] = declare2D(sc);
        int _3DArray[][][] = declare3D(sc);
        print2D(_2DArray);
        int[] largest_location = locateLargest(_2DArray);
        System.out.println("Largest element is "+_2DArray[largest_location[0]][largest_location[1]]);
        print3D(_3DArray);
        int[] largest_array = locateLongest(_3DArray);
        System.out.println("longest array is:");
        for(int i=0;i<largest_array.length;i++){
            System.out.print(largest_array[i]+" ");
        }

    }
    public static int[ ][ ] declare2D(Scanner sc){
        System.out.println("----------------Create 2D Array----------------");
        int r;
        System.out.print("Enter the number of rows in 2D array:");
        r=sc.nextInt();
        int[][] _2DArray = new int[r][];
        for (int i = 0;i<r;i++){
            System.out.print("Enter the number of elements in row "+(i+1)+":");
            int c=sc.nextInt();
            _2DArray[i]=new int[c];
            System.out.print("Enter the elements in row "+(i+1)+":");
            for(int j=0;j<c;j++){
                _2DArray[i][j]=sc.nextInt();
            }
        }
        return _2DArray;
    }
    public static int[ ][ ][ ] declare3D(Scanner sc){
        System.out.println("----------------Create 3D Array----------------");
        int r;
        System.out.print("Enter the number of rows in 3D array:");
        r=sc.nextInt();
        int[][][] _3DArray = new int[r][][];
        for (int i = 0;i<r;i++){
            System.out.print("Enter the number of columns in row "+(i+1)+":");
            int c=sc.nextInt();
            _3DArray[i]=new int[c][];
            for(int j=0;j<c;j++){
                System.out.print("Enter the number of elements in column "+(j+1)+":");
                int d=sc.nextInt();
                _3DArray[i][j]=new int[d];
                System.out.print("Enter the elements in column "+(j+1)+" of row "+(i+1)+":");
                for(int k = 0; k<d;k++) {
                    _3DArray[i][j][k] = sc.nextInt();
                }
            }
        }
        return _3DArray;
    }
    public static int[ ] locateLargest(int[ ][ ] a){
        int[] location  = new int[2];
        int max=-1;
        for(int i=0;i<a.length;i++){
            for(int j = 0;j<a[i].length;j++){
                if(a[i][j]>max) {
                    max = a[i][j];
                    location[0] = i;
                    location[1] = j;
                }
            }
        }
        return location;
    }
    public static int[ ] locateLongest(int[ ][ ][ ] a){
        int length = 0;
        int m=0;
        int n=0;
        for(int i = 0 ; i<a.length;i++){
            for(int j = 0 ; j<a[i].length;j++){
                if(length<a[i][j].length){
                    length = a[i][j].length;
                    m=i;
                    n=j;
                }
            }
        }
        int longest_array[] =new int[length];
        for(int i = 0;i<length;i++){
                longest_array[i] = a[m][n][i];
        }
        return longest_array;
    }
    public static void shuffle(int[ ][ ] m){

    }
    public static void print2D(int[ ][ ] m){
        for(int i=0;i<m.length;i++){
            for(int j = 0;j<m[i].length;j++){
                System.out.print(m[i][j]+" ");
            }
            System.out.println();
        }
    }
    public static void print3D(int[ ][ ][ ] m){
        for(int i=0;i<m.length;i++){
            for(int j = 0;j<m[i].length;j++){
                for(int k = 0;k<m[i][j].length;k++){
                    System.out.print(m[i][j][k]+" ");
                }
                System.out.println();
            }

        }
    }
}
Add a comment
Know the answer?
Add Answer to:
Assignment: Write a program with each of the following methods. You can assume this program is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • import java.util.Scanner; public class Lab6d { public static void main(String[] args) {    Scanner scnr =...

    import java.util.Scanner; public class Lab6d { public static void main(String[] args) {    Scanner scnr = new Scanner(System.in); // TODO: get user choice    // TODO: call printTable method passing choice as the parameter    } public static void printTable(int stop) { // TODO: print header    // TODO: loop to print table rows up to stop value    } Write a Java program where the main () method prompts the user to select an integer value between 1 and...

  • 14.8 Prog 8 Overload methods (find avg) Write a program to find the average of a...

    14.8 Prog 8 Overload methods (find avg) Write a program to find the average of a set numbers (assume that the numbers in the set is less than 20) using overload methods. The program first prompts the user to enter a character; if the character is 'I' or 'i', then invokes the appropriate methods to process integer data set; if the character is 'R' or 'r', then invokes the appropriate methods to process real number data set; if the inputted...

  • In Java Please Create A Program For The Following; Please Note: This program should be able...

    In Java Please Create A Program For The Following; Please Note: This program should be able accept changes to the values of constants ROWS and COLS when testing the codes. Switch2DRows Given a 2D array with ROWS rows and COLS columns of random numbers 0-9, re-order the rows such that the row with the highest row sum is switched with the first row. You can assume that 2D arrau represents a rectangular matrix (i.e. it is not ragged). Sample run:...

  • Write a java Program Initialize Array. Write a Java method initArray() with the following header to...

    Write a java Program Initialize Array. Write a Java method initArray() with the following header to initialize a one-dimensional array a such that the element values (integers) are twice the index value. For example, if i = 23, then a23 = 46. The function is void with one parameter -- the integer array of a[]. Then write a main() with an int[] array2i size 100 to call

  • I need assistance writing the following Java class. Design a class named Location for locating a...

    I need assistance writing the following Java class. Design a class named Location for locating a maximal value and its location in a two-dimensional array. The class contains public data fields row, column, and maxValue that store the maximal value and its indices in a two-dimensional array with row and column as int types and maxValue as a double type. Write the following method that returns the location of the largest element in a two-dimensional array: public static Location locateLargest(double[][]...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • Answer in JAVA 1. Complete the method definition to output the hours given minutes. Output for...

    Answer in JAVA 1. Complete the method definition to output the hours given minutes. Output for sample program: 3.5 import java.util.Scanner; public class HourToMinConv {    public static void outputMinutesAsHours(double origMinutes) {       /* Your solution goes here */    }    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       double minutes;       minutes = scnr.nextDouble();       outputMinutesAsHours(minutes); // Will be run with 210.0, 3600.0, and 0.0.       System.out.println("");    } } 2....

  • How can i print a diamond implementing these methods public static void printNChars(int n, char c))....

    How can i print a diamond implementing these methods public static void printNChars(int n, char c)). This method will print n times the character c in a row, public static void printDiamond(int size, char edgeChar, char fillChar). This method will call printNChars() method to print the shape. It will use the edgeChar to print the sides and the fillChar to fill the interior of the shape. The shape will have a height and a width of the given size. public...

  • Write a for loop to print all elements in courseGrades, following each element with a space...

    Write a for loop to print all elements in courseGrades, following each element with a space (including the last). Print forwards, then backwards. End each loop with a newline. Ex: If courseGrades = {7, 9, 11, 10}, print: 7 9 11 10 10 11 9 7 Hint: Use two for loops. Second loop starts with i = courseGrades.length - 1. (Notes) Note: These activities may test code with different test values. This activity will perform two tests, both with a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT