Results 1 to 5 of 5
  1. #1
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Question BigIntegers syntax.. How to read in from console program

    How to i read in a BigInteger from the console prgram??

    The exercise was to rewrite a rational number class using BigIntegers ( the original code which uses ints was provided in my study text and is posted here: trying to understand this rational numbers class

    after consulting javadoc and other stuff on google I think i have achieved the goal (at least eclipse shows no compile errors) however i am having a mental block or something, because i cant work out how to test my code because i cannot read in a biginteger.

    i have tried various things using scrapbook and a console program . I am assuming that I have to read in a string and convert it to a biginteger.
    I arrived at this notion because of entry number 5 in the constructor summary information at javadoc states:

    BigInteger(String val)
    Translates the decimal String representation of a BigInteger into a BigInteger.

    and i am assuming this is the way I must do it,,

    it has occured to me however that perhaps my code (despite having no compile errors) is a pile of junk! so ive posted it below.

    also there is something about throwing exceptions which i may not have done in the conventional way, i am not concerned with this too much at the moment, but any suggestions on throwing exceptions will be gratefully accepted.

    BigRational code:
    Java Code:
    import java.math.BigInteger;
    import acm.util.ErrorException;
    
    public class BigRational { 
    	private BigInteger numerator;
    	private BigInteger denominator;
    	
    	
    	public BigRational(){
    		this(BigInteger.ZERO);
    	}
    	
    	public BigRational(BigInteger numerator){
    		this(numerator,BigInteger.ONE);
    		
    	}
    	public BigRational(BigInteger numerator, BigInteger denominator) {
    		if (BigInteger.ZERO == denominator)
    			throw new ErrorException("Cannot divide by zero.");
    		BigInteger gcd = numerator.abs().gcd(denominator.abs());
    		this.numerator = numerator.divide(gcd);
    		this.denominator = denominator.divide(gcd);
        }
    
    	public BigRational add(BigRational r) {
    		return new BigRational
    		(this.numerator.multiply(r.denominator)
    				.add(r.numerator.multiply(this.denominator)), 
    				this.denominator.multiply(r.denominator));
    	}
    
    	public BigRational subtract(BigRational r) {
    		return new BigRational(this.numerator.multiply(r.denominator)
    				.subtract(r.numerator).multiply(this.denominator),
    				this.denominator.multiply(r.denominator));
    	}
    
    	public BigRational multiply(BigRational r) {
    		return new BigRational(this.numerator.multiply(r.numerator), 
    				this.denominator.multiply(r.denominator));
    	}
    
    	public BigRational divide(BigRational r) {
    		return new BigRational(this.numerator.multiply(r.denominator),
    				this.denominator.multiply(r.numerator));
    	}
    
    	public String toString() {
    		if (denominator == BigInteger.ONE) {
    			return "" + numerator;
    		} else {
    			return numerator + "/" + denominator;
    		}
    	}	
    
    }
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    your code looks good.

    Instead of throwing ErrorException consider IllegalArgumentException

    as far as parsing string into BigInteger, you should indeed be able to use

    String str = "123";
    new BigInteger (str);

    when you're reading from console, one thing to watch out for is un-intended whitespace in your string. So just in case, do this:

    // acquire String str somewhere
    str = str.trim (); // strips the whitespace
    new BigInteger (str);

  3. #3
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Thumbs up thanks!

    Thanks mate,
    it might have helped if i had imported java.math into my scrapbook :o
    perhaps i would have remembered had i not had a few beers! LOL



    Java Code:
    String num = readLine("  Numerator: ");
    num.trim();
    String den = readLine("Denominator: ");
    den.trim();
    BigInteger numerator = new BigInteger(num);
    BigInteger denominator = new BigInteger(den);
    BigRational one = new BigRational(numerator,denominator);
    and now everything seems to work nicely
    thanks
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

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

    Default

    Quote Originally Posted by sonny View Post
    Thanks mate,
    it might have helped if i had imported java.math into my scrapbook :o
    perhaps i would have remembered had i not had a few beers! LOL
    Or perhaps not enough: Ballmer Peak

    Looks like your programming is coming along nicely. Keep it up.

  5. #5
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Talking

    Quote Originally Posted by Fubarable View Post
    Or perhaps not enough: Ballmer Peak

    Looks like your programming is coming along nicely. Keep it up.
    many thanks

    hopefully its coming along as well as my capacity for beer!

    Java Code:
    private void gotSomeTime2Kill(int beersInFridge, boolean WifeAndKidsInBed);
    	while (beersInFridge > 0 && wifeAndKidsInBed){
    		consumeOneBeer();
    		beersInFridge--;
    		beerConsumed++
    		doSomeMoreCode();
    		if(beerConsumed > BallmerPeak) postSomeDaftHelpRequest();
    		else {
    			gotSomeTimeToKill(beersInFridge,(true));
    		}
    	}
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

Similar Threads

  1. Ready To Program, Images in HSA Console?
    By habbah in forum New To Java
    Replies: 2
    Last Post: 01-17-2010, 03:50 AM
  2. How to read input from console
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-16-2008, 10:57 PM
  3. How to read input from the console
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-06-2008, 07:41 PM
  4. How to read a Web Page through java program
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-04-2008, 02:37 PM
  5. Read from console (Scanner Class)
    By hey in forum New To Java
    Replies: 10
    Last Post: 12-11-2007, 10:11 PM

Tags for this Thread

Posting Permissions

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