74 lines
1.8 KiB
Java
74 lines
1.8 KiB
Java
package serie04;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
public class Operation {
|
|
private int[][] table;
|
|
private int size;
|
|
|
|
public Operation(int size, int[][] table) {
|
|
if (size < 1) throw new NegativeArraySizeException();
|
|
this.size = size;
|
|
this.table = table;
|
|
}
|
|
|
|
public int calculate(int indexA, int indexB) {
|
|
if(indexA >= this.size || indexB >= this.size)throw new IndexOutOfBoundsException();
|
|
return this.table[indexA][indexB];
|
|
}
|
|
|
|
public boolean validateTable() {
|
|
|
|
//validate rows and check closedness
|
|
for(int i = 0; i < this.size; i++) {
|
|
ArrayList<Integer> used = new ArrayList<Integer>();
|
|
for(int j = 0; j < this.size; j++) {
|
|
if(this.table[i][j] >= this.size || this.table[i][j] < 0) return false;
|
|
if (isIn(this.table[i][j], used)) return false;
|
|
used.add(this.table[i][j]);
|
|
}
|
|
}
|
|
//validate columns
|
|
for(int j = 0; j < this.size; j++) {
|
|
ArrayList<Integer> used = new ArrayList<Integer>();
|
|
for(int i = 0; i < this.size; i++) {
|
|
if (isIn(this.table[i][j], used)) return false;
|
|
used.add(this.table[i][j]);
|
|
}
|
|
}
|
|
|
|
return this.checkAssociativity();
|
|
}
|
|
|
|
public boolean checkAssociativity() {
|
|
|
|
for(int a = 0; a < this.size; a++) {
|
|
for(int b = 0; b < this.size; b++) {
|
|
for(int c = 0; c < this.size; c++) {
|
|
if (this.checkAssociativity(a, b, c) == false) return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public boolean checkAssociativity(int indexA, int indexB, int indexC) {
|
|
|
|
return this.calculate(indexA, this.calculate(indexB, indexC)) == this.calculate(this.calculate(indexA, indexB), indexC);
|
|
|
|
}
|
|
|
|
private static boolean isIn(int a, ArrayList<Integer> array) {
|
|
for(int i = 0; i < array.size(); i++) {
|
|
if (a == array.get(i)) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public int getSize() {
|
|
return this.size;
|
|
}
|
|
|
|
}
|