Skip to main content

Matrix Calculation by Java

package mypack;
import java.util.Scanner;

public class Matrix {
    private int row;
    private int col;
    private int[][] matrix;

    public Matrix(int trow, int tcol) {
        this.row = trow;
        this.col = tcol;
    }

    public Matrix(int trow, int tcol, int[][] m) {
        this.row = trow;
        this.col = tcol;
        this.matrix = m;
    }
   
public int[][] fill(){
        int[][] data = new int[row][col];
        Scanner input = new Scanner(System.in);

        for(int row = 0; row< matrix.length; row++){
            for(int col = 0 ;col< matrix[row].length; col++){
                System.out.println("enter the elements for the Matrix");
                data[row][col] = input.nextInt();
             }
            System.out.println();
        }
        return data;
    }

public void disp(int [][]m,String s)
{
     System.out.println(s + " matrix is : ");
     for (int i = 0; i <row ; i++) {
            for (int j = 0; j < col; j++) {
                  System.out.print(m[i][j] + " ");
            }
            System.out.println();
     }
}

public int[][] add(int [][]m1,int [][]m2){
    int [][]result=new int[row][col];
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
              result[i][j] = m1[i][j] + m2[i][j];
        }
 }
    return result;   
}

public int[][] sub(int [][]m1,int [][]m2){
    int [][]result=new int[row][col];
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
              result[i][j] = m1[i][j] - m2[i][j];
        }
 }
    return result;   
}

public int[][] multi(int [][]m1,int [][]m2){
    int [][]result=new int[row][col];
     for (int i = 0; i < row; i++) {
         for (int j = 0; j < col; j++) {
             for (int k = 0; k < col; k++) { //columns in matrix1= rows in matrix2
                 result[i][j] = result[i][j] + m1[i][k] * m2[k][j];
             }
         }
     }
    return result;   
}


public int[][] trans(int [][]m1){
    int [][]result=new int[row][col];
      for (int i = 0; i < row; i++) {
          for (int j = 0; j < col; j++)
                result[j][i] = m1[i][j];
   }

    return result;   
}

    public static void main(String[] args){
    
        Scanner input=new Scanner(System.in);
       
        System.out.println("Enter the First matrix Elements");
        int[][] ma1 = new int[2][2];
        Matrix q1 = new Matrix(2, 2,ma1);
        ma1=q1.fill();
        q1.disp(ma1,"first");
       
        System.out.println("Enter the Second matrix Elements");
        int[][] ma2 = new int[2][2];
        Matrix q2 = new Matrix(2, 2,ma2);
        ma2=q2.fill();
        q2.disp(ma2,"Second");
   
        char type;
        do{
             System.out.println("Choice any option");
             System.out.println("1.Addition");
             System.out.println("2.Substration");
             System.out.println("3.Mutlipication");
             System.out.println("4.Transpose");
             System.out.println("Choicee only integer type");
             System.out.println("Choice any option");
             int myinput=input.nextInt();
             switch(myinput){
             case 1:  int[][] ma3=new int[2][2];
                      Matrix q3=new Matrix(2,2,ma3);
                      ma3=q3.add(ma1,ma2);
                      q3.disp(ma3,"Addtion");
                      break;
           
             case 2:    int[][] ma4=new int[2][2];
                        Matrix q4=new Matrix(2,2,ma4);
                        ma4=q4.sub(ma1,ma2);
                        q4.disp(ma4,"Substration"); break;
                       
             case 3:  int[][] ma5=new int[2][2];
                      Matrix q5=new Matrix(2,2,ma5);
                      ma5=q5.multi(ma1,ma2);
                      q5.disp(ma5,"Multiplication"); break;
                     
             case 4:   System.out.println("Transpose for matrix press key :");
                       System.out.println("1.First matrix");
                       System.out.println("2.Second matrix");
                       int key=input.nextInt();
                       switch(key){
                       case 1: int[][] ma6=new int[2][2];
                               Matrix q6=new Matrix(2,2,ma6);
                               ma6=q6.trans(ma1);
                               q6.disp(ma6,"First Transpose"); break;
                       case 2: int[][] ma7=new int[2][2];
                                Matrix q7=new Matrix(2,2,ma7);
                                ma7=q7.trans(ma2);
                                q7.disp(ma7,"Second Transpose"); break;
                       default : System.out.println("enter wrong key value");
                              
                       } break;
           
             default : System.out.println("plzz enter correct input!!");        
             }
             System.out.println("Do u want Try Again");
             System.out.println("Just type Yes->y and No->any key");
             type = input.next().charAt(0);
           
            }while(type=='y'||type=='Y');
       
         System.out.println("BYE , Thanks for using...");
    }
 }



Comments

Popular posts from this blog

Sum of Digit is Pallindrome or not

  Sum of Digit is Pallindrome or not Given a number N.Find if the digit sum(or sum of digits) of N is a Palindrome number or not. Note:A Palindrome number is a number which stays the same when reversed.Example- 121,131,7 etc. Example 1: Input: N=56 Output: 1 Explanation: The digit sum of 56 is 5+6=11. Since, 11 is a palindrome number.Thus, answer is 1. Example 2: Input: N=98 Output: 0 Explanation: The digit sum of 98 is 9+8=17. Since 17 is not a palindrome,thus, answer is 0. Your Task: You don't need to read input or print aything.Your Task is to complete the function isDigitSumPalindrome() which takes a number N as input parameter and returns 1 if the Digit sum of N is a palindrome.Otherwise it returns 0. Expected Time Complexi...

Menu with 5 option & selected option should appear in text box

Q6. Create a menu with 5 options and selected option should appear in text box. Android Program : Activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/activity_main"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     tools:context="com.kishanhaldar.program6.MainActivity">                 ...

Closest Number

Closest Number Basic Accuracy: 11.21% Submissions: 1615 Points: 1 Given non-zero two integers N and M . The problem is to find the number closest to N and divisible by M . If there are more than one such number, then output the one having maximum absolute value .   Example 1: Input: N = 13 , M = 4 Output: 12 Explanation: 12 is the Closest Number to 13 which is divisible by 4. Example 2: Input: N = -15 , M = 6 Output: -18 Explanation: -12 and -18 are both similarly close to -15 and divisible by 6. but -18 has the maximum absolute value. So, Output is -18   Your Task: You don't need to read input or print anyth...