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...");
}
}
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
Post a Comment