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

Maximum possible number of monsters you can defeat?

 Q).Maximum possible number of monsters you can defeat?  =>Introduction: While playing an RPG game, you were assigned to complete one of the hardest quests in this game. There are n monsters you'll need to defeat in this quest. Each monster i is described with two integer numbers - poweri and bonusi. To defeat this monster, you'll need at least poweri experience points. If you try fighting this monster without having enough experience points, you lose immediately. You will also gain bonusi experience points if you defeat this monster. You can defeat monsters in any order. The quest turned out to be very hard - you try to defeat the monsters but keep losing repeatedly. Your friend told you that this quest is impossible to complete. Knowing that, you're interested, what is the maximum possible number of monsters you can defeat? (Question difficulty level: Hardest) => Input: The first line contains an integer, n, denoting the number of monsters. The next line contains an

Android Activity Lifecycle Phases

Q2. Create an application to display various android activity lifecycle phases. Android Program :    MainActivity.java package com.kishanhaldar.implicit_intent; package com.example.hp.myhello; import android.content.res.Configuration; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; public class MainActivity extends AppCompatActivity {       String msg = "Android :";     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         Log.d(msg, "On created Method()");     }     protected void onStart() {         super.onStart();         Log.d(msg, "On start method() ");     }     protected void onResume(){         super.onResume();         Log.d(msg,"On the Resume method()");     }     protected void on