-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
111 lines (72 loc) · 2.01 KB
/
Matrix.java
File metadata and controls
111 lines (72 loc) · 2.01 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
public class Matrix{
private int[][] matrix;
public Matrix(int a, int b){
this.matrix = new int[a][b];
for(int i = 0; i < a; i++){
for(int e = 0; e < b; e++){
matrix[i][e] = 1;
}
}
}
public void setElement(int a, int b, int set){
if(a < matrix.length && b < matrix[0].length){
matrix[a][b] = set;
System.out.println("The element " + set + " has been added to the matrix");
} else {
System.out.println("That location isnt in the matrix.");
}
}
public void setRow(int a, String s){
if(a < matrix.length && s.length() == matrix[0].length){
for(int i = 0; i < matrix[0].length; i++){
System.out.println("This is working");
matrix[a][i] = Character.getNumericValue(s.charAt(i));
}
System.out.println("Row number " + a + " has now been changed to " + s);
} else {
System.out.println("That location isnt in the matrix or the string is the wrong size, please try again");
}
}
public void setColumn(int a, String s){
if(a<matrix[0].length && s.length() == matrix.length){
for(int i = 0; i < matrix.length; i++){
matrix[i][a] = Character.getNumericValue(s.charAt(i));
}
System.out.println("Column number " + a + " has now been changed to " + s);
} else {
System.out.println("That location isnt in the matrix or the string is the wrong size, please try again");
}
}
public String toString(){
String output = "[";
for(int a = 0; a < matrix.length; a++){
for(int i = 0; i < matrix[0].length; i++){
output += matrix[a][i];
if(i!=matrix[0].length-1){
output += ",";
}
}
if(a!=matrix[0].length-1){
output += ";";
}
}
output += "]";
return output;
}
public String prettyPrint(){
String output = "";
for(int a = 0; a < matrix.length; a++){
output += "Line " + a + ": ";
for(int i = 0; i < matrix[0].length; i++){
output += matrix[a][i];
if(i!=matrix[0].length-1){
output += " ,";
}
}
if(a!=matrix[0].length-1){
output += ". ";
}
}
return output;
}
}