Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Carefully study the provided BigRational class in following code. If you find any logic errors, list them and list them as comments at the top

Carefully study the provided BigRational class in following code. If you find any logic errors, list them and list them as comments at the top of the corresponding source file (You may not find any!). Do the following modifcations to the BigRational class. Make sure to throw appropriate exceptions. 1. BigRational pow( int exp ) // exception if exp < 0 2. BigRational reciprocal( ) 3. BigInteger toBigInteger( ) // exception if denominator is not 1 4. int toInteger( ) // exception if denominator is not 1 5. Modify the BigRational class so that 0=0 is legal and as interpreted as \indeterminate" by toString. 6. Implement Comparable interface for BigRational class. Store \1 / 3", \1 / 5", \1 / 8", \2 / 8", \ 3 / 8' as BigRational in an ArrayList. Now use the stock generic algorithm, sort from Collections clsss to sort the content. Print the nal array list and show that it was actually sorted

package bigrationaltest;

import java.math.BigInteger;

/** * This class represents rational numbers. A rational number stores a numerator * and denominator, and we will use BigIntegers to represent the numerator and * denominator. Thus our class will be aptly named BigRational * * Following concepts haven used. public static final constants use of an * existing class, namely BigInteger (composition) multiple constructors * throwing exceptions implementing a set of accessors implementing equals and * toString */ public class BigRational {

public static final BigRational ZERO = new BigRational(); public static final BigRational ONE = new BigRational("1");

private BigInteger num; // only this can be nagative private BigInteger den; // never negative

public BigRational() { this(BigInteger.ZERO); }

public BigRational(BigInteger n) { this(n, BigInteger.ONE); }

public BigRational(BigInteger num, BigInteger den) { this.num = num; this.den = den; check00(); fixSigns(); reduce(); }

public BigRational(String str) { if (str.length() == 0) { throw new IllegalArgumentException("empty string"); }

int slashIndex = str.indexOf('/'); if (slashIndex == -1) { num = new BigInteger(str.trim()); den = BigInteger.ONE; // den = 1 } else { num = new BigInteger(str.substring(0, slashIndex).trim()); den = new BigInteger(str.substring(slashIndex + 1).trim()); check00(); fixSigns(); reduce(); } }

private void check00() { if (num.equals(BigInteger.ZERO) && den.equals(BigInteger.ZERO)) { throw new ArithmeticException("division by zero"); } }

private void fixSigns() { if (den.compareTo(BigInteger.ZERO) < 0) { num = num.negate(); den = den.negate(); } }

private void reduce() { BigInteger gcd = num.gcd(den); num = num.divide(gcd); den = den.divide(gcd);

}

/** * Get the absolute value * * @return */ public BigRational abs() { return new BigRational(num.abs(), den); }

/** * Negate the number * * @return */ public BigRational negate() { return new BigRational(num.negate(), den); }

/** * Add two numbers * * @param other * @return */ public BigRational add(BigRational other) { BigInteger newNumerator = this.num.multiply(other.den).add(other.num.multiply(this.den)); BigInteger newDenominator = this.den.multiply(this.num); return new BigRational(newNumerator, newDenominator); }

/** * Subtract two numbers * * @param other * @return */ public BigRational subtract(BigRational other) { return add(other.negate()); }

/** * Multiply two numbers * * @param other * @return */ public BigRational multiply(BigRational other) { BigInteger newNumerator = this.num.multiply(other.num); BigInteger newDenominator = this.den.multiply(other.den); return new BigRational(newNumerator, newDenominator); }

/** * Divide two numbers * * @param other * @return */ public BigRational divide(BigRational other) { BigInteger newNumerator = this.num.multiply(other.den); BigInteger newDenominator = this.den.multiply(other.num); return new BigRational(newNumerator, newDenominator); }

@Override public boolean equals(Object other) { if (other instanceof BigRational) { BigRational rhs = ((BigRational) other); return num.equals(rhs.num) && den.equals(((BigRational) other).den); } return false; }

@Override public String toString() { if (den.equals(BigInteger.ZERO)) { if (num.compareTo(BigInteger.ZERO) < 0) { return "-Infinity"; } else { return "Infinity"; } }

if (den.equals(BigInteger.ONE)) { return num.toString(); } else { return num + " / " + den; } } }

public class BigRationalTest {

public static void main(String[] args) { BigRational br1 = new BigRational(BigInteger.ONE, BigInteger.TEN); System.out.println(br1.toString()); BigRational br2 = new BigRational("1/0"); System.out.println(br2.toString()); BigRational br3 = new BigRational("-1/0"); System.out.println(br3.toString()); BigRational result = (new BigRational("2/10")).multiply(new BigRational("2/5")); System.out.println(result.toString()); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

True or False: 2 * ( n ^ 2 ) = o ( n ^ 2 )

Answered: 1 week ago