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

Design and implement a Java program for programming exercise 8.27, page 315 (nam

ID: 3737128 • Letter: D

Question

Design and implement a Java program for programming exercise 8.27, page 315 (name it ColumnSorting) as described in the problem statement. Write method sortColumns()using the method signature specified in the textbook so that it will sort the columns of any non-ragged array (you may change the element type

to integer). The method sortColumns() must be designed to handle two-dimensional arrays with any number of rows and columns. Notice that this method returns a new array, the original array remains unchanged. To test this method, the main method of your program prompts the user to enter a two-dimensional array, passes the original array (not a copy) to the method sortColumns(), and then displays the original array (not a copy created before passing the original array to the sortColumns() method) followed by the column-sorted array as shown in the

sample run. Design the main method of your program to handle all input and output and to allow the user to re-run the

program with different sets of inputs. Document your code and organize the output using appropriate formatting techniques.

8.27 (Column sorting) Implement the following method to sort the columns in a two- dimensional array. A new array is returned and the original array is intact public static double sortColumns (double n m)

Explanation / Answer


import java.util.Scanner;


public class Column_Sorting {

    public static void main(String[] args) {

        double[][] m = new double[3][3];

        Scanner
                input = new Scanner(System.in);
        System.out.println("Enter a 3-by-3 matrix row by row: ");
        for (int i = 0; i < m.length; i++)
            for (int j = 0; j < m[i].length; j++)
                m[i][j] = input.nextDouble();


        double[][] sorted = sortColumns(m);
        displayMatrix(sorted);


    }

    public static double[][] sortColumns(double[][] m) {

        // create copy array
        double[][] sorted = new double[m.length][m[0].length];

        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < m[i].length; j++) {
                sorted[i][j] = m[i][j];
            }
        }

        for (int j = 0; j < sorted[0].length; j++) {

            for (int i = 0; i < sorted.length - 1; i++) {

                double currentMin = sorted[i][j];
                int minIndex = i;

                for (int row = i + 1; row < sorted.length; row++) {

                    if (currentMin > sorted[row][j]) {
                        currentMin = sorted[row][j];
                        minIndex = row;
                    }
                }
                if (minIndex != i) {
                    sorted[minIndex][j] = sorted[i][j];
                    sorted[i][j] = currentMin;
                }

            }
        }
        return sorted;
    }

    public static void displayMatrix(double[][] m) {

        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < m[i].length; j++) {
                System.out.printf("%2.3f ", m[i][j]);
            }
            System.out.println("");
        }
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote