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: 3687906 • 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() as specified (you may change the element type to integer). 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 twodimensional array and displays the original array followed by the column-sorted array as shown in the sample run. Design the main method of your program such that it allows the user to re-run the program in the same session (run). Document your code, and organize and space the outputs properly. Use escape characters and formatting objects when applicable.

From Text:(Column sorting) Implement the following method to sort the columns in a twodimensional array. A new array is returned and the original array is intact. public static double[][] sortColumns(double[][] m). Write a test program that prompts the user to enter a 3 * 3 matrix of double values and displays a new column-sorted matrix.

Explanation / Answer

Hi below i have written the code for your reference,

import java.util.Scanner; /** * (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[][] m) * * Write a test program that prompts the user to enter a 3 * 3 matrix * of double values and displays a new column-sorted matrix. * */ public class Exercise_27 { 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