CS 3401 Homeworks 3

Homework # 3
Exercise #1

Write an implementation of the Rational.java class and that uses BigInteger for numerator and denominator. Write a test driver class called TestRational.java that creates at least two Rational numbers (using BigIntegers as the arguments to the Rational constructor) and then adds, subtracts, multiplies and divides the Rational numbers and tests them for equality, printing out the results of each of these operations.

Be sure your programs compile and run without error.

Review the Submission Requirements document and make sure your submission meets those requirements. Please also review the Grading Guidelines for an indication of general guidelines used in evaluating your assignments.
Deliverables

Turn in the files:

    Rational.java
    TestRational.java

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Rational.java
package assignment3;

import java.math.BigInteger;

public class Rational extends Number implements Comparable {
  // Data fields for numerator and denominator
  private BigInteger numerator = new BigInteger("0");
  private BigInteger denominator = new BigInteger("1");

  /** Construct a rational with default properties */
  public Rational() {
    this(BigInteger.valueOf(0), BigInteger.valueOf(1));
  }

  /** Construct a rational with specified numerator and denominator */
  public Rational(BigInteger numerator, BigInteger denominator) {
    BigInteger gcd = gcd(numerator, denominator);
    this.numerator = BigInteger.valueOf(denominator.signum()).multiply(numerator).divide(gcd);
    this.denominator = denominator.abs().divide(gcd);
  }

  /** Find GCD of two numbers */
  private static BigInteger gcd(BigInteger n, BigInteger d) {
    BigInteger n1 = n.abs();
    BigInteger n2 = d.abs();
    BigInteger gcd = new BigInteger("1");

    for (int k = 1; k <= n1.intValue() && k <= n2.intValue(); k++) {
      if (n1.intValue() % k == 0 && n2.intValue() % k == 0)
        gcd = BigInteger.valueOf(k);
    }

    return gcd;
  }

  /** Return numerator */
  public BigInteger getNumerator() {
    return numerator;
  }

  /** Return denominator */
  public BigInteger getDenominator() {
    return denominator;
  }

  /** Add a rational number to this rational */
  public Rational add(Rational secondRational) {
    BigInteger n = (numerator.multiply(secondRational.getDenominator())).add(denominator.multiply(secondRational.getNumerator()));
    BigInteger d = denominator.multiply(secondRational.getDenominator());
    return new Rational(n, d);
  }

  /** Subtract a rational number from this rational */
  public Rational subtract(Rational secondRational) {
    BigInteger n = (numerator.multiply(secondRational.getDenominator())).subtract(denominator.multiply(secondRational.getNumerator()));
    BigInteger d = denominator.multiply(secondRational.getDenominator());
    return new Rational(n, d);
  }

  /** Multiply a rational number to this rational */
  public Rational multiply(Rational secondRational) {
    BigInteger n = numerator.multiply(secondRational.getNumerator());
    BigInteger d = denominator.multiply(secondRational.getDenominator());
    return new Rational(n, d);
  }

  /** Divide a rational number from this rational */
  public Rational divide(Rational secondRational) {
    BigInteger n = numerator.multiply(secondRational.getDenominator());
    BigInteger d = denominator.multiply(secondRational.numerator);
    return new Rational(n, d);
  }

  /** Override the toString() method */
  public String toString() {
    if (denominator.intValue() == 1)
      return numerator + "";
    else
      return numerator + "/" + denominator;
  }

  /** Override the equals method in the Object class */


  public boolean equals(Object parm1) {
    if ((this.subtract((Rational)(parm1)).getNumerator()).intValue() == 0)
        return true;
    else
      return false;
  }

  /** Override the abstract intValue method in java.lang.Number */
  public int intValue() {
    return (int)doubleValue();
  }

  /** Override the abstract floatValue method in java.lang.Number */
  public float floatValue() {
    return (float)doubleValue();
  }

  /** Override the doubleValue method in java.lang.Number */
  public double doubleValue() {
      double x = this.getNumerator().doubleValue();
      double y = this.getDenominator().doubleValue();
      return x / y;
   
  }

  /** Override the abstract BigIntegerValue method in java.lang.Number */
  public long longValue() {
    return (long)doubleValue();
  }

  /** Override the compareTo method in java.lang.Comparable */
  public int compareTo(Object o) {
    if (((this.subtract((Rational)o)).getNumerator()).intValue() > 0)
      return 1;
    else if (((this.subtract((Rational)o)).getNumerator()).intValue() < 0)
      return -1;
    else
      return 0;
  }

 
  }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TestRational.java
package assignment3;
import java.math.BigInteger;

public class TestRational extends Rational {
   
    public static void main(String[] args)
    {

    Rational c1 = new Rational(new BigInteger("4"), new BigInteger("3"));
    Rational c2 = new Rational(new BigInteger("2"), new BigInteger("3"));
   
    System.out.println(c1 + " plus " + c2 + " = " + c1.add(c2));
    System.out.println(c1 + " minus "+ c2 + " = " + c1.subtract(c2));
    System.out.println(c1 + " multiply " + c2 + " = " + c1.multiply(c2));
    System.out.println(c1 + " divide " + c2 + " = " + c1.divide(c2));
   
    System.out.println(c2 + " is " + c2.doubleValue());
}
}

Comments

Popular posts from this blog

How to clear & format SD card

Sharepoint List: Days Elapsed or Countdown

CS4500 Test 4 Study Guide