Results 1 to 5 of 5
 03062011, 04:41 PM #1Member
 Join Date
 Mar 2011
 Posts
 6
 Rep Power
 0
BigInteger class in “the art & science of java
Hi, I' m new to java. when I do the exercise in the book "the art and science of java", I come across a problem about BigInteger class.
Here is question:
The implementation of the Rational class given in this chapter is not particularly useful in practice because it doesn’t allow the numerator and denominator to exceed the size of an integer, even though larger values tend to occur quite often in rational arithmetic. One way to avoid the problem is to use the BigInteger class in the java.math package, which defines an extended type of integer that can take on arbitrarily large values. Rewrite the implementation of Rational so that the private instance variables num and den are declared as BigIntegers instead of ints, but without changing the argument and result types of any of the public methods. To learn how BigInteger works, consult the javadoc page.
Java Code:import acm.util.*; /** * The Rational class is used to represent rational numbers, which * are defined to be the quotient of two integers. */ public class Rational { /** * Creates a new Rational initialized to zero. */ public Rational() { this(0); } /** * Creates a new Rational from the integer argument. * @param n The initial value */ public Rational(int n) { this(n, 1); } /** * Creates a new Rational with the value x / y. * @param x The numerator of the rational number * @param y The denominator of the rational number */ public Rational(int x, int y) { if (y == 0) throw new ErrorException("Division by 0"); int g = gcd(Math.abs(x), Math.abs(y)); num = x / g; den = Math.abs(y) / g; if (y < 0) num = num; } /** * Adds the rational number r to this one and returns the sum. * @param r The rational number to be added * @return The sum of the current number and r */ public Rational add(Rational r) { return new Rational(this.num * r.den + r.num * this.den, this.den * r.den); } /** * Subtracts the rational number r from this one. * @param r The rational number to be subtracted * @return The result of subtracting r from the current number */ public Rational subtract(Rational r) { return new Rational(this.num * r.den  r.num * this.den, this.den * r.den); } /** * Multiplies this number by the rational number r. * @param r The rational number used as a multiplier * @return The result of multiplying the current number by r */ public Rational multiply(Rational r) { return new Rational(this.num * r.num, this.den * r.den); } /** * Divides this number by the rational number r. * @param r The rational number used as a divisor * @return The result of dividing the current number by r */ public Rational divide(Rational r) { return new Rational(this.num * r.den, this.den * r.num); } /** * Creates a string representation of this rational number. * @return The string representation of this rational number */ public String toString() { if (den == 1) { return "" + num; } else { return num + "/" + den; } } /** * Calculates the greatest common divisor using Euclid's algorithm. * @param x First integer * @param y Second integer * @return The greatest common divisor of x and y */ private int gcd(int x, int y) { int r = x % y; while (r != 0) { x = y; y = r; r = x % y; } return y; } /* Private instance variables */ private int num; /* The numerator of this Rational */ private int den; /* The denominator of this Rational */ }
Here is my code:
Java Code:import acm.util.*; import java.math.*; public class Rational { public Rational() { this(0); } public Rational(int n) { this(n, 1); } public Rational(int x, int y) { if (y == 0) throw new ErrorException("Division by 0"); int g = gcd(Math.abs(x), Math.abs(y)); num = a/g; den = Math.abs(y)/g; if (y < 0) num.negate(); } public Rational add(Rational r) { this.num.multiply( r.den) .add(r.num .multiply( this.den)); this.den .multiply( r.den); return new Rational(num.intValue(),den.intValue()); } public Rational subtract(Rational r) { this.num .multiply( r.den ).subtract ( r.num .multiply( this.den)); this.den .multiply( r.den); return new Rational (num.intValue(),den.intValue()); } public Rational multiply(Rational r) { this.num .multiply( r.num ); this.den .multiply( r.den); return new Rational (num.intValue(),den.intValue()); } public Rational divide(Rational r) { this.num .multiply( r.den ); this.den .multiply( r.num); return new Rational (num.intValue(),den.intValue()); } public String toString() { if (den .equals(new BigInteger ("1"))) { return "" + num; } else { return num + "/" + den; } } private int gcd(int x, int y) { int r = x % y; while (r != 0) { x = y; y = r; r = x % y; } return y; } /* Private instance variables */ private BigInteger num; /* The numerator of this Rational */ private BigInteger den; /* The denominator of this Rational */ }
Java Code:num = a/g; den = Math.abs(y)/g;
this is my first time to post a thread, if there is something wrong,point it out
directly for me.
Last edited by Fubarable; 03062011 at 04:47 PM. Reason: quote tags changed to code tags

 03062011, 04:50 PM #3
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 8
Did you look up all the big integer methods? I believe you may have to change a little. My understanding of them is that you add, divide, etc with method calls. Did you try testing this code, does it compile? If not, what errors are you getting?
 03072011, 05:22 AM #4Member
 Join Date
 Mar 2011
 Posts
 6
 Rep Power
 0
My error in the RationTest class
Before I post my question on the forum,I have seen this method in BigInteger in the javadoc and try to use it,but I still can't solve my
problem.
Here is my code that have made a little change:
Java Code:num = BigInteger.valueOf(x).divide(BigInteger.valueOf(g)); den = BigInteger.valueOf(Math.abs(y)).divide(BigInteger.valueOf(g));
Here is my program to test this class.
Java Code:import acm.program.*; public class RationalTest extends ConsoleProgram { public void run () { Rational a = new Rational ( 1, 2 ); Rational b = new Rational ( 1, 3 ); Rational c = new Rational ( 1, 6 ); Rational sum = a.add(b).add(c); println (a); println (b); println (c); println (sum); } }
Java Code:1/2 1/3 1/6 1/2
 03072011, 02:24 PM #5Member
 Join Date
 Mar 2011
 Posts
 6
 Rep Power
 0
I made it
After thinking twice and referring to the thread BigIntegers syntax.. How to read in from console program I finally make my code run correctly.
Here is my code:
Java Code:import acm.util.*; import java.math.*; public class Rational { public Rational() { this(0); } public Rational(int n) { this(n, 1); } public Rational(int x, int y) { if (y == 0) throw new ErrorException("Division by 0"); int g = gcd(Math.abs(x), Math.abs(y)); num = BigInteger.valueOf(x).divide(BigInteger.valueOf(g)); den = BigInteger.valueOf(Math.abs(y)).divide(BigInteger.valueOf(g)); if (y < 0) num.negate(); } public Rational add(Rational r) { return new Rational(this.num.multiply( r.den) .add(r.num .multiply( this.den)).intValue(), this.den .multiply( r.den).intValue()); } public Rational subtract(Rational r) { return new Rational (this.num .multiply( r.den ).subtract ( r.num .multiply( this.den)).intValue(), this.den .multiply( r.den).intValue()); } public Rational multiply(Rational r) { return new Rational (this.num .multiply( r.num ).intValue(), this.den .multiply( r.den).intValue()); } public Rational divide(Rational r) { return new Rational (this.num .multiply( r.den ).intValue(), this.den .multiply( r.num).intValue()); } public String toString() { if (den .equals(new BigInteger ("1"))) { return "" + num; } else { return num + "/" + den; } } private int gcd(int x, int y) { int r = x % y; while (r != 0) { x = y; y = r; r = x % y; } return y; } /* Private instance variables */ private BigInteger num; /* The numerator of this Rational */ private BigInteger den; /* The denominator of this Rational */ }
If you spot errors,please point them out.
Similar Threads

Basic Java Computation Help Ap Computer Science
By Sean_J in forum New To JavaReplies: 6Last Post: 02102010, 02:10 PM 
My own BigInteger class. Need help.
By Dinde in forum New To JavaReplies: 2Last Post: 01272010, 09:49 PM 
The art and science of java
By islammorad in forum New To JavaReplies: 9Last Post: 12012009, 01:02 PM 
Help On Computer Science Final!!!! Java Program Help!!!!
By BSOS in forum New To JavaReplies: 1Last Post: 12112007, 05:54 AM 
JAVA software solutions:For AP Comp. Science (Lewis,Loftus,Cocking)
By padutch in forum New To JavaReplies: 4Last Post: 11282007, 12:59 AM
Bookmarks