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:
Quote:
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.
Here is the author's code:
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:
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 */
}
My question is how to fix the following code :
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.
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:
Code:
num = BigInteger.valueOf(x).divide(BigInteger.valueOf(g));
den = BigInteger.valueOf(Math.abs(y)).divide(BigInteger.valueOf(g));
To my disappointment,this still can't work.
Here is my program to test this class.
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);
}
}
Here is the output:
what's wrong with my code?