Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6786235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:13:16+00:00 2026-05-26T17:13:16+00:00

import java.math.BigInteger; public class Rational extends Number implements Comparable { // Data fields for

  • 0
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 = this.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() {
    return numerator.divide(denominator).doubleValue();
  }

  /** 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;
  }

  public static void main(String[] args) {
    // Create and initialize two rational numbers r1 and r2.
    Rational r1 = new Rational(BigInteger.valueOf(1), BigInteger.valueOf(2));
    Rational r2 = new Rational(BigInteger.valueOf(1), BigInteger.valueOf(2));

    for (int i = 2; i <= 100; i++){
        r2.numerator = BigInteger.valueOf(i);
        r2.denominator = r2.denominator.add(BigInteger.valueOf(1));


        System.out.println("r2: " + r2.getNumerator() + "/" + r2.getDenominator());

        r1 = r1.add(r2);        
    }
    // Display results 

    System.out.println("r1: " + r1.getNumerator() + "/" + r1.getDenominator());
  }

I have been working on this for a few hours and I absolutely hit a wall. Goal is to use BigInteger to sum a series of fractions. For some reason, the numerator and denominator of r2 are equal, when they are supposed to be 1/2. The end result is also not correct, as the denominator should not be 0. What am I missing, or what am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:13:17+00:00Added an answer on May 26, 2026 at 5:13 pm

    The problem is this line:

    this.denominator = this.denominator.abs().divide(gcd); 
    

    Change to:

    this.denominator = denominator.abs().divide(gcd); 
    

    The first one always uses 1 as denominator as that’s how it’s initialized.

    However, your gcd implementation is VERY ineficient…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem import java.math.BigInteger; import java.io.*; import java.util.*; import java.lang.*; public class
import java.lang.Math; import java.awt.* public class Triangle implements Shape { java.awt.Point a; java.awt.Point b;
import java.math.BigInteger; import java.util.concurrent.*; public class MultiThreadedFib { private ExecutorService executorService; public MultiThreadedFib(final int
import java.lang.Math; public class NewtonIteration { public static void main(String[] args) { System.out.print(rootNofX(2,9)); }
I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo { public static void main(String[]
import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class Filereader { public static void main(String[]
import java.lang.reflect.Array; public class PrimitiveArrayGeneric { static <T> T[] genericArrayNewInstance(Class<T> componentType) { return (T[])
Given the following program: import java.io.*; import java.util.*; public class GCTest { public static
Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.