java lab2 improvements
This commit is contained in:
parent
b217f2f5b8
commit
2f61515fb7
2 changed files with 49 additions and 13 deletions
|
@ -15,19 +15,19 @@ public class GF {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public GF addition(final GF rhs) throws IllegalArgumentException {
|
public GF add(final GF rhs) throws IllegalArgumentException {
|
||||||
verifyCharacteristics(this, rhs);
|
verifyCharacteristics(this, rhs);
|
||||||
|
|
||||||
return new GF((this.value + rhs.value) % this.characteristic, this.characteristic);
|
return new GF((this.value + rhs.value) % this.characteristic, this.characteristic);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void additionAssignment(final GF rhs) throws IllegalArgumentException {
|
public void addAssign(final GF rhs) throws IllegalArgumentException {
|
||||||
verifyCharacteristics(this, rhs);
|
verifyCharacteristics(this, rhs);
|
||||||
|
|
||||||
this.value = this.addition(rhs).value;
|
this.value = this.add(rhs).value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public GF subtraction(final GF rhs) throws IllegalArgumentException {
|
public GF subtract(final GF rhs) throws IllegalArgumentException {
|
||||||
verifyCharacteristics(this, rhs);
|
verifyCharacteristics(this, rhs);
|
||||||
if (this.value < rhs.value) {
|
if (this.value < rhs.value) {
|
||||||
return new GF(this.characteristic - (rhs.value - this.value), this.characteristic);
|
return new GF(this.characteristic - (rhs.value - this.value), this.characteristic);
|
||||||
|
@ -35,40 +35,55 @@ public class GF {
|
||||||
return new GF((this.value - rhs.value), this.characteristic);
|
return new GF((this.value - rhs.value), this.characteristic);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void subtractionAssignment(final GF rhs) throws IllegalArgumentException {
|
public void subtractAssign(final GF rhs) throws IllegalArgumentException {
|
||||||
verifyCharacteristics(this, rhs);
|
verifyCharacteristics(this, rhs);
|
||||||
|
|
||||||
this.value = this.subtraction(rhs).value;
|
this.value = this.subtract(rhs).value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public GF multiplication(final GF rhs) throws IllegalArgumentException {
|
public GF multiply(final GF rhs) throws IllegalArgumentException {
|
||||||
verifyCharacteristics(this, rhs);
|
verifyCharacteristics(this, rhs);
|
||||||
|
|
||||||
return new GF((this.value * rhs.value) % this.characteristic, this.characteristic);
|
return new GF((this.value * rhs.value) % this.characteristic, this.characteristic);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void multiplicationAssignment(final GF rhs) throws IllegalArgumentException {
|
public void multiplyAssign(final GF rhs) throws IllegalArgumentException {
|
||||||
verifyCharacteristics(this, rhs);
|
verifyCharacteristics(this, rhs);
|
||||||
|
|
||||||
this.value = this.multiplication(rhs).value;
|
this.value = this.multiply(rhs).value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public GF division(final GF rhs) throws IllegalArgumentException {
|
public GF divide(final GF rhs) throws IllegalArgumentException {
|
||||||
verifyCharacteristics(this, rhs);
|
verifyCharacteristics(this, rhs);
|
||||||
|
if (rhs.value == 0) {
|
||||||
|
throw new IllegalArgumentException("division by 0 is illegal");
|
||||||
|
}
|
||||||
|
|
||||||
return new GF((this.value / rhs.value) % this.characteristic, this.characteristic);
|
int inverse = 1;
|
||||||
|
for (int i = 1; i < this.characteristic; i++) {
|
||||||
|
if ((rhs.value * i) % this.characteristic == 1) {
|
||||||
|
inverse = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new GF((this.value * inverse) % this.characteristic, this.characteristic);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void divisionAssignment(final GF rhs) throws IllegalArgumentException {
|
public void divideAssign(final GF rhs) throws IllegalArgumentException {
|
||||||
verifyCharacteristics(this, rhs);
|
verifyCharacteristics(this, rhs);
|
||||||
|
|
||||||
this.value = this.division(rhs).value;
|
this.value = this.divide(rhs).value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getCharacteristic() {
|
public int getCharacteristic() {
|
||||||
return this.characteristic;
|
return this.characteristic;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getValue() {
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (obj instanceof GF rhs) {
|
if (obj instanceof GF rhs) {
|
||||||
|
@ -82,6 +97,18 @@ public class GF {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int compare(GF rhs) throws IllegalArgumentException {
|
||||||
|
verifyCharacteristics(this, rhs);
|
||||||
|
|
||||||
|
if (this.value < rhs.value) {
|
||||||
|
return -1;
|
||||||
|
} else if (this.value > rhs.value) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "GF<" + this.characteristic + ">(" + this.value + ")";
|
return "GF<" + this.characteristic + ">(" + this.value + ")";
|
||||||
|
|
|
@ -1,6 +1,15 @@
|
||||||
package pl.jacekpoz;
|
package pl.jacekpoz;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
GF a = new GF(1234577, scanner.nextInt());
|
||||||
|
GF b = new GF(1234577, scanner.nextInt());
|
||||||
|
|
||||||
|
System.out.println(a + " + " + b + " = " + (a.add(b)));
|
||||||
|
System.out.println(a + " - " + b + " = " + (a.subtract(b)));
|
||||||
|
System.out.println(a + " * " + b + " = " + (a.multiply(b)));
|
||||||
|
System.out.println(a + " / " + b + " = " + (a.divide(b)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue