Design and implement a Java program for programming exercise

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 two-dimensional 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 with different inputs (i.e., use a loop). Document your code, and organize and space the outputs properly. Use escape characters and formatting objects when applicable.

Solution

ColumnSorting.java


public class ColumnSorting {

   public static void main(String[] args) {
       java.util.Scanner in = new java.util.Scanner(System.in);
       System.out.println(\"Enter a 3-by-3 matrix row by row: \");
       String firstRow = in.nextLine();
       String firstRowValues[] = firstRow.split(\"\\\\s+\");
       String secondRow = in.nextLine();
       String secondRowValues[] = secondRow.split(\"\\\\s+\");
       String thirdRow = in.nextLine();
       String thirdRowValues[] = thirdRow.split(\"\\\\s+\");
       double a[][] = new double[3][3];
       for(int i=0; i<a.length; i++){
           for(int j=0; j<a[i].length; j++){
               if(i==0){
               a[i][j] = Double.parseDouble(firstRowValues[j]);
               }
               else if(i==1){
               a[i][j] = Double.parseDouble(secondRowValues[j]);  
               }
               else if(i==2){
               a[i][j] = Double.parseDouble(thirdRowValues[j]);  
               }
           }
       }
       System.out.println(\"The column-sorted array is \");
       double d[][] = sortColumns(a);
       String s = \"\";
       for(int i=0; i<d.length; i++){
           for(int j=0; j<d[i].length; j++){
               s = s + d[i][j] + \" \";
           }
           s = s + \"\ \";
       }
       System.out.println(s);

   }
public static double[][] sortColumns(double[][] m) {
   for(int i=0; i<m.length; i++){
       double temp = 0;
       for(int j=0; j<m[i].length-1; j++){
           if(m[j][i] > m[j+1][i]){
               temp = m[j][i];
               m[j][i] = m[j+1][i];
               m[j+1][i] = temp;
           }
       }
   }
return m;
}
}

Output:

Enter a 3-by-3 matrix row by row:
0.15 0.875 0.375
0.55 0.005 0.225
0.30 0.12 0.4
The column-sorted array is
0.15 0.005 0.225
0.3 0.12 0.375
0.55 0.875 0.4

 Design and implement a Java program for programming exercise 8.27, page 315 (name it Columnsorting) as described in the problem statement. Write method sortCol
 Design and implement a Java program for programming exercise 8.27, page 315 (name it Columnsorting) as described in the problem statement. Write method sortCol

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site