public class Rational extends Number implements Comparable { //data fields private long numerator = 0; private long denominator = 1; //constructs a default rational public Rational() { this(0, 1); } //constructs specific rational public Rational(long numerator, long denominator) { long gcd = gcd(numerator, denominator); this.numerator = ((denominator > 0) ? 1 : -1) * numerator / gcd; this.denominator = Math.abs(denominator) / gcd; } //finding gcd private static long gcd(long n, long d) { long n1 = Math.abs(n); long n2 = Math.abs(d); int gcd = 1; for (int k = 1; k <= n1 && k <= n2; k++) { if (n1 % k == 0 && n2 % k == 0) gcd = k; } return gcd; } //return numerator public long getNumerator() { return numerator; } //return denominator public long getDenominator() { return denominator; } //add a rational number to this rational public Rational add(Rational secondRational) { long n = numerator * secondRational.getDenominator() + denominator * secondRational.getNumerator(); long d = denominator * secondRational.getDenominator(); return new Rational(n, d); } //subtracting a rational from this rational public Rational subtract(Rational secondRational) { long n = numerator * secondRational.getDenominator() - denominator * secondRational.getNumerator(); long d = denominator * secondRational.getDenominator(); return new Rational(n, d); } //multiply a rational to this rational public Rational multiply(Rational secondRational) { long n = numerator * secondRational.getNumerator(); long d = denominator * secondRational.getDenominator(); return new Rational(n, d); } //divide a rational from this rational public Rational divide(Rational secondRational) { long n = numerator * secondRational.getDenominator(); long d = denominator * secondRational.numerator; return new Rational(n, d); } //override toString method public String toString() { if (denominator == 1) return numerator + ""; else return numerator + "/" + denominator; } //override equals method in Object class public boolean equals(Object parm1) { if ((this.subtract((Rational)(parm1))).getNumerator() == 0) return true; else return false; } //implement abstract intValue method in java.lang.Number public int intValue() { return (int)doubleValue(); } //implement abstract floatValue method in java.lang.Number public float floatValue() { return (float)doubleValue(); } //implement doubleValue method in java.lang.Number public double doubleValue() { return numerator * 1.0 / denominator; } //implement abstract longValue method in java.lang.Number public long longValue() { return (long)doubleValue(); } //implement compareTo method in java.lang.Comparable public int compareTo(Object o) { if ((this.subtract((Rational)o)).getNumerator() > 0) return 1; else if ((this.subtract((Rational)o)).getNumerator() < 0) return -1; else return 0; } }

That works fine.... Now, this is the tester for it.

import java.util.*; import java.io.*; public class RationalTester extends Rational { //main method public static void main(String[] args) { //create and initialize Rational c1 = new Rational(4, 2); Rational c2 = new Rational(2, 3); //output results System.out.println(c1 + " + " + c2 + " = " + c1.add(c2)); System.out.println(c1 + " - " + c2 + " = " + c1.subtract(c2)); System.out.println(c1 + " * " + c2 + " = " + c1.multiply(c2)); System.out.println(c1 + " / " + c2 + " = " + c1.divide(c2)); System.out.println(c2 + " is " + c2.doubleValue()); } }

And that works just fine...

Here is my output:

----jGRASP exec: javac -g C:\Users\Christopher\Documents\Rational.java

----jGRASP: operation complete.

----jGRASP exec: javac -g C:\Users\Christopher\Documents\RationalTester.java

----jGRASP: operation complete.

----jGRASP exec: java RationalTester

2 + 2/3 = 8/3

2 - 2/3 = 4/3

2 * 2/3 = 4/3

2 / 2/3 = 3

2/3 is 0.6666666666666666

----jGRASP: operation complete.

But, I am now having a lot of trouble trying to get this other tester to work... I need it to add fractions, like so, 1/2 + 2/3 + 3/4 ... and so forth till 99/100. This is all I can get it to do though... Do I need another loop?

import java.io.*; import java.util.*; public class Fraction extends Rational { public static void main(String[] args) { int n = 1; int d = 2; int count = 0; Rational r = new Rational(n, d); for (int now = 0; now < 98; now++) { n++; d++; } r = new Rational(n, d); System.out.println(r); } }

And this is my output...

----jGRASP exec: javac -g C:\Users\Christopher\Documents\Fraction.java

----jGRASP: operation complete.

----jGRASP exec: java Fraction

99/100

----jGRASP: operation complete.