Implement the missing functionality to compare two matrices

Implement the missing functionality to compare two matrices in JAVA. Use the following conditions to determine if Matrix A < B. 1. Size of A is less than size of B 2. If sizes are equal then the sum of diagonal elements in A is less than the sum of diagonal elements of B Diagonal elements are the values (0,0), (1,1) etc.

Solution

// Here is the solution.....

import java.util.Scanner;

public class Matrix implements Comparable{
Integer data[][];
Integer size;
public Matrix(Integer size) {
this.size = size;
data = new Integer[size][size];
}

/* public int compareTo(Matrix dest) {
// Replace with actual code
return 0;
}*/

       @Override
       public int compareTo(Object o) {
      
           Matrix obj = (Matrix)o;
           if(this.size < obj.size){
               return 1; // condition 1. satisfied
           }
           else if(this.size == obj.size){
               int mat1Sum = calculateDiagonalSum(this); // passing mat1 to the function to calculate sum of diagonal ele.
               int mat2sum = calculateDiagonalSum(obj);// passing mat2 to the function to calculate sum of diagonal ele.
               if(mat1Sum < mat2sum){
                   return 1; //cond. 2 satisfied... if mat1\'s sum is less than that of B return true..
               }
               else{
                   return 0;
               }
           }
          
          
           return 0;
       }
      
       public int calculateDiagonalSum(Matrix m){
           int sum = 0;
           for(int i=0;i<m.size;i++){
               for(int j=0;j<m.size;j++){
                   if(i == j){
                       sum += m.data[i][j];
                   }
               }
           }
           return sum;
       }
          
       public static void main(String [] args){
           System.out.println(\"Enter the size of 1st Matrix : \");
           Scanner sc = new Scanner(System.in);
           Matrix mat1 = new Matrix(sc.nextInt());
           System.out.println(\"Enter the elements of 1st Matrix : \");
           for(int i=0;i<mat1.size;i++){
               for(int j=0;j<mat1.size;j++){
               mat1.data[i][j] = sc.nextInt();
               }
           }
           System.out.println(\"Enter the size of 2nd Matrix : \");
           Matrix mat2 = new Matrix(sc.nextInt());
           System.out.println(\"Enter the elements of 2nd Matrix : \");
           for(int i=0;i<mat2.size;i++){
               for(int j=0;j<mat2.size;j++){
               mat2.data[i][j] = sc.nextInt();
               }
           }
          
           System.out.println(\"Comparing matrixes......\");
           if(mat1.compareTo(mat2) == 0){
               System.out.println(\"Matrix A not less than B\");
           }
           else{
               System.out.println(\"Yes .........Matrix A < B\");
           }
       }

}

Implement the missing functionality to compare two matrices in JAVA. Use the following conditions to determine if Matrix A < B. 1. Size of A is less than siz
Implement the missing functionality to compare two matrices in JAVA. Use the following conditions to determine if Matrix A < B. 1. Size of A is less than siz

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site