# Thread: BigInteger Rational (CS106A-Stanford university)

1. Senior Member Join Date
May 2010
Posts
112
Rep Power
0

## BigInteger Rational (CS106A-Stanford university)

Hi Folks,
This is the exercise iam trying to solve from the book "Art and science of Java":

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.
This is the actual Rational class from the chapter:

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
*/
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 */
}```
And this my code at the moment:
Java Code:
```import java.math.BigInteger;
import acm.util.*;
import java.util.*;
import java.math.*;
/**
* The Rational class is used to represent rational numbers, which
* are defined to be the quotient of two integers.
*/

public class Rational {

/**
* Main method
* @param arg
*/

public static void main (String[]arg){

Rational a = new Rational(1,4);
Rational b = new Rational(1,4);
Rational c = new Rational(1,6);
System.out.println(a + " + " + b + " + " + c + " = " + sum);
}

/**
* 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));
BigInteger num = BigInteger.valueOf(x / g);
BigInteger den = BigInteger.valueOf( 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
*/

return new Rational(this.num.multiply(r.den) + r.num.multiply(this.den),
this.den.multiply(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.multiply( r.den) - r.num .multiply( this.den),
this.den.multiply( 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.multiply(r.num), this.den.multiply(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.multiply(r.den), this.den.multiply(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 BigInteger num; /* The numerator of this Rational */
private BigInteger den; /* The denominator of this Rational */
}```
Can anybody give me hints what I need to do resolve this.I want to learn and do it by myself.

Thank you all so much.
Last edited by ccie007; 09-27-2010 at 05:19 PM. Reason: Code  Reply With Quote

2. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Use the CODE tag for code (like you did for the first chunk of code you gave us) otherwise you lose any formatting.

What problem do you have?  Reply With Quote

3. Senior Member Join Date
May 2010
Posts
112
Rep Power
0

## Java Code:
``` private BigInteger num; /* The numerator of this Rational */
private BigInteger den; /* The denominator of this Rational */
}```
I have changed "int" to "BigInteger" in the above lines to resolve the exercise.

changed this lines
Java Code:
```num = x / g;
den = Math.abs(y) / g;```
to this.But not sure I did it right or not.Eclipse editor doesn't complain while compiling.
Java Code:
```BigInteger num = BigInteger.valueOf(x / g);
BigInteger den = BigInteger.valueOf( Math.abs(y) / g);```
Tuff part is changing this

Java Code:
```public Rational add(Rational r) {
return new Rational(this.num * r.den + r.num * this.den,
this.den * r.den);```
to this :

Java Code:
```public Rational add(Rational r) {

return new Rational(this.num.multiply(r.den) + r.num.multiply(this.den),
this.den.multiply(r.den));```
The compiler say there are error in "public Rational add" method.I think I am doing it almost right.Any idea matey?  Reply With Quote

4. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
27

## "this.num.multiply(r.den) + r.num.multiply(this.den)".

It's that "+" sign. You can't add BigIntegers using "+", you need to use add() (or is it sum()?).

Next time you should print the full error message, otherwise we're left guessing here.  Reply With Quote

5. Senior Member Join Date
May 2010
Posts
112
Rep Power
0

## here is the error message that is comming up in the Eclipse editor

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The operator - is undefined for the argument type(s) BigInteger
The constructor Rational(BigInteger, BigInteger) is undefined
The operator - is undefined for the argument type(s) java.math.BigInteger, java.math.BigInteger
The constructor Rational(BigInteger, BigInteger) is undefined
The constructor Rational(BigInteger, BigInteger) is undefined
Incompatible operand types BigInteger and int

at Rational.<init>(Rational.java:50)
at Rational.main(Rational.java:20)

By the way can u explain what BigInteger is for? .I know it roughly.  Reply With Quote

6. ##  Originally Posted by ccie007 By the way can u explain what BigInteger is for? .I know it roughly.
You´re kidding us right?

kind regards,

Jos  Reply With Quote

7. ## operator - is undefined for the argument type(s) BigInteger
Because the operands are objects and not primitives, you need to use methods to do arithmetic with BigInteger objects. See post#4  Reply With Quote

8. Senior Member Join Date
May 2010
Posts
112
Rep Power
0

## No dude I am not kidding. I do not know bigInteger clearly.I know when using biginteger instead of integer we can't use +,-,/ etc rather we have to use add,multiply.But Why do use biginteger instead of integer in the first place.Is it because memory can't hold long decimal numbers like 13.0000000000000000000000000.  Reply With Quote

9. ## The documentation is amazing:
BigInteger (Java 2 Platform SE v1.4.2)

If that's a little much for you, you can read the second reply in this thread, but it points to the documentation as well.  Reply With Quote

10. Senior Member Join Date
May 2010
Posts
112
Rep Power
0

## Can anybody explain to me plz.I tried reading the java doc but found it quite hard to understand.  Reply With Quote

11. ## You didn't even click the second link... did you? Originally Posted by http://www.coderanch.com/t/408968/java/java/difference-between-Integer-BigInteger
Now coming to the BigInteger, you would use it in calculations which deal with very large numbers.The use of BigIntegers is in Security where typically it is used for keys specifications.  Reply With Quote

12. Senior Member Join Date
May 2010
Posts
112
Rep Power
0

## Now I get more ideas with regards to BigInteger by reading this
Big Integers  Reply With Quote

13. Senior Member Join Date
May 2010
Posts
112
Rep Power
0

## Still can't resolve this exeriece.This sam exercise has been discussed before on this forum but I do not want look at it yet.Because I do not want copy others work.

Please guys some one give me any clues how to resolve this exercise.If I learn this way rather than looking at somebody's work I believe I will learn more.Cheers mates.  Reply With Quote

14. ## What is your problem now? Are there compiler errors? Execution errors or logic errors (wrong output)?  Reply With Quote

15. Senior Member Join Date
May 2010
Posts
112
Rep Power
0

## Yay, I resolved the exerice by myself.Thank you guys for your kind support.Here is my final code:
Java Code:
```import java.math.BigInteger;
import acm.util.*;
import java.util.*;
import java.math.*;
/**
* The Rational class is used to represent rational numbers, which
* are defined to be the quotient of two integers.
*/

public class Rational {

/**
* Main method
* @param arg
*/
static final BigInteger ONE = new BigInteger("1");
static final BigInteger ZERO = new BigInteger("0");
static final BigInteger FOUR = new BigInteger("4");
static final BigInteger SIX = new BigInteger("6");
static final BigInteger TWO = new BigInteger("2");

public static void main (String[]arg){

Rational a = new Rational(ONE,FOUR);
Rational b = new Rational(ONE,FOUR);
Rational c = new Rational(ONE,SIX);
System.out.println(a + " + " + b + " + " + c + " = " + sum);
}

/**
* Creates a new Rational initialized to zero.
*/
public Rational() {
this(ZERO);
}
/**
* Creates a new Rational from the integer argument.
* @param n The initial value
*/
public Rational(BigInteger n) {
this(n, ONE);
}
/**
* 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(BigInteger x, BigInteger y) {
if (y.signum() == 0) throw new ErrorException("Division by 0");
BigInteger g = gcd(abs(x),abs(y));
num = x.divide(g);
den =abs(y).divide(g);
if (y.signum()==-1)
num = num.subtract(num.multiply(TWO));
}
/**
* 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
*/

this.den.multiply(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.multiply( r.den).subtract( r.num .multiply( this.den)),
this.den.multiply( 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.multiply(r.num), this.den.multiply(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.multiply(r.den), this.den.multiply(r.num));
}
/**
* Creates a string representation of this rational number.
* @return The string representation of this rational number
*/
public String toString() {
if (den.equals(ONE)) {
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 BigInteger gcd(BigInteger x, BigInteger y) {
BigInteger r = abs(x).mod(abs(y));
while (r.signum()==1) {
x = y;
y = r;
r = abs(x).mod(abs(y));
}
return y;
}
private BigInteger abs(BigInteger x) {
// TODO Auto-generated method stub
return x;
}
/* Private instance variables */
private BigInteger num; /* The numerator of this Rational */
private BigInteger den; /* The denominator of this Rational */
}```
Last edited by ccie007; 10-01-2010 at 07:30 PM.  Reply With Quote

16. Senior Member Join Date
May 2010
Posts
112
Rep Power
0

## Can anybody explain me what is happening behind this part of the code please.

Java Code:
```
/**
* Creates a new Rational initialized to zero.
*/
public Rational() {
this(ZERO);
}
/**
* Creates a new Rational from the integer argument.
* @param n The initial value
*/
public Rational(BigInteger n) {
this(n, ONE);
}
/**
* 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(BigInteger x, BigInteger y) {
if (y.signum() == 0) throw new ErrorException("Division by 0");
BigInteger g = gcd(abs(x),abs(y));
num = x.divide(g);
den =abs(y).divide(g);
if (y.signum()==-1)
num = num.subtract(num.multiply(TWO));
}
/**```
I know about numerator and denomenator being divided by gcd bit but I am confused with what those three contructors are doing.

Here is the explanation from the book for those three constructors but I still can't understand it, it may because it is short explanation.Can anybody elaborate for me please:
• Multiple constructors: The Rational class in Figure 6-9 defines three different
constructors. Calling new Rational() creates a new rational number whose value is
0, which is represented internally as the fraction 0 / 1. Calling new Rational(n)
creates a new rational number equal to the integer n, which is simply the fraction n / 1.
Finally, calling new Rational(x, y) creates a new rational number equal to the
fraction x / y.  Reply With Quote

17. ## It looks like each constructor starting at the top calls the next one down.
the 'this' is a call to a constructor.
So this(ZERO) in the no arg constructor calls the constructor with a single arg  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•