import java.math.BigInteger; /** * This is a class for the creation of immutible objects that * represent fractions. Fractions are maintained in their reduced * form with any negative in the numerator. */ public class BigFraction implements Comparable { /** * This is the BigFraction 0/1. */ public static final BigFraction ZERO; /** * This is the BigFraction 1/1. */ public static final BigFraction ONE; /** * This is the BigFraction 1/2. */ public static final BigFraction HALF; /** * This is the BigFraction 2/1. */ public static final BigFraction TWO; static { ZERO = BigFraction.valueOf(0, 1); ONE = BigFraction.valueOf(1, 1); HALF = BigFraction.valueOf(1, 2); TWO = BigFraction.valueOf(2, 1); } //state variables private final BigInteger num; private final BigInteger denom; /** * This creates a BigFraction with the specified numerator and denominator * @param num The numerator we are giving this BigFraction * @param denom The denominator we are giving this BigFraction. * @throws IllegalArgumentException if a zero denominator is used. */ public BigFraction(BigInteger num, BigInteger denom) { if(denom.equals(BigInteger.ZERO)) { throw new IllegalArgumentException(); } if(denom.compareTo(BigInteger.ZERO) < 0) { num = num.negate(); denom = denom.negate(); } BigInteger d = num.gcd(denom); num = num.divide(d); denom = denom.divide(d); this.num = num; this.denom = denom; } /** * This creates a BigFraction with the specified numerator and denominator * @param num The numerator we are giving this BigFraction represented as a numerical string * @param denom The denominator we are giving this BigFraction represented asa numerical string. * @throws IllegalArgumentException if a zero denominator is used. */ public BigFraction(String num, String denom) { this(new BigInteger(num), new BigInteger(denom)); } /** * This gives a string representation for this BigFraction of the form num/denom. * @return a string containing num/denom */ @Override public String toString() { return String.format("%s/%s", num, denom); } /** * This checks for equality. * @param o an objec we are comapring this BigFraction to * @return false if o is not a BigFraction, true if o is numerically * equal to this BigFraction, and false otherwise. */ public boolean equals(Object o) { if( !(o instanceof Object)) { return false; } BigFraction that = (BigFraction) o; return num.equals(that.num) && num.equals(that.denom); } /** * This computes this + that. * @param that the BigFraction we are adding to this BigFraction. * @return this + that */ public BigFraction add(BigFraction that) { BigInteger top = num.multiply(that.denom).add(denom.multiply(that.num)); BigInteger bottom = denom.multiply(that.denom); return new BigFraction(top, bottom); } /** * This computes this - that. * @param that the BigFraction we are adding to this BigFraction. * @return this - that */ public BigFraction subtract(BigFraction that) { BigInteger top = num.multiply(that.denom).subtract(denom.multiply(that.num)); BigInteger bottom = denom.multiply(that.denom); return new BigFraction(top, bottom); } /** * This computes this * that. * @param that the BigFraction we are adding to this BigFraction. * @return this * that */ public BigFraction multiply(BigFraction that) { BigInteger top = num.multiply(that.num); BigInteger bottom = denom.multiply(that.denom); return new BigFraction(top, bottom); } /** * This computes this / that. * @param that the BigFraction we are adding to this BigFraction. * @return this / that */ public BigFraction divide(BigFraction that) { BigInteger top = num.multiply(that.denom); BigInteger bottom = denom.multiply(that.num); return new BigFraction(top, bottom); } /** * This raises this BigFraction to the nth power * @param n an integer * @return thisn */ public BigFraction pow(int n) { if(n >= 0) { return new BigFraction (num.pow(n), denom.pow(n)); } else { n = -n; return new BigFraction (denom.pow(n), num.pow(n)); } } /** * This finds the fractional part of this BigFraction * @return the fractional part of this BigFraction */ public BigFraction fractionalPart() { BigInteger top = num.mod(denom); return new BigFraction(top, denom); } /** * This static factory method creates a BigFraction from two * specified longs. * @param num the numerator of this BigFraction * @param denom the denominator of this BigFraction * @return the BigFraction num/denom * @throws IllegalArgumentException if a zero denominator is used. */ public static BigFraction valueOf(long num, long denom) { return new BigFraction(BigInteger.valueOf(num), BigInteger.valueOf(denom)); } /** * This compares this to that. * @param that a BigFraction we are comparing this BigFraction to. * @return a positive integer if this > that, a negative integer * if this < that, and 0 if this and that have the same * numerical value. */ public int compareTo(BigFraction that) { return 0; } public static void main(String[] args) { BigFraction out = BigFraction.ZERO; for(int k = 1; k <= 1001; k++) { out = out.add(BigFraction.valueOf(1, k)); } System.out.println(out); } }