-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixDemo.java
More file actions
145 lines (141 loc) · 3.02 KB
/
Copy pathMatrixDemo.java
File metadata and controls
145 lines (141 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import java.util.Scanner;
class Matrix //Super class
{
int a[][],r,c;
Matrix()
{
r=0;
c=0;
a=new int[r][c];
}
Matrix(int x,int y)
{
r=x;
c=y;
a=new int[r][c];
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the elements of the array");
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
a[i][j]=sc.nextInt();
}
Matrix Add(Matrix A) //Add Function to add two similar matrices
{
if(r!=A.r||c!=A.c)
{
System.out.println("Matrices cannot be added");
System.exit(0);
}
Matrix B=new Matrix(r,c);
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
B.a[i][j]=a[i][j]+A.a[i][j];
return B;
}
Matrix Product(Matrix A) //Function to multiply two matrices
{
if(c!=A.r)
{
System.out.println("Matrices cannot be multiplied");
System.exit(0);
}
Matrix B=new Matrix(r,A.c);
for(int i=0;i<r;i++)
{
for(int j=0;j<A.c;j++)
{
B.a[i][j]=0;
for(int k=0;k<c;k++)
B.a[i][j]+=a[i][k]*A.a[k][j];
}
}
return B;
}
Matrix Trans() //Function to create Transpose of a Matrix
{
Matrix B=new Matrix(c,r);
for(int i=0;i<c;i++)
for(int j=0;j<r;j++)
B.a[i][j]=a[j][i];
return B;
}
void display() //Function to display Matrix
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
}
}
public class MatrixDemo extends Matrix //Subclass
{
public static void main(String[] args)
{
int r,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the row size of Matrix");
r=sc.nextInt();
System.out.println("Enter the column size of Matrix");
c=sc.nextInt();
Matrix ob=new Matrix(r,c);
ob.input();
System.out.println("Enter the choice \n 1 for Addition \n 2 for Multiplication \n 3 for Transpose");
int ch=sc.nextInt();
switch(ch)
{
case 1:
{
int a,b;
System.out.println("Enter row and column size of Matrix 2");
a=sc.nextInt();
b=sc.nextInt();
Matrix ob1=new Matrix(a,b);
ob1.input();
Matrix ob2=ob1.Add(ob);
System.out.println("1's Matrix");
ob.display();
System.out.println("\n 2'nd Matrix");
ob1.display();
System.out.println("\n Added Matrix");
ob2.display();
break;
}
case 2:
{
int a,b;
System.out.println("Enter row and column size of Matrix 2");
a=sc.nextInt();
b=sc.nextInt();
Matrix ob1=new Matrix(a,b);
ob1.input();
Matrix ob2=ob.Product(ob1);
System.out.println("1's Matrix");
ob.display();
System.out.println("\n 2'nd Matrix");
ob1.display();
System.out.println("\n Multiplied Matrix");
ob2.display();
break;
}
case 3:
{
Matrix ob1=ob.Trans();
System.out.println("Original Matrix");
ob.display();
System.out.println("\n Transpose Matrix ");
ob1.display();
break;
}
default:
{
System.out.println("WRONG CHOICE,Program Exit");
System.exit(0);
}
}
}
}