Results 1 to 5 of 5
  1. #1
    dage is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Question 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.
    Here is the author's code:
    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 */
    		}
    My question is how to fix the following code :
    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; 03-06-2011 at 03:47 PM. Reason: quote tags changed to code tags

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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?

  4. #4
    dage is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Unhappy 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));
    To my disappointment,this still can't work.

    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);
    		}
    
    }
    Here is the output:
    Java Code:
    1/2
    1/3
    1/6
    1/2
    what's wrong with my code?


  5. #5
    dage is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Cool 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

  1. Basic Java Computation Help Ap Computer Science
    By Sean_J in forum New To Java
    Replies: 6
    Last Post: 02-10-2010, 01:10 PM
  2. My own BigInteger class. Need help.
    By Dinde in forum New To Java
    Replies: 2
    Last Post: 01-27-2010, 08:49 PM
  3. The art and science of java
    By islam-morad in forum New To Java
    Replies: 9
    Last Post: 12-01-2009, 12:02 PM
  4. Replies: 1
    Last Post: 12-11-2007, 04:54 AM
  5. Replies: 4
    Last Post: 11-27-2007, 11:59 PM

Posting Permissions

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