/* TestRational.java David Klick CIS 260 2018-01-20 Test program for Rational number assignment. */ public class TestRational { public static void main(String[] args) { int errors = 0; Rational[] r = { new Rational(), new Rational(12,15), new Rational(-15,-12), new Rational(2, 3) }; if (r[3].getNumerator() != 2) { System.out.printf("Rational(2, 3) or getNumerator() not working properly: %s%n", r[3].toString()); errors++; } if (r[3].getDenominator() != 3) { System.out.printf("Rational(2, 3) or getDenominator() not working properly: %s%n", r[3].toString()); errors++; } if (r[0].getNumerator() != 1 || r[0].getDenominator() != 1) { System.out.printf("Rational() not working properly: %s%n", r[0].toString()); errors++; } if (r[0].getNumerator() != 1 || r[0].getDenominator() != 1) { System.out.printf("Rational() not working properly: %s%n", r[0].toString()); errors++; } if (r[1].getNumerator() != 4 || r[1].getDenominator() != 5) { System.out.printf("Rational(12, 15) not working properly: %s%n", r[1].toString()); errors++; } if (r[2].getNumerator() != 5 || r[2].getDenominator() != 4) { System.out.printf("Rational(-15, -12) not working properly: %s%n", r[2].toString()); errors++; } r[0].setNums(210, 490); if (r[0].getNumerator() != 3 || r[0].getDenominator() != 7) { System.out.printf("setnums(210, 490) not working properly: %s%n", r[0].toString()); errors++; } r[0].setNums(0, 20); if (r[0].getNumerator() != 0 || r[0].getDenominator() != 1) { System.out.printf("setnums(0, 20) not working properly: %s%n", r[0].toString()); errors++; } r[0].setNums(-17, -13); if (r[0].getNumerator() != 17 || r[0].getDenominator() != 13) { System.out.printf("setnums(-17, -13) not working properly: %s%n", r[0].toString()); errors++; } try { r[0].setNums(5, 0); System.out.printf("setnums(5, 0) not throwing exception: %s%n", r[0].toString()); errors++; } catch (IllegalArgumentException e) { String errmsg = "Invalid rational number: 5/0"; if (!e.getMessage().equals(errmsg)) { System.out.printf("setnums(5, 0) exception message does not match%n"); System.out.printf(" expected: %s%n", errmsg); System.out.printf(" got: %s%n", e.getMessage()); errors++; } } if (Rational.getDisplayStyle() != Rational.DisplayStyle.FRACTIONAL) { System.out.printf("Display style not initialized correctly%n"); errors++; } Rational.setDisplayStyle(Rational.DisplayStyle.FRACTIONAL); if (Rational.getDisplayStyle() != Rational.DisplayStyle.FRACTIONAL) { System.out.printf("setDisplayStyle(FRACTIONAL) not working properly%n"); errors++; } Rational.setDisplayStyle(Rational.DisplayStyle.FLOATING_POINT); if (Rational.getDisplayStyle() != Rational.DisplayStyle.FLOATING_POINT) { System.out.printf("setDisplayStyle(FLOATING_POINT) not working properly%n"); errors++; } r[0].setNums(1, 8); if (!r[0].toString().startsWith("0.125")) { System.out.printf("(new Rational(1, 8)).toString() FLOATING_POINT display not working properly%s%n", r[0].toString()); errors++; } Rational.setDisplayStyle(Rational.DisplayStyle.FRACTIONAL); if (!r[0].toString().equals("1/8")) { System.out.printf("(new Rational(1, 8)).toString() FRACTIONAL display not working properly%s%n", r[0].toString()); errors++; } r[0].setNums(1, 2); r[1].setNums(1, 3); r[2] = r[0].add(r[1]); if (r[0].getNumerator() != 1 || r[0].getDenominator() != 2 || r[1].getNumerator() != 1 || r[1].getDenominator() != 3 || r[2].getNumerator() != 5 || r[2].getDenominator() != 6) { System.out.printf("add(Rational) not working properly: %s + %s => %s%n", r[0].toString(), r[1].toString(), r[2].toString()); errors++; } r[0].setNums(1, 2); r[1].setNums(2, 3); r[2] = r[0].subtract(r[1]); if (r[0].getNumerator() != 1 || r[0].getDenominator() != 2 || r[1].getNumerator() != 2 || r[1].getDenominator() != 3 || r[2].getNumerator() != -1 || r[2].getDenominator() != 6) { System.out.printf("subtract(Rational) not working properly: %s - %s => %s%n", r[0].toString(), r[1].toString(), r[2].toString()); errors++; } r[0].setNums(1, 2); r[1].setNums(2, 3); r[2] = r[0].multiply(r[1]); if (r[0].getNumerator() != 1 || r[0].getDenominator() != 2 || r[1].getNumerator() != 2 || r[1].getDenominator() != 3 || r[2].getNumerator() != 1 || r[2].getDenominator() != 3) { System.out.printf("multiply(Rational) not working properly: %s * %s => %s%n", r[0].toString(), r[1].toString(), r[2].toString()); errors++; } r[0].setNums(2, 3); r[1].setNums(7, 33); r[2] = r[0].divide(r[1]); if (r[0].getNumerator() != 2 || r[0].getDenominator() != 3 || r[1].getNumerator() != 7 || r[1].getDenominator() != 33 || r[2].getNumerator() != 22 || r[2].getDenominator() != 7) { System.out.printf("divide(Rational) not working properly: %s / %s => %s%n", r[0].toString(), r[1].toString(), r[2].toString()); errors++; } r[0].setNums(7, 8); r[1] = r[0].increment(); if (r[0].getNumerator() != 15 || r[0].getDenominator() != 8 || r[1].getNumerator() != 15 || r[1].getDenominator() != 8) { System.out.printf("increment() not working properly: %s + 1 => %s, %s%n", "7/8", r[0].toString(), r[1].toString()); errors++; } r[0].setNums(7, 8); r[1] = r[0].decrement(); if (r[0].getNumerator() != -1 || r[0].getDenominator() != 8 || r[1].getNumerator() != -1 || r[1].getDenominator() != 8) { System.out.printf("decrement() not working properly: %s - 1 => %s, %s%n", "7/8", r[0].toString(), r[1].toString()); errors++; } System.out.printf("%d error%s found by test program%n", errors, (errors == 1 ? "" : "s")); } }