-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReturnMenuExit.java
256 lines (227 loc) · 7.87 KB
/
ReturnMenuExit.java
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package project1;
import java.util.Scanner;
public class project1 {
static int getrow()
{
Scanner input =new Scanner(System.in);
return input.nextInt();
}
static int getcolumn()
{
Scanner input =new Scanner(System.in);
return input.nextInt();
}
static double getScalar()
{
Scanner input =new Scanner(System.in);
return input.nextDouble();
}
static double[][] getMatrix(int row, int column)
{
Scanner input =new Scanner(System.in);
double[][] matrix= new double [row][column];
//getting the first matrix
System.out.print("Enter your matrix: ");
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
double arrayElement;
arrayElement= input.nextDouble();
matrix[i][j]=arrayElement;
}
}
return matrix;
}
static void printMatrix(double[][] matrix, int row, int column)
{
{
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++)
System.out.print(matrix[i][j] + ", ");
System.out.println();
}
}
}
static double[][] matrixAddition(double[][] matrix1,double[][] matrix2, int row, int column){
double[][] matrix3 = new double[row][column];
for(int i = 0; i < row; i++) {
for(int j = 0; j < column; j++) {
matrix3[j][i] = matrix1[i][j] + matrix2[j][i];
}
}
return matrix3;
}
static double[][] matrixSubtraction(double[][] matrix1,double[][] matrix2, int row, int column){
double[][] matrix3 = new double[row][column];
for(int i = 0; i < row; i++) {
for(int j = 0; j < column; j++) {
matrix3[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
return matrix3;
}
static double[][] matrixMultiplication(double[][] matrix1,int rows1,int columns1,double[][] matrix2, int rows2, int columns2){
double[][] matrix3 = new double[rows1][columns2];
for(int i = 0; i < rows1; i++) {
for(int j = 0; j < columns2; j++) {
for(int k =0; k < rows2; k++) {
matrix3[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
return matrix3;
}
static double[][] scalarDivision(double[][] matrix, int rows, int columns, double scalar)
{
double[][] matrix3 = new double[rows][columns];
for(int i = 0; i < rows; i++) {
for(int j = 0; j < columns; j++) {
matrix3[i][j] = matrix[i][j] / scalar;
}
}
return matrix3;
}
static double[][] scalarMultiplicaiton(double[][] matrix, int rows, int columns, double scalar)
{
double[][] matrix3 = new double[rows][columns];
for(int i = 0; i < rows; i++) {
for(int j = 0; j < columns; j++) {
matrix3[i][j] = matrix[i][j] * scalar;
}
}
return matrix3;
}
static double[][] matrixTranspose(double[][] matrix, int rows, int columns)
{
double[][] matrix3 = new double[rows][columns];
for(int i = 0; i < rows; i++) {
for(int j = 0; j < columns; j++) {
matrix3[j][i] = matrix[i][j];
}
}
return matrix3;
}
static boolean myOption()
{
int menu;
boolean p,q;
do
{
Scanner input= new Scanner(System.in);
System.out.printf("Press '1' for Menu or '0' to Exit: ");
menu= input.nextInt();
p=(menu==0);
q=(menu==1);
}while( (p || !q) && (!p || q));//not XOR
if (menu==1)
{
return true;
}
else
{
return false;
}
}
public static void main(String[] args) {
while(true)
{
Scanner input = new Scanner(System.in);
int decision;
do
{
System.out.println("Choose your matrix operation below: ");
System.out.println("1-)Matrix addition\n2-)Matrix Subtraction\n3-)Matrix Multiplication\n4-)Scalar division of the matrix\n5-)Scalar multiplicaiton of the matrix" +
"\n6-)Transpose of the matrix\n7-)The inverse of the matrix ");
System.out.print("Operation: ");
decision=input.nextInt();
}while(decision<0 || decision>10);
int columns1, columns2, rows1, rows2;
double[][] matrix1, matrix2, matrix3;
double scalar;
if(decision > 0 && decision <= 3)
{
System.out.println("Type row: ");
rows1 = getrow();
System.out.println("Type column: ");
columns1 = getcolumn();
matrix1 = getMatrix(rows1, columns1);
System.out.println("Type row: ");
rows2 = getrow();
System.out.println("Type column: ");
columns2 = getcolumn();
matrix2 = getMatrix(rows2, columns2);
switch(decision){
case 1:
matrix3 = matrixAddition(matrix1, matrix2, rows1, columns1);
printMatrix(matrix3, rows1, columns1);
break;
case 2:
matrix3 = matrixSubtraction(matrix1, matrix2, rows1, columns1);
printMatrix(matrix3, rows1, columns1);
break;
case 3:
if (rows2 != columns1) {
System.out.println("\nMultiplication Not Possible");
break;
}
else
{
matrix3 = matrixMultiplication(matrix1, rows1, columns1, matrix2, rows2, columns2);
printMatrix(matrix3 ,rows1, columns2);
break;
}
}
} else if (decision == 4 || decision == 5)
{
System.out.println("Type row: ");
rows1 = getrow();
System.out.println("Type column: ");
columns1 = getcolumn();
matrix1 = getMatrix(rows1, columns1);
System.out.println("Type double Scalar: ");
scalar = getScalar();
switch (decision)
{
case 4:
matrix3 = scalarDivision(matrix1, rows1, columns1, scalar);
printMatrix(matrix3, rows1, columns1);
break;
case 5:
matrix3 = scalarMultiplicaiton(matrix1, rows1, columns1, scalar);
printMatrix(matrix3, rows1, columns1);
break;
}
} else if(decision == 6 || decision == 7)
{
System.out.println("Type row: ");
rows1 = getrow();
System.out.println("Type column: ");
columns1 = getcolumn();
matrix1 = getMatrix(rows1, columns1);
switch(decision)
{
case 6:
matrix3 = matrixTranspose(matrix1, rows1, columns1);
printMatrix(matrix3, rows1, columns1);
break;
case 7:
//matrix3 = matrixInverse(matrix1, columns1, rows1);
//printMatrix(matrix3, columns1, rows1);
}
}
else if( decision==000)
{
System.out.println("End of the Programme. ");
break;
}
boolean menU;
menU=myOption();
if(menU==false)
{
System.out.println("End of the Programme. ");
break;
}
}
}
}