Question: Implement the following 3 java classes to refresh your Java programming knowledge IntMod (Integer Modulo) Rat (Rational number) Euclidean Can someone help me with the
Implement the following 3 java classes to refresh your Java programming knowledge
IntMod (Integer Modulo)
Rat (Rational number)
Euclidean
Can someone help me with the unimplemented methods?
........................................................................................................................
public class App { public static void main(String[] args) { UnitTest.testInt(); UnitTest.testIntMod(); UnitTest.testRat(); } }
........................................................................................................................
public class Comp { public static boolean ge(Object a, Object b) { //greater than or equal to Ordered _a = (Ordered)a; Ordered _b = (Ordered)b; return _a.ge(_b); } public static boolean gt(Object a, Object b) { //greater than return ge(a, b) && ne(a, b); } public static boolean le(Object a, Object b) { //less than or equal to return ge(b, a); } public static boolean lt(Object a, Object b) { //less than return ge(b, a) && ne(a, b); } public static boolean eq(Object a, Object b) { //equal return ge(a, b) && ge(b, a); } public static boolean ne(Object a, Object b) { //not equal return !eq(a, b); } } ........................................................................................................................
public class Euclidean { protected static
public interface Field extends Ring { public Ring mulIdentity(); public Ring mulInverse() throws ArithmeticException; } ........................................................................................................................
//Integer public class Int implements Ring, Modulo, Ordered { private int n; private static Int addId; static { addId = new Int(0); } public Int(int n) { this.n = n; } public int getInt() { return n; } //Ring public Ring add(Ring a) { return new Int(n + ((Int)a).n); } public Ring addIdentity() { return addId; } public Ring addInverse() { return new Int(-n); } public Ring mul(Ring a) { return new Int(n * ((Int)a).n); } //Modulo public Ring mod(Ring a) { return new Int(n % ((Int)a).n); } public Ring quo(Ring a) { return new Int(n / ((Int)a).n); } //Ordered public boolean ge(Ordered a) { return n >= ((Int)a).n; }
........................................................................................................................
//Integer modulo m (m is a prime number) // IntMod(7, 5) is 7 in modulo 5 system // IntMod(7, 5) is equivalent to IntMod(2, 5) // public class IntMod implements Field, Ordered { private int n, m; public IntMod(int n, int m) { if(m <= 0) throw new IllegalArgumentException("Not a positive divisor"); n = n % m; n = n < 0 ? n + m : n; //if n is negative, make it positive this.n = n; this.m = m; } public int getInt() { return n; } public int getMod() { return m; } } ........................................................................................................................
public interface Modulo { public Ring mod(Ring a); //remainder public Ring quo(Ring a); //quotient }
........................................................................................................................
public interface Ordered { public boolean ge(Ordered a); //greater than or equal to }
...............................................................................................................................
//Rational number // Rat(10, 15) is 10/15 and is equivalent to Rat(2, 3) // public class Rat implements Field, Modulo, Ordered { int n, d; public Rat(int numerator, int denumerator) { //TODO: make numerator and denominator prime to //each other using Euclidean.gcd } //Modulo public Ring mod(Ring a) { Rat r = (Rat)a; if(r.n == 0) throw new ArithmeticException("Division by zero"); return new Rat((n*r.d) % (d*r.n), d*r.d); } //Ordered public boolean ge(Ordered a) { Rat r = (Rat)a; return n*r.d >= d*r.n; } } .........................................................................................................................
public interface Ring { public Ring add(Ring a); public Ring addIdentity(); public Ring addInverse(); public Ring mul(Ring a); } ..........................................................................................................
public class UnitTest { //Ring public static boolean testAddCommutativity(Ring a, Ring b) { Ring x = a.add(b); Ring y = b.add(a); return Comp.eq(x, y); } public static boolean testAddAssociativity(Ring a, Ring b, Ring c) { Ring x = a.add(b.add(c)); Ring y = a.add(b).add(c); return Comp.eq(x, y); } public static boolean testAddIdentity(Ring a) { Ring x = a.add(a.addIdentity()); Ring y = a.addIdentity().add(a); return Comp.eq(x, a) && Comp.eq(y, a); } public static boolean testAddInverse(Ring a) { Ring b = a.addInverse(); Ring x = a.add(b); Ring y = b.add(a); return Comp.eq(x, a.addIdentity()) && Comp.eq(y, a.addIdentity()); } public static boolean testMulAssociativity(Ring a, Ring b, Ring c) { Ring x = a.mul(b.mul(c));
It would be really helpful :(
Thank you in advance :)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
