I have to create a program that when used with the test program below should produce the results below. So far I have this please help me. Thank you.
public class Rational {
private int num; // the numerator
private int den; // the denominator
// create and initialize a new Rational object
public Rational(int numerator, int denominator) {
if (denominator == 0) {
throw new RuntimeException("Denominator is zero");
}
int g = gcd(numerator, denominator);
num = numerator / g;
den = denominator / g;
}
// return string representation of (this)
public String toString() {
if (den == 1) { return num + ""; }
else { return num + "/" + den; }
}
// return (this * b)
public Rational times(Rational b) {
return new Rational(this.num * b.num, this.den * b.den);
}
// return (this + b)
public Rational plus(Rational b) {
int numerator = (this.num * b.den) + (this.den * b.num);
int denominator = this.den * b.den;
return new Rational(numerator, denominator);
}
// return (1 / this)
public Rational reciprocal() { return new Rational(den, num); }
// return (this / b)
public Rational divides(Rational b) {
return this.times(b.reciprocal());
}
/************************************************** ***********************
* Helper functions
************************************************** ***********************/
// return gcd(m, n)
private static int gcd(int m, int n) {
if (0 == n) return m;
else return gcd(n, m % n);
}
Test Program:
public class RationalTest
{
private static void report(String fmt, Object... args)
{ System.out.printf(fmt, args); }
public static void main(String[] args)
{
Rational r0 = new Rational(0);
report("r0: %s%n", r0);
Rational r5 = new Rational(5);
report("r5: %s%n", r5);
Rational r05 = new Rational(0, 5);
report("r05: %s%n", r05);
Rational rhalf = new Rational(1, 2);
report("rhalf: %s%n", rhalf);
Rational rhalf5 = new Rational(5, 10);
report("rhalf5: %s%n", rhalf5);
report("rhalf == rhalf5: %s%n", rhalf.equals(rhalf5));
Rational rtwo5 = new Rational(10, 5);
report("rtwo5: %s%n", rtwo5);
Rational rnegn = new Rational(-3, 4);
report("rnegn: %s%n", rnegn);
Rational rnegd = new Rational(3, -4);
report("rnegd: %s%n", rnegd);
Rational rneg2 = new Rational(-3, -4);
report("rneg2: %s%n", rneg2);
report("rden0: ");
try {
Rational rden0 = new Rational(3, 0);
report("%s%n", rden0);
}
catch (IllegalArgumentException iae)
{ System.err.println(iae.getMessage()); }
Rational recip1 = rnegn.reciprocal();
report("recip1: %s%n", recip1);
Rational recip2 = rhalf5.reciprocal();
report("recip2: %s%n", recip2);
Rational recip3 = rtwo5.reciprocal();
report("recip3: %s%n", recip3);
Rational rtwothirds = new Rational(2, 3);
int cmp = rneg2.compareTo(rtwothirds);
String reln = (cmp < 0 ? "<" : (cmp > 0 ? ">" : "="));
report("%s %s %s%n", rneg2, reln, rtwothirds);
cmp = rtwothirds.compareTo(rneg2);
reln = (cmp < 0 ? "<" : (cmp > 0 ? ">" : "="));
report("%s %s %s%n", rtwothirds, reln, rneg2);
Rational rfourth = new Rational(1, 4);
Rational rsixth = new Rational(1, 6);
Rational sum1 = rtwothirds.plus(rfourth);
report("sum1: %s%n", sum1);
Rational sum2 = rtwothirds.plus(rsixth);
report("sum2: %s%n", sum2);
Rational sum3 = rnegn.plus(rnegd);
report("sum3: %s%n", sum3);
Rational diff1 = rtwothirds.minus(rfourth);
report("diff1: %s%n", diff1);
Rational diff2 = rtwothirds.minus(rsixth);
report("diff2: %s%n", diff2);
Rational diff3 = rnegn.minus(rnegd);
report("diff3: %s%n", diff3);
Rational prod1 = rtwothirds.times(rfourth);
report("prod1: %s%n", prod1);
Rational prod2 = rtwothirds.times(rsixth);
report("prod2: %s%n", prod2);
Rational prod3 = rnegn.times(rhalf);
report("prod3: %s%n", prod3);
Rational quot1 = rtwothirds.divide(rfourth);
report("quot1: %s%n", quot1);
Rational quot2 = rtwothirds.divide(rsixth);
report("quot2: %s%n", quot2);
Rational quot3 = rnegn.divide(rhalf);
report("quot3: %s%n", quot3);
report("quot4: ");
try {
Rational quot4 = rnegn.divide(r0);
report("%s%n", quot4);
}
catch (Exception e) { System.err.println(e.getMessage()); }
}
}
Result
r0: 0
r5: 5
r05: 0
rhalf: 1/2
rhalf5: 1/2
rhalf == rhalf5: true
rtwo5: 2
rnegn: -3/4
rnegd: -3/4
rneg2: 3/4
rden0: Cannot construct a Rational with a zero (0) denominator
recip1: -4/3
recip2: 2
recip3: 1/2
3/4 > 2/3
2/3 < 3/4
sum1: 11/12
sum2: 5/6
sum3: -3/2
diff1: 5/12
diff2: 1/2
diff3: 0
prod1: 1/6
prod2: 1/9
prod3: -3/8
quot1: 8/3
quot2: 4
quot3: -3/2
quot4: Cannot construct a Rational with a zero (0) denominator