From 2f61515fb71912fbed5f2a4472de8d8e6b80ab2b Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Fri, 12 Apr 2024 10:21:55 +0200 Subject: [PATCH] java lab2 improvements --- .../app/src/main/java/pl/jacekpoz/GF.java | 53 ++++++++++++++----- .../app/src/main/java/pl/jacekpoz/Main.java | 9 ++++ 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/lab2/zad2/app/src/main/java/pl/jacekpoz/GF.java b/lab2/zad2/app/src/main/java/pl/jacekpoz/GF.java index d47e998..a21c18b 100644 --- a/lab2/zad2/app/src/main/java/pl/jacekpoz/GF.java +++ b/lab2/zad2/app/src/main/java/pl/jacekpoz/GF.java @@ -15,19 +15,19 @@ public class GF { this.value = value; } - public GF addition(final GF rhs) throws IllegalArgumentException { + public GF add(final GF rhs) throws IllegalArgumentException { verifyCharacteristics(this, rhs); 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); - 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); if (this.value < rhs.value) { 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); } - public void subtractionAssignment(final GF rhs) throws IllegalArgumentException { + public void subtractAssign(final GF rhs) throws IllegalArgumentException { 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); 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); - 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); + 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); - this.value = this.division(rhs).value; + this.value = this.divide(rhs).value; } public int getCharacteristic() { return this.characteristic; } + public int getValue() { + return this.value; + } + @Override public boolean equals(Object obj) { if (obj instanceof GF rhs) { @@ -82,6 +97,18 @@ public class GF { 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 public String toString() { return "GF<" + this.characteristic + ">(" + this.value + ")"; diff --git a/lab2/zad2/app/src/main/java/pl/jacekpoz/Main.java b/lab2/zad2/app/src/main/java/pl/jacekpoz/Main.java index 6681e1a..6a90155 100644 --- a/lab2/zad2/app/src/main/java/pl/jacekpoz/Main.java +++ b/lab2/zad2/app/src/main/java/pl/jacekpoz/Main.java @@ -1,6 +1,15 @@ package pl.jacekpoz; +import java.util.Scanner; public class Main { 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))); } }