Upload files to "source"
This commit is contained in:
9
source/Addition.java
Normal file
9
source/Addition.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package serie04;
|
||||
|
||||
public class Addition extends Operation {
|
||||
|
||||
public Addition (int size, int[][] table) {
|
||||
super(size, table);
|
||||
}
|
||||
|
||||
}
|
||||
52
source/Field.java
Normal file
52
source/Field.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package serie04;
|
||||
|
||||
public class Field {
|
||||
|
||||
private int size;
|
||||
private Addition addition;
|
||||
private Multiplication multiplication;
|
||||
|
||||
public Field(int size, Addition addition, Multiplication multiplication) {
|
||||
|
||||
if(addition.getSize() != size || multiplication.getSize() + 1 != size) {
|
||||
throw new ArithmeticException();
|
||||
}
|
||||
|
||||
this.size = size;
|
||||
this.addition = addition;
|
||||
this.multiplication = multiplication;
|
||||
}
|
||||
|
||||
public boolean checkDistributivity(int indexA, int indexB, int indexC) {
|
||||
|
||||
int leftSide = multiplication.calculate(indexA, addition.calculate(indexB, indexC));
|
||||
int rightSide = addition.calculate(multiplication.calculate(indexA, indexB), multiplication.calculate(indexA, indexC));
|
||||
|
||||
return leftSide == rightSide;
|
||||
}
|
||||
|
||||
public boolean checkDistributivity() {
|
||||
|
||||
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.checkDistributivity(a, b, c) == false) return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean verifyField() {
|
||||
|
||||
if (this.multiplication.validateTable() == false) return false;
|
||||
|
||||
if (this.addition.validateTable() == false) return false;
|
||||
|
||||
if (this.checkDistributivity() == false) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
40
source/FieldTest.java
Normal file
40
source/FieldTest.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package serie04;
|
||||
|
||||
public class FieldTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int[][] m1Table = {
|
||||
{0,1,2},
|
||||
{1,2,0},
|
||||
{2,0,1}
|
||||
};
|
||||
|
||||
int[][] a1Table = { //This is the addition table number 1 which we want to
|
||||
{0,1,2,3}, //show is a field.
|
||||
{1,0,3,2},
|
||||
{2,3,0,1},
|
||||
{3,2,1,0}
|
||||
};
|
||||
|
||||
int[][] a2Table = { //This is the addition table for which we know it is not
|
||||
{0,1,2,3}, //a field
|
||||
{1,0,3,2},
|
||||
{2,3,1,0},
|
||||
{3,2,0,1}
|
||||
};
|
||||
|
||||
Multiplication m1 = new Multiplication(3, m1Table);
|
||||
Addition a1 = new Addition(4, a1Table);
|
||||
Addition a2 = new Addition(4, a2Table);
|
||||
|
||||
Field k4_1 = new Field(4, a1, m1);
|
||||
Field k4_2 = new Field(4, a2, m1);
|
||||
|
||||
System.out.println(k4_1.verifyField() + " this should be true");
|
||||
System.out.println(k4_2.verifyField() + " this should be false");
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
23
source/Multiplication.java
Normal file
23
source/Multiplication.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package serie04;
|
||||
|
||||
public class Multiplication extends Operation {
|
||||
|
||||
public Multiplication (int size, int[][] table) {
|
||||
super(size, table);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int calculate(int indexA, int indexB) {
|
||||
if(indexA == 0 || indexB == 0) {
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
indexA--;
|
||||
indexB--;
|
||||
return super.calculate(indexA, indexB) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
73
source/Operation.java
Normal file
73
source/Operation.java
Normal file
@@ -0,0 +1,73 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user