Results 1 to 17 of 17
 09272010, 02:55 PM #1Senior Member
 Join Date
 May 2010
 Posts
 112
 Rep Power
 0
BigInteger Rational (CS106AStanford 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.
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 */ }
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); Rational sum = a.add(b).add(c); 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 */ public Rational add(Rational 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 */ }
Thank you all so much.Last edited by ccie007; 09272010 at 04:19 PM. Reason: Code
 09272010, 03:26 PM #2Moderator
 Join Date
 Apr 2009
 Posts
 12,775
 Rep Power
 22
Use the CODE tag for code (like you did for the first chunk of code you gave us) otherwise you lose any formatting.
So, what's your question?
What problem do you have?
 09272010, 04:27 PM #3Senior 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 */ }
changed this lines
Java Code:num = x / g; den = Math.abs(y) / g;
Java Code:BigInteger num = BigInteger.valueOf(x / g); BigInteger den = BigInteger.valueOf( Math.abs(y) / g);
Java Code:public Rational add(Rational r) { return new Rational(this.num * r.den + r.num * this.den, this.den * r.den);
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));
 09272010, 04:53 PM #4Moderator
 Join Date
 Apr 2009
 Posts
 12,775
 Rep Power
 22
"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.
 09282010, 07:23 PM #5Senior 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.
 09282010, 07:29 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,271
 Blog Entries
 7
 Rep Power
 24
 09282010, 07:33 PM #7operator  is undefined for the argument type(s) BigInteger
 09282010, 07:52 PM #8Senior 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.
 09282010, 08:30 PM #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.
 09282010, 08:37 PM #10Senior 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.
 09282010, 08:41 PM #11
You didn't even click the second link... did you?
Originally Posted by http://www.coderanch.com/t/408968/java/java/differencebetweenIntegerBigInteger
Here's more information on the range of the types: Java: java.math.BigInteger
 09282010, 08:54 PM #12Senior Member
 Join Date
 May 2010
 Posts
 112
 Rep Power
 0
Now I get more ideas with regards to BigInteger by reading this
Big Integers
 09292010, 07:57 PM #13Senior 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.
 09292010, 09:56 PM #14
What is your problem now? Are there compiler errors? Execution errors or logic errors (wrong output)?
 10012010, 05:42 PM #15Senior 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); Rational sum = a.add(b).add(c); 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 */ public Rational add(Rational r) { return new Rational(this.num.multiply(r.den).add( 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).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 Autogenerated 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; 10012010 at 06:30 PM.
 10012010, 07:09 PM #16Senior 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)); } /**
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 69 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.
 10012010, 07:16 PM #17
Similar Threads

Java Checkerboad question CS106A Stanford univerity
By ccie007 in forum New To JavaReplies: 71Last Post: 09202013, 04:02 AM 
CS106A Stanford University
By Learning Java in forum New To JavaReplies: 116Last Post: 07092011, 04:43 PM 
Class exercise CS106A (Stanford university)
By ccie007 in forum New To JavaReplies: 2Last Post: 09112010, 01:47 AM 
How to run java class in ECLIPSE(CS106A Stanford)
By ccie007 in forum New To JavaReplies: 17Last Post: 09112010, 12:18 AM 
Eclipse vs Rational Software Development
By tommy in forum Other IDEsReplies: 2Last Post: 05152008, 05:05 PM
Bookmarks