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

Implement a Table class that represents a matrix in which we can add rows and co

ID: 3594218 • Letter: I

Question

Implement a Table class that represents a matrix in which we can add rows and columns. It supports the following operations (with specified running times): rows() returns the number of ros in the Table. (O(1) time) cols() returns the number of columns in the Table (O(1) time) get(i, j) return the item at row i and column j in the Table (O(1) time) set(i,j,x) set the item at row i and column j to x in the Table, and return the value previously stored there. (O(1) time) addRow(i) insert a new row at index i. This creates cols() new entries in the Table, which are all initialized to null. This causes the indices of rows i, ... , rows()-1 to increase by 1. (O(1+rows()-i)) time). removeRow(i) remove row i. This causes the indices of rows i+1, ..., rows()-1 to decrease by 1. (O(1+rows()-i) time). addColumn(i) inserts a new column at index i. This creates rows() new entries in the Table, which are all initialized to null. This causes the indices of columns i, ..., cols()-1 to increase by 1. (O((1+cols()-i)*rows()) time). removeColumni) removes column, i. This causes the indices of columns i+1, ..., cols()-1 to decrease by 1. (O((1+cols()-i)*rows()) time).

package comp2402a2;

/**

*/

public interface AbstractTable<T> {

/**

* You decide on the instance variables you need.

*/

public int rows();

public int cols();

public T get(int i, int j);

public T set(int i, int j, T x);

public void addRow(int i);

public void removeRow(int i);

public void addCol(int j);

public void removeCol(int j);

public String toString();

}

java

Explanation / Answer

Solution:

Here <T> is taken as integer and instead of "null","0" is taken

Table.class:

package comp2402a2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Table implements AbstractTable<Integer> {

   List<int[]> rowList = new ArrayList<int[]>();
  
   public static void main(String[] args) {
       // TODO Auto-generated method stub

       //Creating an table object
       Table obj = new Table();
      
       //adding elements to the list
       obj.rowList.add(new int[] { 1, 2});
        obj.rowList.add(new int[] { 4, 5});
        obj.rowList.add(new int[] { 7, 8});
      
        //Printing number of rows and columns
        System.out.println("Number of rows: "+obj.rows());
        System.out.println("Number of columns: "+obj.cols()+"");
      
        //setting and getting values from and to a particular index
        System.out.println(obj.set(0, 1, 9));
        System.out.println(obj.get(0, 1));
      
        //Performing addRow and removeRow Operations
        obj.displayList();
        obj.addRow(1);
        System.out.println("After adding a row");
        obj.displayList();
        obj.removeRow(1);
        System.out.println("After removing a row");
        obj.displayList();
      
      //Performing addColumn and removeColumn Operations
        obj.addCol(1);
        System.out.println("After adding a column");
        obj.displayList();
        obj.removeCol(1);
        System.out.println("After removing a column");
        obj.displayList();
      
      
   }
  
   //This method is to display the matrix
   void displayList(){
       System.out.println("Array List");
       for (int[] row : rowList) {
            System.out.println("Row = " + Arrays.toString(row));
        }
   }

  
   @Override
   public int rows() {
       //To get the number of rows in the matrix
       return rowList.size();
   }

   @Override
   public int cols() {
       //To get the number of columns in the matrix
       return rowList.get(rows()-1).length;
   }

   @Override
   public int get(int i, int j) {
       //to get the value in the particular index i,j
       return rowList.get(i)[j];
   }

   @Override
   public int set(int i, int j, int x) {
       // to set the value in the particular index i,j
       int temp = rowList.get(i)[j];
       rowList.get(i)[j] = x;
       return temp;
   }

   @Override
   public void addRow(int i) {
       // Adding a new row to a particular index
       rowList.add(i, new int[cols()]);
   }

   @Override
   public void removeRow(int i) {
       // Removing a row from particular index
       rowList.remove(i);
   }

   @Override
   public void addCol(int j) {
       // TODO Auto-generated method stub
      
       for(int i=0;i<rows();i++){
           int[] a = new int[cols()+1];
           int index;
           for(int k=0;k<cols();k++)
               a[k] = rowList.get(i)[k];
          
           index = j;
           int temp,temp1=0;
          
           //From position of j swapping of elements is done till end of number of columns
           for(;index<cols()+1;index++){
               temp = a[index];
               a[index] = temp1;
               temp1 = temp;
           }
           //Removing the previous row and adding newly created array to the list
           rowList.remove(i);
           rowList.add(i, a);
       }
      
   }

   @Override
   public void removeCol(int j) {
       // TODO Auto-generated method stub
      
       for(int i=0;i<rows();i++){
           int[] a = new int[cols()-1];
          
           for(int k=0;k<j;k++){
               a[k] = rowList.get(i)[k];
           }
          
           int index = j;
          
           //Swapping the elements from position j
           for(;index<cols()-1;index++){
               a[index] = rowList.get(i)[index+1];
              
           }
           //Removing the previous row and adding newly created array to the list
           rowList.remove(i);
           rowList.add(i, a);
       }
      
   }

}

output:

Number of rows: 3
Number of columns: 2
2
9
Array List
Row = [1, 9]
Row = [4, 5]
Row = [7, 8]
After adding a row
Array List
Row = [1, 9]
Row = [0, 0]
Row = [4, 5]
Row = [7, 8]
After removing a row
Array List
Row = [1, 9]
Row = [4, 5]
Row = [7, 8]
After adding a column
Array List
Row = [1, 0, 9]
Row = [4, 0, 5]
Row = [7, 0, 8]
After removing a column
Array List
Row = [1, 9]
Row = [4, 5]
Row = [7, 8]

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