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

Write a program called ColumnSorting that asks the user to enter a 3-by-3 array

ID: 646491 • Letter: W

Question

Write a program called ColumnSorting that asks the user to enter a 3-by-3 array of doubles, entering the values one row at a time. The program then displays the array with each column being separately sorted, meaning that each column continues to have the same numbers in it as were inputted, but those numbers are printed out in each column in sorted order from smallest to largest. Your main()method must call the following method to accomplish this task:

Notice that this method returns a new array, and the original array should remain unchanged.

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 structure).

Here is a sample input and output:

Explanation / Answer

package comparator_test;

import java.util.Scanner;
import java.util.Arrays;
import java.util.Comparator;


/**
* This program sorts a double dimensional array on the basis of columns
*/
public class sort_double
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.println("What is the dimension of your matrix?");
        int arr_size = input.nextInt();
        Double[][] matrix = new Double[arr_size][arr_size];

        System.out.println("Enter " + arr_size + " rows, and " + arr_size + " columns.");
        //Scanner input1 = new Scanner(System.in);
        for (int row = 0; row < matrix.length; row++)
        {
            for (int column = 0; column < matrix.length; column++)
                matrix[row][column] = input.nextDouble();
        }
        for (int row = 0; row < matrix.length; row++)
        {
            for (int column = 0; column < matrix.length; column++)
                System.out.println(matrix[row][column]+" ");
        }
        System.out.println(sortCol(matrix));
    }


   

    
    public static Double[][] sortCol(Double[][] matrix)
    {
        //make a true copy of the original two dimensional array
        Double matrix_copy[][]=new Double[matrix.length][matrix.length];
       
        for (int i = 0; i < matrix.length; i++)
        {
            for(int j=0; j<matrix.length;j++)
            {
                matrix_copy[i][j] = matrix[i][j];
            }
        }
       
       // sort the matrix by column
       //Arrays.sort(matrix_copy, new ColumnComparator(0));
       //Arrays.sort(matrix_copy, new ColumnComparator(1));
       Arrays.sort(matrix_copy, new Comparator<Double[]>() {
            @Override
            public int compare(Double[] double1, Double[] double2)
            {
                Double numOfKeys1 = double1[1];
                Double numOfKeys2 = double2[1];
                return numOfKeys1.compareTo(numOfKeys2);
            }
        });


       return(matrix_copy);

       

       
      
    }
   
   
}

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