Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //The Rational class public class
- public class Rational
- {
- private int n;
- private int d;
- /**
- * The default constructor for objects of class Rational. Creates the rational number 1.
- */
- public Rational()
- {
- n = 1;
- d = 1;
- }
- /**
- * The alternate constructor for objects of class Rational. Creates a rational number equivalent to n/d.
- * @param n The numerator of the rational number.
- * @param d The denominator of the rational number.
- */
- public Rational(int n, int d)
- {
- if(d == 0){
- throw new ZeroDenominatorException
- }
- this.n = n;
- this.d = d;
- /**
- * Get the value of the Numerator
- *
- * @return the value of the numerator
- */
- public int getNumerator()
- {
- return n;
- }
- /**
- * Get the value of the Denominator
- *
- * @return the value of the denominator
- */
- public int getDenominator()
- {
- return d;
- }
- /**
- * Negate a rational number r
- *
- * @return a new rational number that is negation of this number -r
- */
- public Rational negate()
- {
- Rational r = new Rational (-this.n, this.d);
- return r;
- }
- /**
- * Invert a rational number r
- *
- * @return a new rational number that is 1/r.
- */
- public Rational invert()
- {
- int newN = this.d;
- int newD = this.n;
- return new Rational (newN, newD);
- }
- /**
- * Add two rational numbers
- *
- * @param other the second argument of the add
- * @return a new rational number that is the sum of this and the other rational
- */
- public Rational add(Rational other)
- {
- int newN = (this.n*other.d+this.d*other.n);
- int newD = (this.d*other.d);
- return new Rational (newN, newD);
- }
- /**
- * Subtract a rational number t from this one r
- *
- * @param other the second argument of subtract
- * @return a new rational number that is r-t
- */
- public Rational subtract(Rational other)
- {
- int newN = (this.n*other.d-this.d*other.n);
- int newD = (this.d*other.d);
- return new Rational (newN, newD);
- }
- /**
- * Multiply two rational numbers
- *
- * @param other the second argument of multiply
- * @return a new rational number that is the sum of this object and the other rational.
- */
- public Rational multiply(Rational other)
- {
- int newN = (this.n*other.n);
- int newD = (this.d*other.d);
- return new Rational (newN, newD);
- }
- /**
- * Divide this rational number r by another one t
- *
- * @param other the second argument of divide
- * @return a new rational number that is r/t
- */
- public Rational divide(Rational other)
- {
- int newN = (this.n*other.d);
- int newD = (this.d*other.n);
- return new Rational(newN, newD);
- }
- /**
- * Put the rational number in normal form where the numerator
- * and the denominator share no common factors. Guarantee that only the numerator
- * is negative.
- *
- */
- private void normalize()
- {
- int absN = Math.abs(n);
- int absD= Math.abs(d);
- int signD = d/absD;
- int GCD = gcd(absN, absD);
- n = (n / GCD) * signD;
- d = (d / GCD) * signD;
- }
- /**
- * Recursively compute the greatest common divisor of two positive integers
- *
- * @param a the first argument of gcd
- * @param b the second argument of gcd
- * @return the gcd of the two arguments
- */
- private int gcd(int a, int b)
- {
- int result = 0;
- if(a<b)
- result = gcd(b,a);
- else if(b==0)
- result = a;
- else
- {
- int remainder = a % b;
- result = gcd(b, remainder);
- }
- return result;
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------------
- public class ZeroDenominatorException extends RuntimeException
- {
- public ZeroDenominatorException(String reason)
- {
- super(reason);
- }
- }
- ---------------------------------------------------------------------------------------------------------------------------------------
- public class RationalTest
- {
- public static void main (String args[])
- {
- testConstructor();
- testNegate();
- testInvert();
- testAddSubtract();
- testMultiplyDivide();
- }
- public static void testConstructor()
- {
- System.out.println("TESTING the constructor, getNumerator, getDenominator");
- System.out.println("Trying default constructor");
- Rational r0 = new Rational();
- if(r0.getNumerator() == 1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not 1");
- }
- if(r0.getDenominator() == 1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 1");
- }
- System.out.println("Constructing 2/5");
- try{
- Rational r1 = new Rational(2, 5);
- System.out.println(" Passes");
- if(r1.getNumerator() == 2)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not 2");
- }
- if(r1.getDenominator() == 5)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 5");
- }
- }
- catch(ZeroDenominatorException e)
- {
- System.out.println("**** Fails - exception thrown");
- }
- System.out.println("Trying 2/0");
- try{
- Rational r1 = new Rational(2, 0);
- System.out.println("**** Fails - no exception thrown");
- }
- catch(ZeroDenominatorException e)
- {
- System.out.println(" Passes");
- }
- System.out.println("Trying 42/30");
- Rational r2 = new Rational(42, 30);
- if(r2.getNumerator() == 7)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not 7");
- }
- if(r2.getDenominator() == 5)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 5");
- }
- System.out.println("Trying 6/-3");
- Rational r3 = new Rational(6, -3);
- if(r3.getNumerator() == -2)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not -2");
- }
- if(r3.getDenominator() == 1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 1");
- }
- System.out.println("Trying -6/-3");
- Rational r4 = new Rational(-6, -3);
- if(r4.getNumerator() == 2)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not 2");
- }
- if(r4.getDenominator() == 1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 1");
- }
- System.out.println("Trying -6/3");
- Rational r5 = new Rational(-6, 3);
- if(r5.getNumerator() == -2)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not -2");
- }
- if(r5.getDenominator() == 1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 1");
- }
- System.out.println("Trying 0/3");
- Rational r6 = new Rational(0, 3);
- if(r6.getNumerator() == 0)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not 0");
- }
- if(r6.getDenominator() == 1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 1");
- }
- System.out.println("Constructor tests finished");
- }
- public static void testNegate()
- {
- System.out.println();
- System.out.println();
- System.out.println("TESTING the negate method");
- System.out.println("Negate 1/2");
- Rational r1 = new Rational(1, 2);
- Rational r2 = r1.negate();
- if(r1.getNumerator() == 1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed numerator of negate argument");
- }
- if(r1.getDenominator() == 2)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed denominator of negate argument");
- }
- if(r2.getNumerator() == -1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not -1");
- }
- if(r2.getDenominator() == 2)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 2");
- }
- System.out.println("Negate -2/3");
- r1 = new Rational(-2, 3);
- r2 = r1.negate();
- if(r1.getNumerator() == -2)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed numerator of negate argument");
- }
- if(r1.getDenominator() == 3)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed denominator of negate argument");
- }
- if(r2.getNumerator() == 2)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not 2");
- }
- if(r2.getDenominator() == 3)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 3");
- }
- System.out.println("Negate tests finished");
- }
- public static void testInvert()
- {
- System.out.println();
- System.out.println();
- System.out.println("TESTING the invert method");
- System.out.println("Invert 1/2");
- Rational r1 = new Rational(1, 2);
- Rational r2 = r1.invert();
- if(r1.getNumerator() == 1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed numerator of negate argument");
- }
- if(r1.getDenominator() == 2)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed denominator of negate argument");
- }
- if(r2.getNumerator() == 2)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not 2");
- }
- if(r2.getDenominator() == 1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 1");
- }
- System.out.println("Invert -2/3");
- r1 = new Rational(-2, 3);
- r2 = r1.invert();
- if(r1.getNumerator() == -2)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed numerator of negate argument");
- }
- if(r1.getDenominator() == 3)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed denominator of negate argument");
- }
- if(r2.getNumerator() == -3)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not -3");
- }
- if(r2.getDenominator() == 2)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 2");
- }
- System.out.println("Invert 0/5");
- r1 = new Rational(0, 5);
- try
- {
- r2 = r1.invert();
- System.out.println("**** Fails - did not throw zero denominator exception");
- }
- catch(ZeroDenominatorException e)
- {
- System.out.println(" Passes");
- }
- System.out.println("Invert tests finished");
- }
- public static void testAddSubtract()
- {
- System.out.println();
- System.out.println();
- System.out.println("TESTING the add and subtract methods");
- System.out.println("Adding 1/2 and 1/2");
- Rational r1 = new Rational(1, 2);
- Rational r2 = r1.add(r1);
- if(r1.getNumerator() == 1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed numerator of add argument");
- }
- if(r1.getDenominator() == 2)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed denominator of add argument");
- }
- if(r2.getNumerator() == 1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not 1");
- }
- if(r2.getDenominator() == 1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 1");
- }
- System.out.println("Adding 4/7 and 3/5");
- r1 = new Rational(4, 7);
- r2 = new Rational(3, 5);
- Rational r3 = r1.add(r2);
- if(r1.getNumerator() == 4)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed numerator of first add argument");
- }
- if(r1.getDenominator() == 7)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed denominator of first add argument");
- }
- if(r2.getNumerator() == 3)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed numerator of second add argument");
- }
- if(r2.getDenominator() == 5)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed denominator of second add argument");
- }
- if(r3.getNumerator() == 41)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not 41");
- }
- if(r3.getDenominator() == 35)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 35");
- }
- System.out.println("Adding 1/2 and 1/6");
- r1 = new Rational(1, 2);
- r2 = new Rational(1, 6);
- r3 = r1.add(r2);
- if(r3.getNumerator() == 2)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not 2");
- }
- if(r3.getDenominator() == 3)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 3");
- }
- System.out.println("Subtracting 1/2 and 1/2");
- r1 = new Rational(1, 2);
- r2 = r1.subtract(r1);
- if(r1.getNumerator() == 1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed numerator of subtract argument");
- }
- if(r1.getDenominator() == 2)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed denominator of subtract argument");
- }
- if(r2.getNumerator() == 0)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not 0");
- }
- if(r2.getDenominator() == 1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 1");
- }
- System.out.println("Subtracting 4/7 and 3/5");
- r1 = new Rational(4, 7);
- r2 = new Rational(3, 5);
- r3 = r1.subtract(r2);
- if(r1.getNumerator() == 4)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed numerator of first subtract argument");
- }
- if(r1.getDenominator() == 7)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed denominator of first subtract argument");
- }
- if(r2.getNumerator() == 3)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed numerator of second subtract argument");
- }
- if(r2.getDenominator() == 5)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed denominator of second subtract argument");
- }
- if(r3.getNumerator() == -1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not -1");
- }
- if(r3.getDenominator() == 35)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 35");
- }
- System.out.println("Subtracting 1/2 and 1/6");
- r1 = new Rational(1, 2);
- r2 = new Rational(1, 6);
- r3 = r1.subtract(r2);
- if(r3.getNumerator() == 1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not 1");
- }
- if(r3.getDenominator() == 3)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 3");
- }
- System.out.println("Add/Subtract tests finished");
- }
- public static void testMultiplyDivide()
- {
- System.out.println();
- System.out.println();
- System.out.println("TESTING the multiply and divide methods");
- System.out.println("Multiply 1/2 and 1/2");
- Rational r1 = new Rational(1, 2);
- Rational r2 = r1.multiply(r1);
- if(r1.getNumerator() == 1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed numerator of add argument");
- }
- if(r1.getDenominator() == 2)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed denominator of add argument");
- }
- if(r2.getNumerator() == 1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not 1");
- }
- if(r2.getDenominator() == 4)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 4");
- }
- System.out.println("Multiply 5/7 and 3/5");
- r1 = new Rational(5, 7);
- r2 = new Rational(3, 5);
- Rational r3 = r1.multiply(r2);
- if(r1.getNumerator() == 5)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed numerator of first add argument");
- }
- if(r1.getDenominator() == 7)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed denominator of first add argument");
- }
- if(r2.getNumerator() == 3)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed numerator of second add argument");
- }
- if(r2.getDenominator() == 5)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed denominator of second add argument");
- }
- if(r3.getNumerator() == 3)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not 3");
- }
- if(r3.getDenominator() == 7)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 7");
- }
- System.out.println("Multiply 1/2 and 0/1");
- r1 = new Rational(1, 2);
- r2 = new Rational(0, 1);
- r3 = r1.multiply(r2);
- if(r3.getNumerator() == 0)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not 0");
- }
- if(r3.getDenominator() == 1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 1");
- }
- System.out.println("Dividing 1/2 by 1/2");
- r1 = new Rational(1, 2);
- r2 = r1.divide(r1);
- if(r1.getNumerator() == 1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed numerator of subtract argument");
- }
- if(r1.getDenominator() == 2)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed denominator of subtract argument");
- }
- if(r2.getNumerator() == 1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not 1");
- }
- if(r2.getDenominator() == 1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 1");
- }
- System.out.println("Dividing 4/7 by 3/28");
- r1 = new Rational(4, 7);
- r2 = new Rational(3, 28);
- r3 = r1.divide(r2);
- if(r1.getNumerator() == 4)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed numerator of first subtract argument");
- }
- if(r1.getDenominator() == 7)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed denominator of first subtract argument");
- }
- if(r2.getNumerator() == 3)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed numerator of second subtract argument");
- }
- if(r2.getDenominator() == 28)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - changed denominator of second subtract argument");
- }
- if(r3.getNumerator() == 16)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not 16");
- }
- if(r3.getDenominator() == 3)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 3");
- }
- System.out.println("Dividing 1/2 by 1/6");
- r1 = new Rational(1, 2);
- r2 = new Rational(1, 6);
- r3 = r1.divide(r2);
- if(r3.getNumerator() == 3)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - numerator not 3");
- }
- if(r3.getDenominator() == 1)
- {
- System.out.println(" Passes");
- }
- else
- {
- System.out.println("**** Fails - denominator not 1");
- }
- System.out.println("Dividing 1/2 by 0/1");
- r1 = new Rational(1, 2);
- r2 = new Rational(0, 1);
- try
- {
- r3 = r1.divide(r2);
- System.out.println("**** Fails - did not throw zero denominator exception");
- }
- catch(ZeroDenominatorException e)
- {
- System.out.println(" Passes");
- }
- System.out.println("Multiply/Divide tests finished");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement