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

JAVA Need help with ByColumns method import java.util.*; @SuppressWarnings(\"raw

ID: 3813107 • Letter: J

Question

JAVA

Need help with ByColumns method

import java.util.*;

@SuppressWarnings("rawtypes")
public class Table implements Iterable<Comparable[]> {
   private Comparable data[][];
  
   // Creates a table that is a copy of the given input array
   public Table(Comparable data[][]) {
       int rows = data.length;
       int cols = 0;
       if (rows > 0) cols = data[0].length;
       this.data = new Comparable[rows][cols];
       for (int i = 0; i < rows; i++) {
           for (int j = 0; j < cols; j++) {
               this.data[i][j] = data[i][j];
           }
       }
   }

   // Comparator class for comparing with respect to a single column
   public static class ByColumn implements Comparator<Comparable[]> {
       public ByColumn(int col, boolean ascending) {
           ascending = true;
           for(int i = 0; i < data.length-1; i++) {
           if(data[i] >= data[i+1]);
                 
           ascending = false;
       }
   }         

       @Override
       public int compare(Comparable[] o1, Comparable[] o2) {
           Comparable o1Comparable = getComparable(o1);

           Comparable o2Comparable = getComparable(o2);

           return o1Comparable.compareTo(o2Comparable);

           }

           @SuppressWarnings("rawtypes")

           private Comparable getComparable(Object o) {

           try {
           Object invoke = o.getClass().getMethod(property, new Class[] {})

           .invoke(o, new Class[] {});

           return ((Comparable)invoke);

           } catch (Exception e) {

           throw new IllegalArgumentException(e.getMessage());

}
           return 0;
       }
   }
  
   // Comparator class for comparing with respect to several columns
   public static class ByColumns implements Comparator<Comparable[]> {
       public ByColumns(int cols[], boolean ascending[]) {
           // TODO
       }

Explanation / Answer

As the parameters are arrays You need to run the originalpublic ByColumn(int col, boolean ascending) inside a nested loop.

public ByColumns(int cols[], boolean ascending[]) {
           // running the loop for getting elements of cols

for(int i=0;i<cols.length;i++){

   for(intj=0;j<ascending.length;j++){

ByColumn(cols[i], ascending[j]);   

}

}
       }

You already have the following code:

public static class ByColumn implements Comparator<Comparable[]> {
       public ByColumn(int col, boolean ascending) {
           ascending = true;
           for(int i = 0; i < data.length-1; i++) {
           if(data[i] >= data[i+1]);
                 
           ascending = false;
       }
   }