-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixMain.java
More file actions
189 lines (159 loc) · 8.82 KB
/
MatrixMain.java
File metadata and controls
189 lines (159 loc) · 8.82 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
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
import java.util.Scanner;
/**
* Matrix operations main program.
*/
public class MatrixMain extends MatrixCalculations {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.println("Hello and welcome to the Matrix Calculator.");
System.out.println("Please select a calculation.");
System.out.println("Type A for addition, S for subtraction, T for transpose, P for power, M for multiplication.");
String calculation = userInput.nextLine().trim();
// Allow both uppercase and lowercase inputs.
while (!calculation.matches("(?i)a|s|t|p|m")) {
System.out.println("Sorry, your input is not one of the options. Please try again.");
System.out.println("Type A for addition, S for subtraction, T for transpose, P for power, M for multiplication.");
calculation = userInput.nextLine().trim();
}
try {
int rows = 0, cols = 0, rows2 = 0, cols2 = 0;
// For operations that require two matrices.
if (calculation.equalsIgnoreCase("A") ||
calculation.equalsIgnoreCase("S") ||
calculation.equalsIgnoreCase("M")) {
System.out.println("Enter the number of rows for the first matrix:");
rows = userInput.nextInt();
System.out.println("Enter the number of columns for the first matrix:");
cols = userInput.nextInt();
System.out.println("Enter the number of rows for the second matrix:");
rows2 = userInput.nextInt();
System.out.println("Enter the number of columns for the second matrix:");
cols2 = userInput.nextInt();
// Validate dimensions.
if (calculation.equalsIgnoreCase("A") || calculation.equalsIgnoreCase("S")) {
if (rows != rows2 || cols != cols2) {
System.out.println("Error: For addition and subtraction, both matrices must have the same dimensions.");
userInput.close();
return;
}
} else if (calculation.equalsIgnoreCase("M")) {
if (cols != rows2) {
System.out.println("Error: For multiplication, the number of columns of the first matrix must equal the number of rows of the second matrix.");
userInput.close();
return;
}
}
} else { // For operations that require only one matrix (Transpose and Power).
System.out.println("Enter the number of rows for the matrix:");
rows = userInput.nextInt();
System.out.println("Enter the number of columns for the matrix:");
cols = userInput.nextInt();
if (calculation.equalsIgnoreCase("P") && (rows != cols)) {
System.out.println("Error: For the power operation, the matrix must be square.");
userInput.close();
return;
}
}
// Clear the newline left by nextInt.
userInput.nextLine();
// Prepare matrix string arrays.
if (calculation.equalsIgnoreCase("A")) {
String[][] matrix1 = new String[rows][cols];
String[][] matrix2 = new String[rows][cols];
System.out.println("Enter the elements for the first matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Element (" + (i + 1) + "," + (j + 1) + "): ");
matrix1[i][j] = userInput.next();
}
}
System.out.println("Enter the elements for the second matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Element (" + (i + 1) + "," + (j + 1) + "): ");
matrix2[i][j] = userInput.next();
}
}
double[][] m1 = stringToMatrix(matrix1);
double[][] m2 = stringToMatrix(matrix2);
System.out.println("Result of addition:");
System.out.println(addition(m1, m2));
} else if (calculation.equalsIgnoreCase("S")) {
String[][] matrix1 = new String[rows][cols];
String[][] matrix2 = new String[rows][cols];
System.out.println("Enter the elements for the first matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Element (" + (i + 1) + "," + (j + 1) + "): ");
matrix1[i][j] = userInput.next();
}
}
System.out.println("Enter the elements for the second matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Element (" + (i + 1) + "," + (j + 1) + "): ");
matrix2[i][j] = userInput.next();
}
}
double[][] m1 = stringToMatrix(matrix1);
double[][] m2 = stringToMatrix(matrix2);
System.out.println("Result of subtraction:");
System.out.println(subtraction(m1, m2));
} else if (calculation.equalsIgnoreCase("M")) {
// For multiplication, first matrix is of size (rows x cols) and
// the second matrix is of size (rows2 x cols2), where cols == rows2.
String[][] matrix1 = new String[rows][cols];
String[][] matrix2 = new String[rows2][cols2];
System.out.println("Enter the elements for the first matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Element (" + (i + 1) + "," + (j + 1) + "): ");
matrix1[i][j] = userInput.next();
}
}
System.out.println("Enter the elements for the second matrix:");
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
System.out.print("Element (" + (i + 1) + "," + (j + 1) + "): ");
matrix2[i][j] = userInput.next();
}
}
double[][] m1 = stringToMatrix(matrix1);
double[][] m2 = stringToMatrix(matrix2);
System.out.println("Result of multiplication:");
System.out.println(matrixToString(multiplication(m1, m2)));
} else if (calculation.equalsIgnoreCase("T")) {
String[][] matrix1 = new String[rows][cols];
System.out.println("Enter the elements for the matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Element (" + (i + 1) + "," + (j + 1) + "): ");
matrix1[i][j] = userInput.next();
}
}
double[][] m1 = stringToMatrix(matrix1);
System.out.println("Transpose of the matrix:");
System.out.println(transpose(m1));
} else if (calculation.equalsIgnoreCase("P")) {
String[][] matrix1 = new String[rows][cols];
System.out.println("Enter the elements for the matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Element (" + (i + 1) + "," + (j + 1) + "): ");
matrix1[i][j] = userInput.next();
}
}
System.out.print("Enter the power to which you want to raise the matrix: ");
int power = userInput.nextInt();
double[][] m1 = stringToMatrix(matrix1);
System.out.println("Matrix raised to the power " + power + ":");
System.out.println(matrixPower(power, m1));
}
} catch (Exception e) {
System.out.println("Sorry, we experienced an error running the calculations on your matrix. "
+ "Please try again and make sure all inputs are correct.");
} finally {
userInput.close();
}
}
}