forked from nithyasri27/firstpro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathj1.java
More file actions
199 lines (191 loc) · 7.72 KB
/
j1.java
File metadata and controls
199 lines (191 loc) · 7.72 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
189
190
191
192
193
194
195
196
197
198
199
import java.util.Scanner;
public class j1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
boolean stat = true;
int option;
while(stat){
System.out.println("Hello this is a simple matrix calculator \n "
+ "To Create a new matrix problem enter 1 and to exit the calculator enter 2");
option = in.nextInt();
switch(option){
case 1:
stat = createProblem(option);
break;
case 2:
stat = false;
break;
default:
stat = false;
break;
}
}
System.out.println("Calculator now terminated");
}
public static boolean createProblem(int i){
Scanner in = new Scanner(System.in);
boolean condition;
int contStat;
boolean statusC;
int frows;
int fcolumns;
int srows;
int scolumns;
int[][] matrix1;
int[][] matrix2;
int[][] matrix1Values;
int[][] matrix2Values;
System.out.println("Enter number of rows in first matrix");
frows = in.nextInt();
String operator;
System.out.println("Enter number of columns in first matrix");
fcolumns = in.nextInt();
System.out.println("Enter number of rows in second matrix");
srows = in.nextInt();
System.out.println("Enter number of columns in second matrix");
scolumns = in.nextInt();
matrix1 = createMatrix(frows, fcolumns);
matrix2 = createMatrix(srows, scolumns);
matrix1Values = setMatrixValues(matrix1,"First");
matrix2Values = setMatrixValues(matrix2, "Second");
statusC = true;
while(statusC){
System.out.println("Select matrix operation, \n type in + for addition, - for subtraction or * for multiplication:");
operator = in.next();
statusC = performOperation(operator, matrix1Values, matrix2Values);
}
System.out.println("Type 1 if you would like to continue using the calcultor,\n type 2 if you would like to exit");
contStat = in.nextInt();
switch(contStat){
case 1:
condition = true;
break;
case 2:
condition = false;
break;
default:
condition = false;
break;
}
return condition;
}
public static boolean performOperation(String operator, int[][] matrix1, int[][] matrix2){
Scanner in = new Scanner(System.in);
int optSelected;
boolean status = false;
switch(operator){
case "+":
addMatrices(matrix1, matrix2);
break;
case "-":
subtractMatrices(matrix1, matrix2);
break;
default:
System.out.println("Operator not valid");
break;
}
System.out.println("If you would like to perform additional operations on the two matrices used"+
" Type 1, or if you what to use a different pair of matrices to perform operations with type 2"+
"To exit type 3");
optSelected = in.nextInt();
switch(optSelected){
case 1:
status = true;
break;
case 2:
status = false;
break;
case 3:
status = false;
break;
}
return status;
}
public static int[][] setMatrixValues(int[][] matrix, String label){
Scanner in = new Scanner(System.in);
int dataEntered;
int rows = matrix.length;
int columns = matrix[0].length;
for(int i=0; i<rows; i++){
for(int j=0; j< columns; j++){
System.out.println("Enter value of "+label+" matrix at row: "+i+" and column:"+j+" :");
dataEntered = in.nextInt();
matrix[i][j] = dataEntered;
}
}
return matrix;
}
public static int[][] createMatrix(int numRows, int numColumns){
int[][] matrix = new int[numRows][numColumns];
return matrix;
}
public static void addMatrices(int[][] matrix1, int[][] matrix2){
boolean status = checkIfValidAddSub(matrix1, matrix2);
if(status == true){
for(int i = 0; i < matrix1.length; i++){
for(int j = 0; j < matrix1[0].length; j++){
int charM1 = matrix1[i][j];
int charM2 = matrix2[i][j];
int resultant = charM1 + charM2;
System.out.print(resultant+" ");
}
System.out.print("\n");
}
}else{
System.out.println("In order to add or subtract matrices they both must have the same number of rows \n and the same number of columns");
}
}
public static void subtractMatrices(int[][] matrix1, int[][] matrix2) {
boolean status = checkIfValidAddSub(matrix1, matrix2);
if (status == true) {
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[0].length; j++) {
int charM1 = matrix1[i][j];
int charM2 = matrix2[i][j];
int resultant = charM1 - charM2;
System.out.print(resultant + " ");
}
System.out.print("\n");
}
} else {
System.out.println("In order to add or subtract matrices they both must have the same number of rows \n and the same number of columns");
}
}
public static boolean checkIfValidAddSub(int[][] matrix1, int[][] matrix2){
boolean status;
int numRows1 = matrix1.length;
int numColumns1 = matrix1[0].length;
int numRows2 = matrix2.length;
int numColumns2 = matrix2[0].length;
if(numRows1 == numRows2 && numColumns1 == numColumns2){
status = true;
}else{
status = false;
}
return status;
}
public static boolean checkIfValidMult(int[][] matrix1, int[][] matrix2){
boolean status;
int columns = matrix1[0].length;
int rows = matrix2.length;
if( columns == rows){
status = true;
}else{
status = false;
}
return status;
}
}
{
public static boolean checkIfValidMult(int[][] matrix1, int[][] matrix2){
boolean status;
int columns = matrix1[0].length;
int rows = matrix2.length;
if( columns == rows){
status = true;
}else{
status = false;
}
return status;
}
}